VMware Cloud Community
BenBrazil
Enthusiast
Enthusiast
Jump to solution

Lauch PowerCLI script from external application

Hi,

I am working with an external scheduling application. It allows launching command line scripts. I have written a ps1 script which creates a new VM Guest. I would like to be able to call this script - CreateVM.ps1 from the scheduling application however I am not sure of the command line to use. Launching Powershell is not a problem, however this will lauch the normal Powershell which does not give me access to the PowerCLI commands. Does anyone know how to launch a PowerCLI ps1 script from a DOS prompt of from a BAT file?

Many thanks,

Ben

0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

You can either add the snapin on the first line of the script:

Add-PSSnapin VMware.VimAutomation.Core

or use the following to run your script:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" " & "C:\CreateVM.ps1

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
10 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

You can either add the snapin on the first line of the script:

Add-PSSnapin VMware.VimAutomation.Core

or use the following to run your script:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" " & "C:\CreateVM.ps1

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
BenBrazil
Enthusiast
Enthusiast
Jump to solution

Hi Alan,

thanks for the swift answer. I've chosen to use Add-PSSnapin VMware.VimAutomation.Core

Regards,

Ben

0 Kudos
rhoworth101
Contributor
Contributor
Jump to solution

many thanks for the command line info - of course, now that Vmware has rebranded PowerCLI as part of vSphere the command line should read:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" " & "C:\somescript.ps1

Regards, Roger

http://[www.thehypervisor.com|http://www.thehypervisor.com]

0 Kudos
haimram
Contributor
Contributor
Jump to solution

Hi alan

I am sorry but i am new with PowerCLI

I am trying and trying again and again the 2 ways you suggested and nothing happens, i am getting an error that "Add-PSSnapin VMware.VimAutomation.Core" is not familiar command.i want to run the script i wrote for reverting a snapshot and it's working great when i run it from sphere powerCLI manually, but i want to run it with batch file, i have tried the second way

"C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile

"C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" " & "C:\CreateVM.ps1

"VIToolkitForWindows" this folder does not exist on my system so i switched it with "vSphere PowerCLI" and i get an error: see attached file

I dont know what to do please advise me what next?!

Thank you

Haim

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Haim,

Running your script from a batch file should work with the next commandline:

%SystemRoot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command "&{c:\CreateVM.ps1}"

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
haimram
Contributor
Contributor
Jump to solution

Thank you Robert

I tried your way and this is what i get:see attached file

I don't understand why?!

Haim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-VIServer cmdlet is an old alias that is not present in PowerCLI 4.1.

Replace it with a Connect-VIServer cmdlet.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
haimram
Contributor
Contributor
Jump to solution

Hello LucD

I use PowerCLI 4.0 but for some reason your suggestion work for me, thank you very much i really appreciate your help

Thanks

Haim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-VIServer alias was already gone in PowerCLI 4

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dwcrist
Enthusiast
Enthusiast
Jump to solution

I am also new to PowerCLI.  I've written some commands that work fine from the PowerCLI window interactively, but not from a .cmd file.  This thread has helped to point me in the right direction.

Rather than using the -command switch, mine worked when I used the -file switch.

I found this by going to "C\Windows\System32\WindowsPowershell\v1.0 and running powershell.exe /? > textfile.txt

This produced a text file with all of the switches.

Hope this helps.  Thanks again for all of the contributors.  I've looked at some of the books available, but couldn't find any that dealt with the basics like getting stuff to work from the command line.  They seem to all dive right into scripts and functions.

0 Kudos