简体   繁体   English

PSExec Pass参数带有Powershell空格

[英]PSExec Pass argument with spaces for powershell

With PSexec tool, I am invoking a powershell session on remote computer and executing a PS1 script. 使用PSexec工具,我正在远程计算机上调用powershell会话并执行PS1脚本。 I need to pass argument to this powershell script file. 我需要将参数传递给此powershell脚本文件。 It works fine if the argument does not contain space. 如果参数不包含空格,它将很好地工作。 If the argument does contain space, everything after space is trimmed. 如果参数确实包含空格,则将修剪空格之后的所有内容。

Below is my command: 下面是我的命令:

$script="<PATH>"
$args = "%ProjectSourcePath%"

cmd /c %psexecpath% \\<REMOTE SERVER>   -u <UNAME> -p <PASSWORD> -h  "c:\windows\system32\windowspowershell\v1.0\powershell.exe" $script $args

where "%ProjectSourcePath%" is the environment variable. 其中"%ProjectSourcePath%"是环境变量。

Background: I am using this command in Jenkins Automated Build tool. 背景:我正在Jenkins自动构建工具中使用此命令。 And have added this script in Windows Powershell script block. 并已在Windows Powershell脚本块中添加了此脚本。

First thing first, in order to run psexec straight from the command line, add the sysinternals (or wherever your psexec location) folder to the PATH with this cmd 首先,为了直接从命令行运行psexec,请使用此cmd将sysinternals(或任何psexec位置)文件夹添加到PATH中

SET PATH=%PATH%;[FOLDER]

Next up, running the command line... 接下来,运行命令行...

I've created a really simple script creating file with the nameof the argument im passing 我已经创建了一个非常简单的脚本创建文件,文件名是im传递

param (
    [string]$name = "defaultname"
 )

$array = "string"

$array | out-file "C:\Temp\Script Location\$name.txt"

When running it from the remote server im doing the following: 从远程服务器运行它时,请执行以下操作:

psexec \\build -h "C:\windows\system32\windowspowershell\v1.0\powershell.exe" "-File" "C:\temp\script location\script.ps1" "argument with space"

ending up with file named "argument with space.txt" 最后以名为“带有space.txt的参数”的文件结尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM