I wrote a script to shut down my ESX Hosts.
I did some work with a Script per ESX Host, started with "Start-Job".
This script, which runs thereof in the background stops with a strange (sorry - german) error message,
but as foreground job, started as script, it runs fine.
I did some little debug and found, that the use of the function Get-View produces a syntax error (? not sure about it!)
I have the same problem with other functions.
And BTW, a Stop-VmHost in a Backround Job doesnt work as well, and in a foreground job stops the host in a manner,
that, after starting it, refuses to start VMs: operation not allowed in this state of connection ...
Has anyone the same problem, any idea?
ESX 4.1, no patches
VmWare vSphere PowerCLI on a German Windows 7, 64 Bit, all Patches from MS
Vcenter Server Build 258902
vSphere Client Build 258902
The script which starts the job:
-
$EsxHostList = @("Fqdn_Of_EsxHost","Host.domain.local")
foreach ( $EsxHost in $EsxHostList )
{
$job = start-job -FilePath "D:VmWareTest\VmTest.ps1" -InitializationScript { Add-PSsnapin VMware.VimAutomation.Core } -ArgumentList $EsxHost
}
Start-Sleep 5
-
The background job started by the script above:
-
Connect-VIServer -ServerFqdn_Of_Vcenter -User DOMAIN\USER -Password VERYSECRET
$TargetVmHost = $Args[0]
$GuestPowState = @{}
$GuestVmTools = @{}
$HostHasVcs = $False
$VmView = get-VmHost -Name $TargetVmHost | get-vm | Get-View
$vmview | ForEach-Object {
if ( ($_.Guest.ToolsStatus -eq "toolsNotRunning") -or ($_.Guest.ToolsStatus -eq "toolsNotInstalled") )
{
$GuestVmTools = 0
}
else
{
$GuestVmTools = $_.Config.Tools.Toolsversion
}
$vm = get-vm -Name $GuestName
if ( $vm.Powerstate -eq "PoweredOn" )
{
$GuestPowState = "PowOn "
}
else
{
$GuestPowState = "PowOff"
}
}
"The List with Power State and ToolsVersion :"
foreach ($vm in $GuestVmTools.Keys)
{
"Die Maschine " + $vm + " ist " + $GuestPowState + " und tools sind " + $GuestVmTools
}
Disconnect-Viserver -Server Fqdn_Of_Vcenter -Confirm:$false
There are some know issues when running on a 64-bit station.
Can you try from a 32-bit OS to verify ?
Which PowerCLI version are you running ?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello,
What is that strange error?
Did you try to analyse the output of "Get-Job -Id | Receive-Job" ? Are there any errors, where they are, etc?
Regards,
Vitali
PowerCLI team
Sorry, i have no 32bit available, must run on 64bit Win 2k8 R2 Server, Win7 is only a test environment.
I did some debugging:
The German error message:
-
Receive-Job : Vom Hintergrundvorgang wurde ein Fehler mit der folgenden Meldung
ausgegeben: .
Bei Zeile:1 Zeichen:12
+ Receive-Job <<<< 9 -keep
+ CategoryInfo : OperationStopped: (System.Manageme...emotingChil
dJob:PSRemotingChildJob) , PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure,Microsoft.PowerShell.Commands.Receive
JobCommand
-
In line 1, character 12 in my original script is nothing, an empty line containing an error ?!
The background job with a Set-PsDebug -trace 2 gives the following output:
-
DEBUG: 8+ $TargetVmHost = <<<< $Args[0]
DEBUG: ! SET $TargetVmHost = 'xxxx.xxx.xxx.xxx'.
DEBUG: 10+ $GuestPowState = <<<< @{}
DEBUG: ! SET $GuestPowState = 'System.Collections.Hashtable'.
DEBUG: 11+ $GuestVmTools = <<<< @{}
DEBUG: ! SET $GuestVmTools = 'System.Collections.Hashtable'.
DEBUG: 12+ $HostHasVcs = <<<< $False
DEBUG: ! SET $HostHasVcs = 'False'.
DEBUG: 13+ $GuestName = <<<< ""
DEBUG: ! SET $GuestName = ''.
DEBUG: 16+ $VmView = <<<< get-VmHost -Name $TargetVmHost | get-vm |
Get-View
DEBUG: ! SET $VmView = ''.
DEBUG: 17+ <<<< $vmview | ForEach-Object
DEBUG: ! CALL scriptblock.
DEBUG: 1+ & { <<<< Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: 1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.
<<<< PSMessageDetails }
Das Argument für den Parameter "Name" kann nicht überprüft werden. Das Argument
ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, u
nd führen Sie dann den Befehl erneut aus.
+ CategoryInfo : InvalidData: ( , ParameterBindingValid
ationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVM
DEBUG: 32+ if <<<< ( $vm.Powerstate -eq "PoweredOn" )
DEBUG: 38+ $GuestPowState = <<<< "PowOff"
DEBUG: 45+ "The List with Power State and ToolsVersion :" <<<<
The List with Power State and ToolsVersion :
DEBUG: 47+ foreach <<<< ($vm in $GuestVmTools.Keys)
DEBUG: 50+ "Die Maschine " + <<<< $vm + " ist " + $GuestPowState
+ " und tools sind " + $GuestVmTools
Die Maschine ist PowOff und tools sind
DEBUG: 57+ <<<< Disconnect-Viserver -Server vmuvcs1.vu-wien.ac.at
-Confirm:$false
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI
-
referencing my script:
connect-viserver ....
set-psdebug -trace 2
$Targetvmhost=
$Guestpowstate=
i changed the background job a little bit to show, that Get-View makes troubles.
it seems, that Get-View needs a parameter called VIObject, which is empty -
in the case of a background job ! Why ?
Connect-VIServer -Server xxxxxx -User uuuuuuu -Password ppppppp
set-psdebug -trace 2
$TargetVmHost = $Args[0]
$GuestPowState = @{}
$GuestVmTools = @{}
$HostHasVcs = $False
$VmHost = get-VmHost -Name $TargetVmHost
$Vm = $VmHost | get-vm
$VmView = $vm | Get-View
^^^^^^^^^^
$vmview | ForEach-Object {
"The Name1 of the Guest is :" + $_.Name + "."
DEBUG: 8+ $TargetVmHost = <<<< $Args[0]
DEBUG: ! SET $TargetVmHost = 'vmuvs20.vu-wien.ac.at'.
DEBUG: 10+ $GuestPowState = <<<< @{}
DEBUG: ! SET $GuestPowState = 'System.Collections.Hashtable'.
DEBUG: 11+ $GuestVmTools = <<<< @{}
DEBUG: ! SET $GuestVmTools = 'System.Collections.Hashtable'.
DEBUG: 12+ $HostHasVcs = <<<< $False
DEBUG: ! SET $HostHasVcs = 'False'.
DEBUG: 13+ $GuestName = <<<< ""
DEBUG: ! SET $GuestName = ''.
DEBUG: 15+ $VmHost = <<<< get-VmHost -Name $TargetVmHost
DEBUG: ! SET $VmHost = 'vmuvs20.vu-wien.ac.at'.
DEBUG: 16+ $Vm = <<<< $VmHost | get-vm
DEBUG: ! SET $Vm = ''.
DEBUG: 17+ $VmView = <<<< $vm | Get-View
DEBUG: 1+ <<<< & { Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: ! CALL scriptblock.
DEBUG: 1+ & { <<<< Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: 1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.
<<<< PSMessageDetails }
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!vvvvvvvvv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Das Argument für den Parameter "VIObject" kann nicht überprüft werden. Das Argu
ment ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer is
t, und führen Sie dann den Befehl erneut aus.
+ CategoryInfo : InvalidData: ( , ParameterBindingVal
idationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
DEBUG: ! SET $VmView = ''.
DEBUG: 19+ <<<< $vmview | ForEach-Object
DEBUG: ! CALL scriptblock.
DEBUG: 1+ & { <<<< Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: 1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.
<<<< PSMessageDetails }
Das Argument für den Parameter "Name" kann nicht überprüft werden. Das Argument
ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer ist, u
nd führen Sie dann den Befehl erneut aus.
+ CategoryInfo : InvalidData: ( , ParameterBindingValid
ationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVM
DEBUG: 37+ if <<<< ( $vm.Powerstate -eq "PoweredOn" )
DEBUG: 43+ $GuestPowState = <<<< "PowOff"
DEBUG: 50+ "The List with Power State and ToolsVersion :" <<<<
The List with Power State and ToolsVersion :
DEBUG: 52+ foreach <<<< ($vm in $GuestVmTools.Keys)
DEBUG: 55+ "Die Maschine " + <<<< $vm + " ist " + $GuestPowState
+ " und tools sind " + $GuestVmTools
Die Maschine ist PowOff und tools sind
DEBUG: 62+ <<<< Disconnect-Viserver -Server vmuvcs1.vu-wien.ac.at
-Confirm:$false
Hi again,
LucD already asked you - which PowerCLI version do you use? The last PowerCLI 4.1 version?
I've just tied this simple script and it works OK on Windows 7 64 bit , english version. PowerCLI 4.1 version:
$hostname = $args[0]
connect-viserver -server MyServerName
$vmhost = Get-VMHost -name $hostname
$vmView = $vmhost | get-vm | get-view
$vmView | % {
Write-Output $_.Name
}
disconnect-viserver * -confirm:$false
Vitali
How can i get the CLI version ?
it should be 4.1 because i got all software from the 4.1 download page.
Does this script work as background job? did you try it ?
mario
Hi,
the commandlet is: Get-PowerCLIVersion
And, yes it works as background job. The call is similar to yours:
Start-Job -FilePath .\doJob.ps1 -InitializationScript { Add-PSSnapin 'VMware.VimAutomation.Core' } -ArgumentList $oneHostName
Vitali
it is 4.1 build 264274
mario
thanx, i will try it with an english version,
with my german version - no chance
mario
Hi,
your version is the last one.
Is it possible the host that you are using while testing to be have no machines?
$vm = $null
$vm | Get-View
Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
Which is the same message as the German (according to the google translate).
Check your input parameter
Vitali
yes, it is possible in that way:
it has only one W2K3 VM without tools installed
for testing purpose ...
Mario
but i run my script against all ESX hosts,
and at least two of my four Hosts have running machines with tools installed,
but (excuse me) old tools (from Vmware 3.5 Upd2) installed.
At least one machine (the VCS) has actual tools!
Mario
Hi mario,
This portion of the log means that had failed on the row "$vmView = $vm | Get-View".
Which means that the $vm variable is null.
$VmView = <<<< $vm | Get-View
DEBUG: 1+ <<<< & { Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: ! CALL scriptblock.
DEBUG: 1+ & { <<<< Set-StrictMode -Version 1;
$this.Exception.InnerException.PSMessageDetails }
DEBUG: 1+ & { Set-StrictMode -Version 1; $this.Exception.InnerException.
<<<< PSMessageDetails }
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!vvvvvvvvv!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Das Argument für den Parameter "VIObject" kann nicht überprüft werden. Das Argu
ment ist NULL oder leer. Geben Sie ein Argument an, das nicht NULL oder leer is
t, und führen Sie dann den Befehl erneut aus.
Vitali
i think, we found it:
i have now on the machine which i am asking for the view
only one VM
which is correct installed
has correct tools
and a correct IP address
previous i had
this one machine above AND
a machine with correct tools installed,
configured for DHCP, but not receiving an IP address (because no MAC registered at DHCP Server)
and thereof having a MS private address 169.254.68.xxx
and this did not work.
I will retry the erroneous configuration
Mario
i cant reproduce the correct running version ....
there are two problems:
1) one problem is an empty ESX Host which produces an empty object in the script.
Empty parameter ViObject
2) an other thing is the difference foreground - background job and probably the german OS
i will try it on an english version
Mario
Hi Mario,
did you try the simpler script that just prints VM names? In background job...
Vitali
thats the current code without connect/disconnect:
set-psdebug -trace 2
$TargetVmHost = $Args[0]
$VmHost = get-VmHost -Name $TargetVmHost
$vm = $VmHost | get-vm
$VmView = $vm | get-view
$vmview | % {
Write-Output $_.Name
}
i forgot to tell you that a short time after starting the job a window appears asking to stop or debug the script.
Mario
Hi again,
I didn't understand , does the script work or not?
I have no prompts with or without Set-PSDebug...
Vitali
The current state is:
(a)
script started via START-JOB against a host without any vm: Error complaining an empty ViObject
(b)
script started in the foreground against a host without any vm: Error complaining an empty ViObject
(c)
script started via START-JOB against a host with only one fully and correct configured VM: Error Window asking whether to close or debug a failing PS script, background job continues to be in the State "Running" (Get-Job), after a looot of time changes to state "Failed", Receive-Job 1 displays the following text:
-
vmuvcs1.DOMAIN.COM 443 user\domain
DEBUG: 8+ $TargetVmHost = <<<< $Args[0]
DEBUG: ! SET $TargetVmHost = 'vmuvs22.DOMAIN.COM'.
DEBUG: 12+ $VmHost = <<<< get-VmHost -Name $TargetVmHost
DEBUG: ! SET $VmHost = 'vmuvs22.DOMAIN.COM'.
DEBUG: 13+ $vm = <<<< $VmHost | get-vm
DEBUG: ! SET $vm = 'EdvzSrv2'.
DEBUG: 15+ <<<< Write-Output "111" + $vm
111
+
Receive-Job : Vom Hintergrundvorgang wurde ein Fehler mit der folgenden Meldung
ausgegeben: .
Bei Zeile:1 Zeichen:12
+ Receive-Job <<<< 1 -keep
+ CategoryInfo : OperationStopped: (System.Manageme...emotingChil
dJob:PSRemotingChildJob) , PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure,Microsoft.PowerShell.Commands.Receive
JobCommand
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI
-
the relevant part of PS code:
-
set-psdebug -trace 2
$TargetVmHost = $Args[0]
$VmHost = get-VmHost -Name $TargetVmHost
$vm = $VmHost | get-vm
Write-Output "111" + $vm
$VmView = $vm | get-view
Write-Output "222" + $VmView
$vmview | % {
Write-Output $_.Name
}
-
in my opinion the error is in the neighborhood of "Write-Output "111" + $vm"
because the code displays a "111", a "+" and then fails .... why ? or any other cause.
(d)
the script started in the foreground against a host with only one fully and correct configured VM:
runs correct, without any errors.
i am for 10 days in vacation, i will post when i have an english Windows 7
Mario