VMware Cloud Community
PipeSmokingMari
Contributor
Contributor

troubles with Start-Job

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

$GuestName = ""

$VmView = get-VmHost -Name $TargetVmHost | get-vm | Get-View

$vmview | ForEach-Object {

$GuestName = $_.Name

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

0 Kudos
32 Replies
PipeSmokingMari
Contributor
Contributor

i installed yesterday a brand new Win7 machine, english, with all WindowsUpdates from MS, with Power CLI and i got the same behavior as in the german Win7:

the script started as Background Job stops at the line containig "$VmView = $vm | get-view" .

This is my very first Powershell script, i have no idea how to continue and solve the problem.

one of the differences between foreground/background job is that the Background job is started with "-InitializationScript {Add-PSsnapin ...."

could be this difference the cause ?

and again, after a few seconds apears a window, asking me whether to close or debug a failed PS script. When i click "View Problem Details" i get the following information:

Description:

Stopped working

Problem signature:

Problem Event Name: APPCRASH

Application Name: powershell.exe

Application Version: 6.1.7600.16385

Application Timestamp: 4a5bc7f3

Fault Module Name: KERNELBASE.dll

Fault Module Version: 6.1.7600.16385

Fault Module Timestamp: 4a5bdfe0

Exception Code: e053534f

Exception Offset: 000000000000aa7d

OS Version: 6.1.7600.2.0.0.256.48

Locale ID: 1033

When i Click "Debug Program" the background job continues to be in the state "Running" (get-Job) and after a lot of time (minutes!) it changes to

the state "Failed".

Mario

0 Kudos
admin
Immortal
Immortal

Hi again,

There are two shortcuts on 64 bit systems - which of them do you use in your attempts?

About the initialization part of the Start-Job cmdlet - try to move the Add-PSSnapin call into the script.

Also, can you try to change the row that obtains virtual machine view objects with this one:

$VmView = Get-View -ViewType 'VirtualMachine'

Vitali

0 Kudos
PipeSmokingMari
Contributor
Contributor

H@i,

I am using a PowerShell Script which starts the script i am using.

The (full) way is:

A W2K3 Server with UPS Software starts using PSEXEC from SYSINTERNALS a Batch script (*.bat) on the Server i am working on (for test purpose this is now a Win7 machine, but will be in the final environment a W2K8R2 Server).

The Batch script on my Win7 Machine starts a PowerShell Script which makes the problems we are talking about.

for my Test purpose i have a PS1 script named "Start_x.ps1" which does a

"start-job -Filepath ...\x.ps1 -Initializationscript {Add-PsSnapin vmware.vimautomation.core } -Argumentlist ...

This script (Start_x.ps1) is during my tests started by calling it from the standard windows power shell in the path "...\system32\windowspowershell\v1.0\powershell.exe"

This should be in my opinion the V2 powershell in the 64bit Version.

The script started by "start_x.ps1" containes a line like

Set-ExecutionPolicy Unrestricted

get-ExecutionPolicy

& "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"

I mean, this should correctly initialize the vmware environment !???

$VmView = Get-View -ViewType 'VirtualMachine'

produces the following error:

Get-View : The input object cannot be bound to any parameters for the command e

ither because the command does not take pipeline input or the input and its pro

perties do not match any of the parameters that take pipeline input.

At D:\EsxShutdown\x.ps1:45 char:33

+ $VmView = $vm | get-view <<<< -ViewType 'VirtualMachine'

+ CategoryInfo : InvalidArgument: (TestSrv2k3-1:PSObject) [Get-Vi

ew], ParameterBindingException

+ FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.

Cmdlets.Commands.DotNetInterop.GetVIView

Mario

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Instead of:

& "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"

you should do:

. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"

to keep the things the script does after you leave the script. This is called "dot sourcing" a PowerShell script.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
admin
Immortal
Immortal

Hi,

This changes the picture a little bit. Smiley Happy Did I understand correct that you are only executing the script remotely using psexec? Did you try to execute the script on windows 7 while you are on that windows 7 with RDP?

About the new error - currently the row in your script looks like this:

$VmView = $vm | get-view -ViewType 'VirtualMachine'

but my idea was (just for testing) to obtain all virtual machine view objects using this call:

$VmView = get-view -ViewType 'VirtualMachine'

Vitali

0 Kudos
PipeSmokingMari
Contributor
Contributor

Good morning,

i explained only the final situation. currently i am starting the script on the english win7 machine by calling the script "start_x.ps1".

i removed now -initializationscript {} from the start_x.ps1.

i changed the & "c:\.....ps1" to . "c:\....ps1"

i added after this "dot sourcing a script" a "add-pssnapin ...."

so the script looks like

. "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-VIToolkitEnvironment.ps1"

Add-PSsnapin VMware.VimAutomation.Core

Connect-ViServer -Server "*MyHostName*" -User "$Domain\$DomUsr" -Password $UsrPwd

Set-PsDebug -Trace 2

"another version of getting the view:"

$VmView = get-view -ViewType 'VirtualMachine'

"Got a view to all VMs"

$VmView | % {

Write-Output "Virtual machine" $_.Name

}

Disconnect-ViServer -Server "*MyHostName*" -Confirm:$False

no change in the behavior:

stops after the get-view

a window asking to stop or debug the power shell script

and after a lot of time an error message (got with receive-job 1 -keep)

sorry, no news

Mario

0 Kudos
PipeSmokingMari
Contributor
Contributor

thank you,

this is a great difference in theory, but no difference for my script (i dont understand why!!),

its always the GET-VIEW call which makes troubles in the background !

Mario

0 Kudos
admin
Immortal
Immortal

Hi Mario,

Can you try the script in 32 bit powershell? I'm unable to reproduce it on our test machines.

Vitali

0 Kudos
PipeSmokingMari
Contributor
Contributor

H@i,

On a first glance it seems to work with 32 bit, i will test it tomorrow more expensive.

Should the code run on 64bit ?

Is it helpful to send you the installed software und patches on the win7 machine?

Mario

0 Kudos
admin
Immortal
Immortal

Hi,

I hope it will work! Smiley Happy

And yes it could help us to research why there's a problem with 64 bit mode.

Vitali

0 Kudos
sscheller
Contributor
Contributor

Hi,

got the same problem with a win32 xp german.

$VM = Get-VM $vmList[$i].Name # Retrieve the VM from vCenter

Get-VM : 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 le

er ist, und führen Sie dann den Befehl erneut aus.

+ $VM = Get-VM &lt;&lt;&lt;&lt; $vmList[$i].Name # Retrieve the VM from vCente

r

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingValid

ationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

ation.ViCore.Cmdlets.Commands.GetVM

Why it doesn´t work?

I can´t find the answer and the error.

Thanks

0 Kudos
admin
Immortal
Immortal

Hello,

The error message says that the argument passed to the Name parameter is null or empty and my guess is that:

1. the index variable $i is out of the $vmList array boundaries

or

2. the elements in $vmList array don't have Name property which is referred.

Can you check does your script fall in one of these situations?

Vitali

0 Kudos
h00kupz
Contributor
Contributor

I have the exact same issue running my script on Windows 2008 R2 and the FIX is to run Powershell 2.0 (x86) or PowerCLI ( 32-bit).  There is another shortcut so it's easy.

Reminder you'll have to set-executionpolicy again in the 32-bit version if you don't use Restricted (Default) and have your own PS profile file to load without a cert.

ISSUE:

Running PowerCLI inside a Powershell 2.0 background job (Start-Job) crashes with a Kernel error.  The code works fine outside of a job.

CAUSE:


Some issue with 64-bit and PowerCLI

SOLUTION:

Run scripts in 32-bit shell environment.  There are shortcuts for Powershell and PowerCLI 32-bit.

VERSIONS:

Windows = Server 2008 R2

Powershell = 2.0

PowerCLI = 4.1 U1 build 332441

EXAMPLES:

WORKS in 64-bit:

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

connect-viserver vcenterserver.domain.com

get-vm somevmhostname

disconnect-viserver -Server vcenterserver.domain.com -Confirm:$false -Force:$true

FAILS in 64-bit but WORKS in 32-bit shell:

Start-Job

-ScriptBlock {

Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

connect-viserver vcenterserver.domain.com

get-vm somevmhostname

disconnect-viserver -Server vcenterserver.domain.com -Confirm:$false -Force:$true

}

-=Levon

0 Kudos