5

Currently the following is my path for launching the VMware vSphere PowerCLI command prompt. I wish to run my sample.ps1 script automatically using a batch file. How can I incoporate sample.ps1 into this path and create a batch file?

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dwyane
  • 127
  • 2
  • 4
  • 8
  • What are you asking? You can always call a powershell script from a cmd script simply by running powershell.exe, as you have done above. What else are you trying to do? – Ryan Bemrose Jul 07 '11 at 03:28
  • -psc = -PSConsoleFile -> load console file to import automaticaly his SNAPIN. -noe = -NoExit -> in order to keep powershell session. and then the script he wants to launch. – JPBlanc Jul 07 '11 at 03:31
  • Isn't this a duplicate of ... http://stackoverflow.com/questions/6037146/how-to-execute-powershell-commands-from-a-batch-file – SteveC Aug 29 '13 at 09:05

4 Answers4

4

If you are working with PowerShell 2.0, you can use the -file parameter of PowerShell.exe

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"

If you are working with PowerShell 1.0, you can use -command parameter this way

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1
echo off

Title,Report Script &color 9e
for /f "usebackq delims=$" %%a in (`cd`) do (
  set SCRIPTDIR=%%a
)

(Set ScriptFile=%SCRIPTDIR%\Report.ps1)

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"
Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
Farhad
  • 11
  • 1
0

You can use this to launch arbitrary .ps1 scripts via .bat files by calling the bat file like your ps1. Then extract the name of file in batch and call powershell with it.

For a ready to use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237

Jonas Gröger
  • 1,558
  • 2
  • 21
  • 35
0

I saw this code in another page, I test it in a W2012 R2 and it runs.

I hope it work:

C:\>powershell "C:\>1\file.ps1"
Seb33300
  • 7,464
  • 2
  • 40
  • 57
Arthur
  • 1