我想使用New-VM命令创建VMware VM,并根据VM名称相应地重命名VM名称(在Windows级别)。
像: VM名称= 100-SVR01-Jack Windows主机名= Jack-100
问题:我无法使用Invoke-VMScript命令在Windows级别将计算机重命名为Jack-100,似乎无法将newcomputername传递给Invoke命令。任何人都可以帮我吗?
$ErrorActionPreference = "Stop"
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIserver $vCenterName -user $vCenterUserName -password "Password01" -ErrorAction 'Stop'
$CID = '100'
$CName = "Jack"
$ComputerName1 = $CID + "-SVR01-" + $CName
$NewComputerName1 = "SVR01-" + $CID
$sourcetemplate = "BaseWin2012R2"
$description = "Jack System"
$OSCustomizationspec = "SVR01"
$InfraResourcePool = "Infra-ResourcePool"
Function Create-VM (ComputerName1 , $InfraResourcePool,$description,$sourcetemplate,$OSCustomizationspec ){
New-VM -Name $ComputerName1 -ResourcePool $InfraResourcePool -Datastore "datastore1" -Description $description -Template $sourcetemplate -OSCustomizationspec $OSCustomizationspec -DiskStorageFormat Thin
Start-VM -VM $ComputerName1
}
Function Set-Computername($ComputerName , $NewComputerName , $HostCredential , $GuestCredential) {
$RenameComputer = '$hostname = hostname
Rename-Computer -computername $hostname -newname $NewComputerName
RESTART-COMPUTER -force
'
Invoke-VMScript -VM $computername -HostCredential $HostCredential -GuestCredential $GuestCredential -ScriptText $RenameComputer
}
$HostCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $computername", "root", "")
$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $computername", "administrator", "")
Create-VM $Computername1 $InfraResourcePool $description $sourcetemplate $OSCustomizationspec;
Set-Computername $Computername1 $NewComputerName1 $HostCred $GuestCred
答案 0 :(得分:0)
我通常在定义函数时使用强制参数标记:https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/22/use-powershell-to-make-mandatory-parameters/
这样做可以帮助您正确传递变量。此外,您可能希望更改首选的错误操作,以防您没有看到所有内容。