Automation

 View Only
  • 1.  Executing cmdlets from a bat file

    Posted May 06, 2008 05:37 PM

    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.



  • 2.  RE: Executing cmdlets from a bat file

    Posted May 06, 2008 07:04 PM

    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. :smileyhappy: 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)



  • 3.  RE: Executing cmdlets from a bat file

    Posted May 06, 2008 08:14 PM

    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!



  • 4.  RE: Executing cmdlets from a bat file
    Best Answer

    Posted May 06, 2008 07:05 PM

    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



  • 5.  RE: Executing cmdlets from a bat file

    Posted May 06, 2008 07:27 PM

    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)



  • 6.  RE: Executing cmdlets from a bat file

    Posted May 07, 2008 02:37 AM

    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.



  • 7.  RE: Executing cmdlets from a bat file

    Posted May 07, 2008 03:24 AM

    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



  • 8.  RE: Executing cmdlets from a bat file

    Posted May 07, 2008 04:55 PM

    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)