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

Start-Job with PowerCLI not working

$
0
0

This script never gets past the first write-host line in the start-job...

 

 

 

# Do work
$csv = Import-Csv tmpbuild.csv

 

foreach ($item in $csv) {
    Start-Job -RunAs32 -ScriptBlock {
   
        # Add PSSnapins
       
        Add-PSSnapin VMware.VimAutomation.Core
        Add-PSSnapin VMware.VimAutomation.Cloud
       
        # Load Functions
        cd e:
        . E:\Cloud\Scripts\Functions\set-civm.ps1
        . e:\Cloud\Scripts\Functions\new-ciharddisk.ps1
        . e:\Cloud\Scripts\Functions\Sharing-CIVapp.ps1
        . e:\Cloud\Scripts\Functions\Instantiate-VApp.ps1
        . e:\Cloud\Scripts\Functions\Set-CIVMAdminPassword.ps1
   
        # Connect to vCloud and vCenters
        Write-Host -ForegroundColor Cyan "Connecting to vCloud and vCenters"
        if (-not $global:defaultciserver) {
            Connect-CIServer uscloud.cloud.com
        }
        if (-not $global:defaultviservers) {
            Connect-VIServer test102 -WarningAction:SilentlyContinue
            Connect-VIServer test202 -WarningAction:SilentlyContinue
        }
   
        # Set Variables
        $catalog = Get-Catalog | ? {$_.name -match $args[0].location}
        $orgvdc = Get-Org $args[0].agency | Get-OrgVdc | ?{$_.name -match $args[0].offering}
        $vapptemplate = $catalog | Get-CIVAppTemplate | ? {$_.name -match $args[0].OS}
        $vmnetwork = Get-Org $args[0].agency | Get-OrgNetwork | ? {$_.name -match $args[0].agency -and $_.name -match $args[0].vlan -and $_.externalnetwork -match $args[0].location}

 

        # Deploy from Catalog
        Write-Host -ForegroundColor Yellow "Deploying vApp " $args[0].vmname
        Instantiate-VApp -Name $args[0].vmname -CatalogName $catalog -VAppName $vapptemplate -orgVdcName $orgvdc -Poweron:$false

 

        do {
            $Status = (Get-CIVApp $args[0].vmname).status
            sleep -Seconds 10
        }
        until ($status -match 'power')

 

        # Stop vApp
        Write-Host -ForegroundColor Yellow "Stopping vApp " $args[0].vmname
        Stop-CIVApp $args[0].vmname -Confirm:$false -ErrorAction:SilentlyContinue
        sleep -Seconds 15
       
        # Set Password on VM
        Write-Host -ForegroundColor Yellow "Setting Password on " $args[0].vmname
        $civm = Get-CIVApp $args[0].vmname | Get-CIVM
        Set-CIVMAdminPassword -vm $civm -Password Password
       
        # Update VMName and Computer Name
        Write-Host -ForegroundColor Yellow "Updating VMName and Computer Name"
        $civm = Get-CIVApp $args[0].vmname | Get-CIVM
        $civm.extensiondata.name = $args[0].vmname
        $civm.extensiondata.UpdateServerData()
        $custom = $civm.extensiondata.getguestcustomizationsection()
        $custom.ComputerName = $args[0].vmname
        $custom.AdminPassword = 'Password'
        $custom.UpdateServerData()

 

        # Set vApp/VM network settings
        Write-Host -ForegroundColor Yellow "Updating Network Settings"
        $civm = Get-CIVApp $args[0].vmname | Get-CIVM
        $vapp = Get-CIVApp $args[0].vmname
        $vappnetwork = $vapp | Get-CIVAppNetwork
        if (-not $vappnetwork) {
            New-CIVAppNetwork -VApp $vapp -ParentOrgNetwork $vmnetwork -Direct
        }
        $vappnetwork = $vapp | Get-CIVAppNetwork
        if ($args[0].ipallocationmodel -match 'manual') {
            $civm | Get-CINetworkAdapter | Set-CINetworkAdapter -Connected:$true -IPAddress $args[0].ipaddress -VAppNetwork $vappnetwork -IPAddressAllocationMode:Manual
        }
        if ($args[0].ipallocationmodel -match 'pool') {
            $civm | Get-CINetworkAdapter | Set-CINetworkAdapter -Connected:$true -VAppNetwork $vappnetwork -IPAddressAllocationMode:Pool
        }
           
        # Set CPU and Memory
        Write-Host -ForegroundColor Yellow "Setting CPU and Memory"
        Set-CIVM -vm $civm -MemoryMB ([int]$args[0].vmemory * 1024) -NumCpu $args[0].vcpu
        sleep -Seconds 5
       
        # Storage vMotion to change config files to correct names
        Write-Host -ForegroundColor Yellow "Migrating VM"
        $vm = $civm | Get-VM
        $ds = $vm | Get-Datastore
        $dscluster = $vm | Get-DatastoreCluster
        $newds = $dscluster | Get-Datastore | ? {$_.name -ne $ds.name} | sort freespacegb | select -Last 1
        Move-VM $vm -Datastore $newds -Confirm:$false
       
        # Start vAPP
        Write-Host -ForegroundColor Yellow "Starting vApp"
        Start-CIVApp $vapp -Confirm:$false

 

        # Add Disks
        Write-Host -ForegroundColor Yellow "Adding Disks"
        if ($args[0].harddisk1gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk1gb * 1024)
            sleep -seconds 8
        }
        if ($args[0].harddisk2gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk2gb * 1024)
            sleep -seconds 8
        }
        if ($args[0].harddisk3gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk3gb * 1024)
            sleep -seconds 8
        }
        if ($args[0].harddisk4gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk4gb * 1024)
            sleep -seconds 8
        }
        if ($args[0].harddisk5gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk5gb * 1024)
            sleep -seconds 8
        }
        if ($args[0].harddisk6gb) {
            Get-CIVM $args[0].vmname | New-CIHarddisk -size ([int]$args[0].harddisk6gb * 1024)
            sleep -seconds 8
        }
       
        # Update Tools
        Write-Host -ForegroundColor Yellow "Updating VMTools"
        do {
            $vm = $civm | Get-VM
            $toolsstatus = $vm.extensiondata.guest.ToolsRunningStatus
            sleep -Seconds 5
        }
        until ($toolsstatus -eq 'guesttoolsrunning')
        $vm | Update-Tools
    } -ArgumentList @($item) -Name $item.vmname
}


Viewing all articles
Browse latest Browse all 179681

Trending Articles



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