Hi,
I've been running into an issue attempting to retrieve information from multiple Virtual Center servers using the PowerCLI in a multi-threaded environment using C#. Essentially, in one test scenario, I have one PowerCLI script that is used to retrieve raw disk information from VMs, one thread per Virtual Center, and multiple Virtual Centers.
When using a single thread to retrieve such information, everything works fine. Using multi-threading generates this error:
Message: VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidState: You have modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. Please reset them to $null and reconnect to the vSphere server. at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.SessionStateManager.SyncDefaultVISeverPSVariables(SessionState sessionState, PSHost host) at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.ErrorCallbackCmdletBase.BeginProcessing() at System.Management.Automation.Cmdlet.DoBeginProcessing() at System.Management.Automation.CommandProcessorBase.DoBegin()
The PowerCLI script that retrieves raw disk info looks like this:
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin "VMware.VimAutomation.Core"
}
$global:DefaultVIServer = $null
$global:DefaultVIServers = $null
$server = connect-viserver -Server "ServerName" -User "UserName" -Password "Password" #-NotDefault -Verbose:$true -Debug:$true
$global:DefaultVIServer
$global:DefaultVIServers
$VMs = Get-VM -Server $server
$count= 0
$Disks = @()
foreach($VM in $VMs)
{
if (++$count % 10 -eq 0) {Write-Output "Done $count VMs"}
if($VM -ne $null)
{
foreach ($HDD in get-HardDisk -VM $VM -Server $server)
{
if ($HDD.DiskType -eq "rawPhysical" -or $HDD.DiskType -eq "rawVirtual")
{
if ($Disks -notcontains $HDD) {$Disks += $HDD}
}
}
}
}
return $Disks
As a stand-alone script, the code above works just fine. However, if the " -NotDefault -Verbose:$true -Debug:$true" is enabled in the call to connect-viserver, the line that reads "$VMs = Get-VM -Server $server" throws the same exception:
You have modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. Please reset
them to $null and reconnect to the vSphere server.
I have attempted to resolve the issue by doing exactly what the exception says, resetting the $global:DefaultVIServer and $global:DefaultVIServers variables to null and reconnecting to the vSphere server, however, the error persists. I'm thinking that if I can resolve this issue when running the code as a standalone script, and using the -NotDefault option in connect-viserver cmdlet, then I will be able to run the script by invoking it from C# and using multi-threading.
Any suggestions for how I can resolve this issue?
Thanks,
Juan