需要使用psexec取消阻止远程ps脚本运行

时间:2015-08-07 06:10:51

标签: powershell tfs psexec

作为使用TFS自动构建和部署的一部分,我需要在目标服务器上执行powershell脚本。以下是TFS Build Agent PreBuild步骤在(pre-build.ps1)脚本中在构建服务器上运行的(为了清晰起见而简化)命令...

C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1 -name AppPool-DEV -usr User -pw pass

如果我在命令窗口中的WSRMO632WEB框上运行命令的powershell部分,我会收到警告......

Security warning
Run only scripts that you trust. While scripts from the internet can be useful, 
this script can potentially harm your computer. If you trust this script, 
use the Unblock-File cmdlet to allow the script to run without this warning message. 
Do you want to run \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1?
[D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"):

如果我选择R,脚本将运行并正确执行。

我的问题是我无法正确使用Unblock-File cmdlet。

我目前正在考虑我将不得不使用多个psexec命令,一个用于从构建服务器复制文件,一个用于解除阻塞,第三个用于最终运行它。

当然它应该比这更容易,但我找不到合适的例子,也无法正确理解语法。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Powershell -command首先执行Unblock-File,然后将其作为脚本运行。

C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe "-command \"$file='\\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1'; $file; Unblock-File $file; & $file\"" -name AppPool-DEV -usr User -pw pass

引号是必要的,以便将完整的命令字符串传递给Powershell。根据需要添加反斜杠以自行逃避。

更新:您也可以尝试将所需命令输入标准输入。

echo r | C:\Builds\<snip>\psexec.exe -accepteula -s -i \\WSRMO632WEB powershell.exe \\TFS-BAGENT-POC\<snip>\PreBuild-AppPool.ps1 -name AppPool-DEV -usr User -pw pass

这样Powershell会运行,得到&#34; R&#34;为&#34;运行一次&#34;并运行脚本,而不对脚本或调用命令进行任何更改。