通过 PowerShell + WinRM 传递双引号 [英] Passing double quotes through PowerShell + WinRM

查看:26
本文介绍了通过 PowerShell + WinRM 传递双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在服务器上执行远程代码(MSI 安装).通过脚本传递双引号是行不通的.我尝试了下面给出的两种变体(#3 和 #4)以及输出.

I am using this code to execute remote code (MSI installs) on a server. Passing double quote through the script is just not working. I tried two variations as given below (#3 and #4) along with the outputs.

输入 #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

如果回声有效,我应该能够在我正在执行的基于运行空间的使用中合并对 MSI 命令的更改.

If the echo works, I should be able to incorporate the changes to the MSI commands in the Runspace based usage I am doing.

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

MSI setup works fine if I use the following. Notice the single quotes.

msiexec /qn /i 'C:\setups\My Software.msi'

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

But, I need to pass public properties and MSI does not like single quote in it. Trying to run the following opens up the MSI arguments dialog.

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

在服务器上的本地命令提示符下运行它可以正常工作.

Running this from the local command prompt on the server works fine.

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

推荐答案

如果你从 cmd.exe 调用这个,你必须根据 CMD 的规则对双引号进行转义.

If you're calling this from cmd.exe, you'll have to escape the double quotes according to CMD's rules.

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

输出

hello world

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

Personally, I would recommend avoiding passing the parameters in from the command line if at all possible. Maybe you could store the parameter values in a file (eg. serialized XML, JSON), and have the PowerShell script read the file?

更好的是,我建议通过 Start-Process cmdlet 对进程(例如 msiexec.exe)进行任何工作.这样,您可以在变量中建立 -ArgumentList 参数的值,然后保证它会完全按照您想要的方式传递,此外,您不会受到限制cmd.exe 的引用规则.

Better yet, I would suggest doing any work with processes (eg. msiexec.exe) through the Start-Process cmdlet. That way, you can build up the value for the -ArgumentList parameter in a variable, and then be guaranteed that it will get passed through exactly the way you want it, and furthermore, you will not be restricted to the quoting rules of cmd.exe.

考虑以下事项:

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

这篇关于通过 PowerShell + WinRM 传递双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆