Solved

Error while running start-job in powershell

Posted on 2011-09-15
8
1,931 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 69

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 69

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 69

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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