2017-01-27 10 views
0

Я хочу создать VMware VM с помощью команды New-VM и соответственно переименовать имя виртуальной машины (на уровне Windows) в соответствии с именем виртуальной машины.не удается переименовать компьютер на уровне ОС Windows с помощью команды Invoke-VMScript

Что-то вроде: VM Name = 100-SVR01-Jack для Windows Host Name = Jack-100

Проблема: Я не может переименовать компьютер Джек-100 на окнах уровня с помощью команды Invoke-VMScript , кажется, что он не может передать 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

Я обычно использую обязательные теги параметров при определении функции: https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/22/use-powershell-to-make-mandatory-parameters/

Doing, которые могут помочь вам передать переменную в должным образом. Также вы можете изменить свое предпочтительное действие с ошибкой, если вы не видите все.