2011-01-27 100 views
1

我一直在尝试从批处理文件启动简单的powershell脚本。在线查看后,建议是使用Set-ExecutionPolicy设置策略。Powershell脚本不从批处理文件发射

我已经完成了这项工作,并使用Get-ExecutionPolicy策略已被更改。

但是,运行此批处理文件会导致“打开方式”对话框而不是执行脚本。

批次:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&'./script.psl' 

PowerShell的:

Write-Host "This is a message" 

我已经在Windows 7和Server 2008 R2中采样。两者都有相同的结果。我错过了什么?

回答

1

要从命令行运行脚本文件,请使用powershell.exe的-file参数。

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file './script.psl' 
+1

我得到一个错误信息这样做: 处理 - 文件'./script.psl'失败,因为该文件不具有”名为.ps1' 延伸。指定一个有效的PowerShell脚本文件名,然后重试。 – Tezza 2011-01-27 21:17:35

0

若要从* .cmd文件脚本文件,使用powershell.exe的文件参数和双引号:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file "./script.ps1" 

当你只需要使用一张在批处理文件中引用你可以预期PowerShell的错误,如:

处理 - 文件'./build.ps1'失败,因为文件没有 一”的.ps1' 延伸。指定一个有效的Windows PowerShell脚本文件 名称,然后重试。