We're trying to write a script to deploy VM's using Remote PowerShell. If we execute locally it works every time. If we execute remote it hangs in the same spot every time. Anyone have any ideas?
$session = New-PSSession -ComputerName localhost -ConfigurationName Microsoft.PowerShell32
Invoke-Command -Session $session -ScriptBlock {
#Add the PowerCli Snapin
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Add-PSSnapin VMware.VimAutomation.Vds -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -InvalidCertificateAction "Ignore" -Confirm:$false
#Set variables
[int]$SuccessCode = 1
$hosts = @(
"vcenter1",
"vcenter2"
);
$VCCred= "user"
$VCPwd= "psswrd"
$Domain= "mydomain.com"
$DNS1= "192.168.1.1"
$DNS2= "192.168.1.2"
$VMName = "LNX002"
$Template= "RHEL-SM-02"
$ClusterName= "Cluster"
$LUN = "SAN_LUN"
$VDPortGroup = "Public"
#Connect to VCenter Servers
Connect-VIServer -Server $hosts -User $VCCred -Password $VCPwd
#Deploy New VM
$Cluster = Get-Cluster -Name $ClusterName
$NewHost = Get-VMHost -Location $Cluster | Sort $_.MemoryUsageGB -Descending | Select -First 1
$NewDatastore = $NewHost | Get-Datastore | Where-Object -FilterScript {$_.Name -like "$LUN"} | Sort-Object -Descending -Property FreeSpace | Select-Object -Property Name, FreeSpaceMB -First 1
$custSpec = New-OSCustomizationSpec -Type NonPersistent -OSType Linux -NamingScheme Fixed -NamingPrefix LNX002 -DnsSuffix $Domain -Domain $Domain -DnsServer $DNS1,$DNS2
$CustSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseDHCP
$NewVM = New-VM -Name $VMName -Template $Template -ResourcePool $ClusterName -Datastore $NewDatastore.Name -OSCustomizationSpec $custSpec
#$myNetworkAdapter = Get-VM -Name $VMName | Get-NetworkAdapter | Select-Object -First 1
$myVDPortGroup = Get-VDPortGroup -Name $VDPortGroup
#Set-NetworkAdapter -NetworkAdapter $myNetworkAdapter -Portgroup $myVDPortGroup -Confirm:$false
#Set-NetworkAdapter -NetworkAdapter $myNetworkAdapter -StartConnected:$true -Confirm:$false
Get-VM -Name $NewVM | Get-NetworkAdapter | Select-Object -First 1 | Set-NetworkAdapter -StartConnected:$true -Confirm:$false -RunAsync
****************************Script hangs right here**********************************************************************************
Get-VM -Name $NewVM | Get-NetworkAdapter | Select-Object -First 1 | Set-NetworkAdapter -Portgroup $myVDPortGroup -Confirm:$false -RunAsync
Get-VM -Name $NewVM | Start-VM -Confirm:$false
$DeployedVM = Get-VM -Name $NewVM
#$DeployedVM.PowerState
IF ($DeployedVM.PowerState -eq "PoweredOn") {
$SuccessCode = 0
}
ELSE {
$SuccessCode = 1}
}
$SuccessCode = Invoke-Command -Session $session -ScriptBlock { $SuccessCode }
$SuccessCode
And where does it hang ? On the Add-PSSnapin ?
Any exceptions ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hangs right here. No exception just won't go past this step. (-RunAsync not required. It was just something we tried to get past the hang.
Get-VM -Name $NewVM | Get-NetworkAdapter | Select-Object -First 1 | Set-NetworkAdapter -StartConnected:$true -Confirm:$false -RunAsync
****************************Script hangs right here**********************************************************************************
Get-VM -Name $NewVM | Get-NetworkAdapter | Select-Object -First 1 | Set-NetworkAdapter -Portgroup $myVDPortGroup -Confirm:$false -RunAsync
Get-VM -Name $NewVM | Start-VM -Confirm:$false
$DeployedVM = Get-VM -Name $NewVM
#$DeployedVM.PowerState
IF ($DeployedVM.PowerState -eq "PoweredOn") {
$SuccessCode = 0
}
ELSE {
$SuccessCode = 1}
}
$SuccessCode = Invoke-Command -Session $session -ScriptBlock { $SuccessCode }
$SuccessCode
And do you get the same phenomena when you set up an interactive remote session and run that line ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Nope, if we run it interactivity it work fine.
And you are sure you are running the script with the same account ?
Can you perhaps show how you start the remote session and start the script ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
Looks like you might be right. Some weird authentication problem. We took the code as it is and dropped it into our workflow and it worked fine. Just plain weird.
Thanks for looking at it.