I am trying to run through VMs and shut them down, enable hot add then power them up.
The script works flawlessly when doing a foreach loop in serial or manually executing the commands in PowershellISE, but when I try to push the work into jobs it fails with the following error:
Exception calling "ReconfigVM" with "1" argument(s): "
Required parameter spec is missing
while parsing call information for method ReconfigVM_Task
at line 1, column 171
while parsing SOAP body
at line 1, column 64
while parsing SOAP envelope
at line 1, column 0
while parsing HTTP request for method reconfigure
on object of type vim.VirtualMachine
at line 1, column 0"
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
+ PSComputerName : localhost
The jobs all execute and power off/on the VMs as desired, but the line to set the new spec fails.
Here is the full script:
$vcenter = "vcenter"
Write-host "Datacenter - "$datacenter" and vCenter Server - "$vcenter
$site = read-host "What is the target site for Tools Upgrade today ? (example - site1) - "
connect-viserver -Server $vcenter #-WarningAction SilentlyContinue
#CSV file Name and Location
$filename = $site+"_deploy.csv"
$folder = "C:\temp\"
$path = $folder+$filename
#Get VM info
$vms = Get-vm | where-object{((($_.Name -notlike "vCLS*" -and $_.Name -notlike "stCtlVM*") -and ($_.VMHost -like "*"+$site+"*") -and ($_.PowerState -eq "PoweredOn")))}
$vms | Select-Object -Property Name,@{Name='ToolsVersion';Expression={$_.ExtensionData.Guest.ToolsVersion}},@{Name='ToolsVersionStatus';Expression={$_.ExtensionData.Guest.ToolsVersionStatus}},PowerState,CpuHotAddEnabled,MemoryHotAddEnabled | Export-Csv $path -Append #####
foreach($vm in $vms) {
$strNewVMName = $vm.Name
write-host working on $strNewVMName -ForegroundColor Green
$ScriptBlock = {
param(
[string]$Server,
[string]$SessionId,
[string]$strNewVMName
)
#Define new settings for RAM and CPU
$newSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$newSpec.CpuHotAddEnabled = $true
$newSpec.MemoryHotAddEnabled = $true
Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null
Connect-VIServer -Server $Server -Session $SessionId
$vm = Get-VM $strNewVMName
$strNewVMName = $vm.Name
$vmlog = $using:folder+$using:site+"-"+$strNewVMName+".log"
$vmlog = $folder+$site+"-"+$strNewVMName+".log"
#filter used to add timestamps to log files
filter timestamp {"$(Get-Date): $_"}
"$strNewVMName-starting process" | timestamp | out-file $vmlog -Append
shutdown-vmguest $vm.Name -Confirm:$false
Do {
$cstate = (Get-VM $strNewVMName).PowerState
if ($cstate -eq "PoweredOff") {
#wait a bit before making changes
sleep 5
#Do our memory and CPU Hot Add changes here
"$strNewVMName-Updating Settings for Hot-Add CPU/Memory" | timestamp | out-file $vmlog -Append
$vm.ExtensionData.ReconfigVM($newSpec)
write-host Updating settings -ForegroundColor Green
"$strNewVMName-Updated Settings for Hot-Add CPU/Memory" | timestamp | out-file $vmlog -Append
#Start the VM
"$strNewVMName-Powering on" | timestamp | out-file $vmlog -Append
Start-VM $vm.Name
}
else {
#If machine is not powered off... we wait
"$strNewVMName-not powered off, waiting" | timestamp | out-file $vmlog -Append
write-host $vm.Name not powered off, waiting -ForegroundColor Yellow
sleep 5
}
} while ($cstate -ne "PoweredOff")
"$strNewVMName-complete" | timestamp | out-file $vmlog -Append
Rename-Item -Path $vmlog -NewName "done-$using:site-$strNewVMName.log"
}
#Start the jobs, one for each VM
#Argument list has info for connecting to vcenter using the existing session
$sJob = @{
ScriptBlock = $ScriptBlock
ArgumentList = $global:DefaultVIServer.Name, $global:DefaultVIServer.SessionId, $strNewVMName
}
Start-Job @sjob -Name "Job-$strNewVMName"
write-host Starting job Job:$strNewVMName -ForegroundColor Yellow
}
Are you sure that
$newSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
actually creates an object?
I suspect the type is at that point in the job unknown since no PowerCLI modules were loaded yet.
Try moving those lines after the line with the Get-VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I created my owned scripts and tested working based on the post from LucD, hope this can help you as well.
https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-set-hotadd-memory-or-cpu-using-Powercli-in-Vsphere-6-7/td-p/2270295
# connect to selected vCenter
Connect-VIServer -Server $vcenter -User $creds.User -Password $creds.Password
write-host "Connecting to vCenter server $vcenter" -ForegroundColor Green
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.CpuHotAddEnabled = $true
$spec.MemoryHotAddEnabled = $true
$VMs = gc C:\Docs\scripts\input\torappwfmqv.txt
foreach($vmName in $VMs){
#Shutdown VM Guest and VM
$vm = Get-VM -Name $vmName
Write-Host "Initiating Guest Shutdown for VM + $VM" -ForegroundColor Red
if($vm.Guest.State -eq "Running"){
Shutdown-VMGuest -VM $vm -Confirm:$false
}
else{
Stop-VM -VM $vm -Confirm:$false
}
#Wait for VM to power off before executing config cmd section – in which case the config cmd will fail
while((Get-VM $vm).PowerState -ne 'PoweredOff') {
Start-Sleep -Seconds 10
}
#Enable the CPU & Memory hotplug
if(-not $vm.ExtensionData.Config.cpuHotAddEnabled -or -not $vm.ExtensionData.Config.memoryHotAddEnabled){
$vm.ExtensionData.ReconfigVM($spec)
)
}
#Power on VM after the change
Write-Host "$VM is starting" -ForegroundColor Cyan
Get-VM $vm | where {$_.status -eq "powered off"}
Start-VM $vm -Confirm:$false
}
Write-Host
Write-Host "Done" -ForegroundColor Green
# Disconnect from vCenter
Write-Host "Disconnect from vCenter $vcenter " -ForegroundColor Green
disconnect-viserver -Server $vcenter -confirm:$false
Are you sure that
$newSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
actually creates an object?
I suspect the type is at that point in the job unknown since no PowerCLI modules were loaded yet.
Try moving those lines after the line with the Get-VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, LucD. This is exactly the issue.
Script works as intended now.
I saw this and leveraged it for pieces of my script.
I'll likely add in your validation check for the settings.
Thanks for sharing it.
