开发者

Running a PS1 script using a batch file (.bat)

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\""


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'"


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%"


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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜