VMware Cloud Community
casmered
Contributor
Contributor
Jump to solution

Executing cmdlets from a bat file

I am trying to create a bat file that opens up the VMware PowerShell prompt and executes a couple of cmdlets. But what I have so far opens up the VMware PS prompt, but then never executes the following cmdlets. Is there any way to force the execution of the cmdlets? I am sure there is, but at a loss for getting it to happen. Any help would be appreciated.

Here is what I have in a bat file as of now. First line stolen from shortcut VMware puts on desktop. I know the second two lines work as I have tested independently from the bat file. The real issue is getting hung after the first line and the second two lines never executing.

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

And then after that opens the appropriate prompt, execute the following:

Get-VIServer xxx -User xxx -Password xxx

Add-VMHost -Name xxx -Location (Get-Datacenter -Name xxx) -UserName root -Password xxx

Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
kjb007
Immortal
Immortal
Jump to solution

PowerShell -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -command .\test.ps1

The init.ps1 file aliases get-viserver to get-vc, so check that as well, and / or put it into your sript.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB

View solution in original post

Reply
0 Kudos
7 Replies
halr9000
Commander
Commander
Jump to solution

I am trying to create a bat file that opens up the VMware PowerShell prompt and executes a couple of cmdlets. But what I have so far opens up the VMware PS prompt, but then never executes the following cmdlets.

Totally no offense intended when I say this but it's funny to me. Smiley Happy Your approach is "bass-ackwards".

This topic comes up fairly often on the powershell newsgroup. The best way to put together powershell commands is in a powershell script file. the best way to execute that script is by using the script engine--namely powershell.exe. You can provide to powershell.exe several command-line options, one of which is "-command". It takes a string or actual script block of powershell code as input, which is kinda neat. You can run powershell.exe /? at a DOS prompt.

Try something like powershell.exe -command "& c:\path\to\script.ps1"

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
kjb007
Immortal
Immortal
Jump to solution

PowerShell -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -command .\test.ps1

The init.ps1 file aliases get-viserver to get-vc, so check that as well, and / or put it into your sript.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

PowerShell -PSConsoleFile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -command .\test.ps1

Good call on the PSC1 file. For those that don't know, this file contains instructions to add the VMware snapin so that the cmdlets are available.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
casmered
Contributor
Contributor
Jump to solution

No offense taken. I am not truly running this from a batch file (well, I kind of am), but it was easiest to explain it this way. I am using the Altiris Deployment Solution to manage my servers and VMs and the best way to describe how to make the calls from within Altiris Deployment Solution is to describe it as executing from a batch file. I knew that if I could execute them from a batch file then I could set it up to run inside of DS.

kjb, twice this week. Very helpful. I have it running now. Thanks!

Reply
0 Kudos
gpeck29
Enthusiast
Enthusiast
Jump to solution

Along this same line, I'd like to know how to open up powershell to run a script, but I need to load different cmdlets...

i.e. I want to write a script that first, contacts ActiveDirectory using the Quest AD cmdlet, does some investigation of an OU and restarts VM's using the VMware cmdlets...each is pretty easy, but I need to learn how to load differing cmdlets into the same environment.

Reply
0 Kudos
kjb007
Immortal
Immortal
Jump to solution

That's what the PSConsoleFile option is doing. It is "importing the cmdlets". Check the file and see the syntax, you should be able to do the same to include the Quest stuff.

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

That's what the PSConsoleFile option is doing. It is "importing the cmdlets". Check the file and see the syntax, you should be able to do the same to include the Quest stuff.

No need to hand-edit, although that would certainly work. Do a "get-help export-console" and you'll see.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos