次のサンプル スクリプトをカスタマイズして使用し、クローン作成した仮想マシン (VM) を Active Directory (AD) ドメインに参加させることができます。このスクリプトでは SSH を使用して、Linux 仮想マシンでコマンドを実行します。
Active Directory 統合に Winbind ソリューションを使用する場合は、このスクリプトを実行する必要があります。クローン作成した仮想マシンでは、ドメインに参加する手順がエラーになるためです。このスクリプトでは、ドメインに参加するためのコマンドが各仮想マシンで実行されます。OpenLDAP ソリューションを使用する場合、このスクリプトを実行する必要はありません。
改ページせずにスクリプトの内容をコピーして貼り付けるには、このトピックの HTML 版を使用します。HTML 版は https://www.vmware.com/support/pubs/view_pubs.html にある Horizon 7 のドキュメントのページから入手できます。
スクリプト入力
このスクリプトは、Linux デスクトップを展開するサンプル PowerCLI スクリプトの入力ファイルで説明しているように 1 つの入力ファイルを読み取ります。また、このスクリプトは、次の情報をインタラクティブに確認します。
- vCenter Server の IP アドレス
- vCenter Server 管理者のログイン名
- vCenter Server 管理者のパスワード
- Linux 仮想マシンのユーザー ログイン名
- Linux 仮想マシンのユーザー パスワード
- マシンをドメインに参加させる許可を受けた Active Directory ユーザーのログイン名
- 許可された Active Directory ユーザーのパスワード
スクリプトのコンテンツ
<#
.SYNOPSIS
run command "sudo /usr/bin/net ads join" via SSH
.DESCRIPTION
The tool is to run the command "sudo /usr/bin/net ads join" to join Linux machine to AD via SSH
.NOTES
#>
#------------------------- Functions -------------------------
function GetInput
{
Param($prompt, $IsPassword = $false)
$prompt = $prompt + ": "
Write-Host $prompt -NoNewLine
[Console]::ForegroundColor = "Blue"
if ($IsPassword)
{
$input = Read-Host -AsSecureString
$input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
}
else
{
$input = Read-Host
}
[Console]::ResetColor()
return $input
}
function Check_SSH_Client
{
Param($IsPlink, $IsPSCP)
if ($IsPlink)
{
if (Test-Path ".\plink.exe")
{
write-host -ForeGroundColor Yellow 'SSH client "plink.exe" found'
}
else
{
write-host -ForeGroundColor Red 'SSH client "plink.exe" not found, please download from its official web site'
exit
}
}
if ($IsPSCP)
{
if (Test-Path ".\pscp.exe")
{
write-host -ForeGroundColor Yellow 'SSH client "pscp.exe" found'
}
else
{
write-host -ForeGroundColor Red 'SSH client "pscp.exe" not found, please download from its official web site'
exit
}
}
}
function RunCmdViaSSH
{
Param($VM_Name, $User, $Password, $Cmd, $returnOutput = $false)
$VM= Get-VM $VM_Name
$IP = $VM.guest.IPAddress[0]
write-host "Run cmd on $VM_Name ($IP)"
if($returnOutput)
{
$command = "echo yes | .\plink.exe -ssh -l $user -pw $password $IP " + '"' + $cmd +'"'
$output = Invoke-Expression $command
return $output
}
else
{
echo yes | .\plink.exe -ssh -l $user -pw $password $IP "$cmd"
}
}
function UploadFileViaSSH
{
Param($VM_Name, $User, $Password, $LocalPath, $DestPath)
$VM= Get-VM $VM_Name
$IP = $VM.guest.IPAddress[0]
$command = "echo yes | .\pscp.exe -l $User -pw $Password $LocalPath $IP" + ":" + "$DestPath"
write-host "Upload file: $command"
Invoke-Expression $command
}
#------------------------- Handle input -------------------------
"-----------------------------------------------------"
Check_SSH_Client -IsPlink $true -IsPSCP $false
"-----------------------------------------------------"
$vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false
$vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false
$vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true
"-----------------------------------------------------"
$guestUser = GetInput -prompt 'Your VM guest OS user name' -IsPassword $false
$guestPassword = GetInput -prompt 'Your VM guest OS user password' -IsPassword $true
"-----------------------------------------------------"
$adUser = GetInput -prompt 'Type the AD user name to join the AD' -IsPassword $false
""
"`nPlease type the AD user password."
[Console]::ForegroundColor = "Yellow"
"Plase note that special character should be escaped. For example, $ should be \$"
[Console]::ResetColor()
$adUserPassword = GetInput -prompt 'Your AD user password' -IsPassword $true
"-----------------------------------------------------"
#$csvFile = Read-Host 'Csv File '
$csvFile = '.\CloneVMs.csv'
#------------------------- Main Script -------------------------
#Connect to vCenter
$VC_Conn_State = Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
if([string]::IsNullOrEmpty($VC_Conn_State))
{
Write-Host 'Exit since failed to login vCenter'
exit
}
else
{
Write-Host 'vCenter is connected'
}
#Read input CSV file
$csvData = Import-CSV $csvFile
$destFolder = "/home/$guestUser/"
#Handle VMs one by one
foreach ($line in $csvData)
{
"-----------------------------------------------------"
$VMName = $line.VMName
write-host -ForeGroundColor Yellow "VM: $VMName`n"
$cmd = "sudo /usr/bin/net ads join -U $adUser%$adUserPassword"
Write-Host "Run cmd 'sudo /usr/bin/net ads join' in VM '$VMName' with user '$guestUser'"
RunCmdViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -Cmd $cmd
}
Disconnect-VIServer $vcAddress -Confirm:$false
exit
スクリプトの実行
このスクリプトを実行すると、次のメッセージが表示されます。
PowerCLI C:\scripts> .\ClonedVMs_JoinDomain_SSH.ps1 -------------------------------------------------- Your vCenter address: 10.117.44.17 Your vCenter admin user name: administrator Your vCenter admin user password: ******* -------------------------------------------------- Your VM guest OS user name: ViewUser Your VM guest OS user password: ******* -------------------------------------------------- Type the AD user name to join the AD: viewadmin Please type the AD user password. Please note that special character should be escaped. For example, $ should be \$ Your AD user password: *******