加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

通过PowerShell WinRM传递双引号

发布时间:2020-12-14 04:38:32 所属栏目:Windows 来源:网络整理
导读:我使用此 code在服务器上执行远程代码(MSI安装).通过脚本传递双引号是行不通的.我尝试了下面给出的两个变体(#3和#4)以及输出. 输入#1(在命令中测试双引号的简单情况) powershell.exe -inputformat none -File client.ps1 -target 1.2.3.4 -port 5985 -passwo
我使用此 code在服务器上执行远程代码(MSI安装).通过脚本传递双引号是行不通的.我尝试了下面给出的两个变体(#3和#4)以及输出.

输入#1(在命令中测试双引号的简单情况)

powershell.exe -inputformat none -File client.ps1 -target 1.2.3.4 -port 5985 -password "pass" -username "user" -command "echo hello"

输出(工程)

hello

输入#2(可以理解,这不起作用)

powershell.exe -inputformat none -File client.ps1 -target 1.2.3.4 -port 5985 -password "pass" -username "user" -command "echo hello world"

产量

hello
world

输入#3

powershell.exe -inputformat none -File client.ps1 -target 1.2.3.4 -port 5985 -password "pass" -username "user" -command "echo `"hello world`""

输出(另一个词怎么了?)

hello

输入#4

powershell.exe -inputformat none -File client.ps1 -target 1.2.3.4 -port 5985 -password "pass" -username "user" -command @'
>> echo "hello world"
>> '@
>>

输出(再次,缺少第二个字)

hello

如果echo工作,我应该能够在我正在进行的基于运行空间的使用中将更改合并到MSI命令中.

如果我使用以下内容,MSI设置正常.注意单引号.

msiexec /qn /i 'C:setupsMy Software.msi'

但是,我需要传递公共属性,MSI不喜欢单引号.尝试运行以下命令将打开MSI参数对话框.

msiexec /qn /i 'C:setupsMy Software.msi' MYPROP='My Value'

从服务器上的本地命令提示符运行此工作正常.

msiexec /qn /i "C:setupsMy Software.msi" MYPROP="My Value"

解决方法

如果你是从cmd.exe调用它,你必须根据CMD的规则逃避双引号.

powershell.exe -command "echo "hello world""

产量

hello world

就个人而言,我建议尽可能避免从命令行传递参数.也许你可以将参数值存储在一个文件中(例如序列化的XML,JSON),并让PowerShell脚本读取文件?

更好的是,我建议通过Start-Process cmdlet对进程(例如msiexec.exe)进行任何操作.这样,您可以在变量中为-ArgumentList参数构建值,然后保证它将以您希望的方式完全传递,此外,您不会受限于cmd的引用规则.可执行程序.

考虑以下:

$ArgumentList = '/package "c:setupsMy Software.msi" /passive /norestart /l*v "{0}tempInstall My Software.log" MYPROP="My Value With Spaces"' -f $env:windir;
Start-Process -FilePath msiexec.exe -ArgumentList $ArgumentList;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读