【问题标题】:Powershell /c with Special Characters (PsExec/EncodedCommand)带有特殊字符的 Powershell /c (PsExec/EncodedCommand)
【发布时间】:2020-11-25 03:51:51
【问题描述】:

我正在尝试使用命令行(通过 PsExec)配置 autologon.exe。但是,密码中的特殊字符会导致问题。

给定这个密码:I'vegotspe"cia"char@ters

此命令有效:

Start-Process 'C:\Autologon.exe' -ArgumentList 'username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'

但是,由于我使用的是 PsExec,因此我需要使用“/c”进行调用,所以我有以下命令。

powershell.exe /c  "Start-Process 'C:\Autologon.exe' -ArgumentList 'username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'"

此命令不会输出任何错误,但也不起作用。如果我没有双引号字符,它可以工作。知道我该如何完成这项工作吗?

我正在使用PyPsexec,所以一旦我得到上述命令;调用看起来像这样

c.run_executable("powershell.exe", arguments=''' /c Start-Process 'C:\Autologon.exe' -ArgumentList 'username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula' ''')

更新 1: 刚刚按照@lit 的建议尝试了 EncodedCommand,但我似乎遇到了同样的语法问题,因为它需要用引号括起来。

命令:

[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("Start-Process 'C:\Autologon.exe' -ArgumentList 'username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'"))

输出:

ine:1 char:140
+ ... ess 'C:\Autologon.exe' -ArgumentList 'username','hostname','"I''vegot ...
+                                                                  ~
Missing ')' in method call.
At line:1 char:140
+ ... username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'"))
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'I''vegotspe\"cia\"char@ters"','/accepteula'"' in expression or statement.
At line:1 char:184
+ ... username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'"))
+                                                                        ~
Unexpected token ')' in expression or statement.
At line:1 char:185
+ ... username','hostname','"I''vegotspe\"cia\"char@ters"','/accepteula'"))
+                                                                         ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

【问题讨论】:

  • /c 参数是-Command。目前尚不清楚psexec 是如何使用的。这是来自 SysInternals 工具包的psexec 吗?为什么使用psexecpowershell?另请参阅 powershell -EncodedCommand 参数。
  • 它与 PsExec 相同,但存在一个 python 库 (PyPsExec) 以使其更容易合并到脚本中。使用 PsExec 以便我可以远程执行此命令,而 Powershell 用于调用 Autologin。
  • 刚刚尝试了编码命令(用结果更新了原始问题)
  • 那么目标是 Windows 框吗?或者来源,我想。你有什么理由不只是做一个远程Invoke-Command

标签: python powershell cmd psexec


【解决方案1】:

我建议将密码存储在变量中。然后使用呼叫运算符&。这可能比尝试传递一个奇怪的字符串更好。

& C:\Autologon.exe /accepteula username hostname $password 

所有参数放在变量中可能会有所帮助,具体取决于/accepteula 的方式。

但是,如果您要从等效的 psexec 运行它 - 这意味着它正在使用命令解释器 - 为什么要使用 Powershell?

如果你只是在命令行上运行复杂的密码,也许它并不好——我不知道你是否测试过它。简化它可能会使生活更容易,因此它是一个具有“足够”复杂性的长字符串。或者您可以使用SET 命令为您的密码设置一个变量。

set PWD=I'vegotspe"cia"char@ters"
C:\Autologon.exe /accepteula username hostname %PWD%

它可能需要一个CMD /C 在可执行文件之前,这取决于你的 Python 是如何工作的。如果您不需要它们,添加额外的解释器(如 Powershell)是没有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 2023-03-14
    • 2012-10-06
    • 2018-11-14
    • 2012-09-15
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多