nikhil9492
asked on
Error while running start-job in powershell
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
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
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
The proper way to execute for you, as hinted by DaleHarris, is hencestart-job -I -initializationScript {add-pssnapin VMware.VimAutomation.Core} -scriptblock {Invoke-VMScript -VM rhel5 -ScriptText "sh /root/script" -HostUser root -HostPassword "" -GuestUser root -GuestPassword se}
ASKER
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
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
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}
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, ...).
ASKER
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 ?
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 ?
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the help.
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