PowerShell Universal environment
Windows Server 2022
PowerShell 7.2.3
(Same issue with 7.2.2)
PSU working: 2.9.3
PSU don't working: 2.10.2
(I have tried 2.10.0 and 2.10.1 also)
Script is trying to connect to:
vSphere v7
Module used:
**Installed module:**
VMWare.PowerCli 12.6
(I have also tried 12.5 same issue)
**Importing:**
VMware.VimAutomation.Core
VMware.VimAutomation.HorizonView
VMware.VimAutomation.Vds
I do have some scripts that runs against vSphere and with 2.9.3 it works perfect but with 2.10.x it did stop working. I don’t have any parameters set in the environment.ps1.
Same issue on three different servers
My conclusion is that something has changed with the runspaces for 2.10.x as directly when I change back to 2.9.3 it starts to work again.
Error message:
Apr 27, 2022 6:44 PM Connecting to xxxx...
Apr 27, 2022 6:44 PM [warning] Could not connect to xxxx
Apr 27, 2022 6:44 PM [warning] Object reference not set to an instance of an object.
Apr 27, 2022 6:44 PM Connecting to xxxx..
Apr 27, 2022 6:44 PM [warning] Could not connect to xxxx
Apr 27, 2022 6:44 PM [warning] Object reference not set to an instance of an object.
A connection to the directory on which to process the request was unavailable. This is likely a transient condition.
Apr 27, 2022 7:08 PM [warning] Could not connect to xxxx
Apr 27, 2022 7:08 PM [warning] System.NullReferenceException: Object reference not set to an instance of an object.
at VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer.HandleConnectError(List`1 connectExceptions)
at VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer.ProcessRecordErrorHandled()
at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ErrorCallbackCmdletBase.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
In my environment.ps1 I have it like this in 2.9.3
New-PSUEnvironment -Name "vSphereOnly" -Version "7.2.3" -Path "C:\Program Files\PowerShell\7\pwsh.exe" -Modules @('VMware.VimAutomation.Core', 'VMware.VimAutomation.HorizonView', 'VMware.VimAutomation.Vds') -Variables @('VISrv050', 'VMWareCred')
That don’t work with 2.10.x I get it to work like 1 of 10 times.
Then I made it like this and then I get it to work 2/5 times.
New-PSUEnvironment -Name "vSphereOnly" -Version "7.2.3" -Path "C:\Program Files\PowerShell\7\pwsh.exe" -Modules @('VMware.VimAutomation.Core', 'VMware.VimAutomation.HorizonView', 'VMware.VimAutomation.Vds') -Variables @('VISrv050', 'VMWareCred') -PersistentRunspace
Here is the script
$VMWareAuth = Get-Secret -name VMWareCred
Write-Host "--STARTING SCRIPT--`n"
Write-Host "Connecting to $($VISrv050)..."
try {
$ConnectedVIP050 = Connect-VIServer -Server $VISrv050 -Credential $VMWareAuth
Write-Host "Connected to $($VISrv050)"
[Bool]$ConnectionSuccess = $true
}
catch {
Write-Warning "Could not connect to $($VISrv050)"
Write-Warning "$($PSItem.Exception)"
[Bool]$ConnectionSuccess = $false
}
if ($ConnectionSuccess -eq $true) {
Try {
Write-Host "Collecting data from VM's from $($VISrv050)..."
$VMInfoP050 = Get-VM | Foreach-Object {
if ($null -ne ($VM = $_)) {
[PSCustomObject]@{
Name = $VM.name
Power = $VM.PowerState -as [String]
Notes = $VM.Notes -as [Array]
vCPU = $VM.NumCPU
Cores = $VM.CoresPerSocket
VMHost = $VM.VMHost.name -as [String]
Folder = $VM.Folder.Name
CreateDate = $VM.CreateDate
vLAN = ($VM | Get-NetworkAdapter).networkname
}
}
}
Write-Host "Collecting RAM information from $($VISrv050)..."
#Collecting Free Memory
$MemoryP050 = (get-vmhost | ForEach-Object { $_.MemoryTotalGB - $_.MemoryUsageGB })
$FreeMemoryP050 = $MemoryP050 | ForEach-Object -begin { $sum = 0 }-process { $sum += $_ } -end { $sum }
Write-Host "Collecting Schedule tasks for XVM* from $($VISrv050)..."
$ScheduleTasksP050 = (Get-View ScheduledTaskManager).ScheduledTask | ForEach-Object { (Get-View $_).Info } | Select-Object Name, Enabled, PrevRunTime, NextRunTime | Where-Object { $_.Name -like "Restart X*" }
Write-Host "Collecting name of vLans from $($VISrv050)..."
$CollectvLansP050 = Get-VDportgroup -name vlan*
}
catch {
Write-Warning "Could not collect data from VM's from $($VISrv050)..."
Write-Warning "$($PSItem.Exception)"
Continue
}
Write-Host "Disconnecting from $($VISrv050)..."
try {
Disconnect-VIServer $ConnectedVIP050 -Force -Confirm:$false
Write-Host "Disconnected from $($VISrv050)"
}
catch {
Write-Warning "Could not disconnect from $($VISrv050)"
Write-Warning "$($PSItem.Exception.Message)"
}
Write-Host "Trying to send VM information to cache from $($VISrv050)..."
if (-Not([string]::IsNullOrEmpty($VMInfoP050))) {
Set-PSUCache -Key "VIinfoP050" -Value $VMInfoP050 -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(36))
Write-Host "VM information was sent to cache from $($VISrv050)!"
}
else {
Write-Host "VM information is empty, didn't send it to cache from $($VISrv050)..."
}
Write-Host "Trying to send Schedule task information to cache from $($VISrv050)..."
if (-Not([string]::IsNullOrEmpty($ScheduleTasksP050))) {
Set-PSUCache -Key "ScheduleVI050" -Value $ScheduleTasksP050 -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(36))
Write-Host "Schedule task information sent to cache from $($VISrv050)!"
}
else {
Write-Host "Schedule task information is empty, didn't send it to cache from $($VISrv050)..."
}
Write-Host "Trying to send free memory information to cache from $($VISrv050)..."
if (-Not([string]::IsNullOrEmpty($FreeMemoryP050))) {
Set-PSUCache -Key "FreeMemoryV050" -Value $FreeMemoryP050 -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(36))
Write-Host "Free memory information sent to cache from $($VISrv050)!"
}
else {
Write-Host "Free memory information is empty, didn't send it to cache from $($VISrv050)..."
}
Write-Host "Trying to send vlan information to cache from $($VISrv050)..."
if (-Not([string]::IsNullOrEmpty($CollectvLansP050))) {
Set-PSUCache -Key "vlanV050" -Value $CollectvLansP050 -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(36))
Write-Host "vlan information sent to cache from $($VISrv050)!"
}
else {
Write-Host "vlan information is empty, didn't send it to cache from $($VISrv050)..."
}
}
Write-Host "Completed!`n"
I’m lost and I need help