Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 179681

Deploy VM using Remote PowerShell

$
0
0

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


Viewing all articles
Browse latest Browse all 179681

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>