Error while running start-job in powershell


     I am running the following command to run a script on the virtual machine, when it throws the error that Invoke-VMScript is not recognized

[vSphere PowerCLI] C:\pcli> start-job -scriptblock {Invoke-VMScript -VM rhel5 -ScriptText "sh /root/script" -HostUser root -HostPassword "" -GuestUser root -GuestPassword se}

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               Job1            Running    True            localhost            Invoke-VMScript -VM rh...

[vSphere PowerCLI] C:\pcli> Get-Job

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               Job1            Failed     False           localhost            Invoke-VMScript -VM rh...

[vSphere PowerCLI] C:\pcli> Receive-Job -Id 1
Receive-Job : The term 'Invoke-VMScript' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:12
+ Receive-Job <<<<  -Id 1
    + CategoryInfo          : ObjectNotFound: (Invoke-VMScript:String) [Receive-Job], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

[vSphere PowerCLI] C:\pcli>

How do i resolve this

Who is Participating?
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
The last comment builds the bridge between the (at least) two questions implied here. But I'm not certain what you are talking about now.

You seem to mix up things, and how to achieve the goals you are targeting at. So I will first state some facts.

The  start-job   commandlet is used to create background jobs, not waiting for termination before returning control back to PowerShell. If your command spawns another process in background, there is no reason for using jobs. Further, for such simple tasks as what you showed I would not use any script, but just code it into the Invoke command in PS (but that is only a nuance for improvement).

You can start "background" tasks in cmd.exe by using the start command. Just take care that the first parameter isn't included in double quotes, as that will be treated special by start - as window title:
@echo off
start "Server shall run forever" "C:\Tools\RunMyServer.exe"

Open in new window

Conclusion: Don't use start-job here, instead just execute the remote command (Invoke-Command or Invoke-VMScript), which will start all tasks in background itself. That works as long as your scripts do not a lot of work.
Doing that removes any issue which  might arise by the asynchronous handling of background jobs in PowerShell.
Dale HarrisProfessional Services EngineerCommented:
When you try to run "Invoke-VMScript" I'm assuming it works if you're not executing a job?

Have you ensured your Snapins are properly added before attempting this?  I think the command we are looking for is:

add-pssnapin VMware.VimAutomation.Core

Or you can just run it from the Toolkit shortcut.

Good luck

Dale Harris
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I checked with the Exchange snapin, and indeed, the job is not inheriting the snapins loaded. You can check yourself with
start-job {get-pssnapin}  | wait-job | receive-job; get-job | remove-job

Open in new window

The proper way to execute for you, as hinted by DaleHarris, is hence
start-job -I -initializationScript {add-pssnapin VMware.VimAutomation.Core} -scriptblock {Invoke-VMScript -VM rhel5 -ScriptText "sh /root/script" -HostUser root -HostPassword "" -GuestUser root -GuestPassword se}

Open in new window

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

nikhil9492Author Commented:
Hi Dale and Qlemo, thanks for the reply. Firstly, sorry for  the delayed response as i was busy other work.

Since I'm new to PowerCli, i read about snap-ins before testing the solution suggested. I have followed the steps given in the PowerCli Documentation to add a snap-ins to powershell and see that VMware.VimAutomation.Core is added. redirected the o/p to a file and attached with this comment. I'm assuming that this will work.

First i tried to execute the command as posted in my first comment and throws exactly the same error as in the o/p and same error messages
After that i tried to execute the command given by Qlemo & worked fine but, the command never completes in the background. This is the status when i ran get-job

[vSphere PowerCLI] C:\pcli> Get-Job

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               Job1            Running    True            localhost            Invoke-VMScript -VM wi...

The job never finishes and the status is shown running.  Can you help me in solution for this.

Actually in my script I'm trying to get the IP addresses of VM's ( mixed Linux and Windows OS ) in a loop and execute commands inside VM. Here i have to differentiate between a Windows and Linux VM and run Invoke-Command and Invoke-VMScript respectively as i didn''t find a common command to run a remote script. I have used the parameter "OSFullName" to differentiate b/w Windows and Linux but procedure seems useless and want to run a single common command. Can anybody help me on this. ps-snapins.txt
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If the VM command does not finish executing, most probably it is expecting more input. What happens if you execute ONLY the scriptblock from PowerCLI? That is, just issue
{Invoke-VMScript -VM rhel5 -ScriptText "sh /root/script" -HostUser root -HostPassword "" -GuestUser root -GuestPassword se}

Open in new window

Regarding a single script - I don't think you can do that. The shells for *nix and Windows are too different, as are the paths to use (starting with having slash or backslash, running cmd.exe/powershell.exe or sh, ...).
nikhil9492Author Commented:
If i run the above command on Linux OS it executes the job and exits. this is since i have the way to run process in background in Linux using "&" .
-- my script --
echo "Starting Server"
iperf -s &

On Windows I'm using batch files, and since the process does not run in background the the script block ran from PowerCLI doesn't exit and is just waits there. Until i go into the Windows VM and kill that process when the script block returns and a new command can be typed.

How can we run Windows Commands In background in a script file such as Shell in Linux ?
nikhil9492Author Commented:
Earlier when i was using -initializescriptblock, I'm providing with all the necessary params and still the command runs indefinitely without exiting, whereas if i give only the command in the Script Block it executes as I mentioned in the previous comment.
nikhil9492Author Commented:
Thanks for all the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.