Solved

Error while running start-job in powershell

Posted on 2011-09-15
8
1,895 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
  • 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 68

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
 
LVL 68

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now