Solved

Error while running start-job in powershell

Posted on 2011-09-15
8
1,993 Views
Last Modified: 2012-05-12
HI,

     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

Nikhil
0
Comment
Question by:nikhil9492
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36549073
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
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 36575548
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


0
 

Author Comment

by:nikhil9492
ID: 36714975
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
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 70

Expert Comment

by:Qlemo
ID: 36716471
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, ...).
0
 

Author Comment

by:nikhil9492
ID: 36716659
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 "&" .
example
-- 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 ?
0
 

Author Comment

by:nikhil9492
ID: 36716685
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.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 250 total points
ID: 36716849
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.
0
 

Author Closing Comment

by:nikhil9492
ID: 36978982
Thanks for all the help.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question