如何使用Python表示Powershell“ToBase64String”功能

发布于 2025-01-17 09:41:11 字数 1796 浏览 2 评论 0 原文

因为我想调用Windows Shell运行命令并从Python获取输出,所以我尝试在Python中编码命令字符串,然后

> powershell -EncodedCommand <base64 string from Python encode>

通过错误而通过使用它来运行它。
Python中的代码看起来像是

s = '''Get-ADUser -Filter ('Surname -eq "aa" -and GivenName -eq "bb" -and Department -eq "cc"') | Select-Object -Property UserPrincipalName'''
bs = bytearray(s, 'utf-16')
base64.b64encode(bs)

,但是,当我使用PowerShell函数将我的命令字符串加密到Base64字符串

PS > $bytes = [System.Text.Encoding]::Unicode.GetBytes("Get-ADUser -Filter ('Surname -eq `"aa`" -and GivenName -eq `"bb`" -and Department -eq `"cc`"') | Select-Object -Property UserPrincipalName")
PS > [Convert]::ToBase64String($bytes)

之后时,我可以获得有效的base64字符串以成功地在普通的Windows Shell上执行此命令。

> powershell -EncodedCommand <base64 string encoded by Powershell in last two steps>

我的问题是这里有什么问题? 还是我还有其他选择来解决问题?例如,使用“ -command”的PowerShell参数直接运行? 实际上,我尝试

> powershell -Command "Get-ADUser -Filter ('Surname -eq `"aa`" -and GivenName -eq `"bb`" -and Department -eq `"cc`"') | Select-Object -Property UserPrincipalName"

Get-ADUser : Error parsing query: 'Surname -eq `aa` -and GivenName -eq `bb` -and Department -eq `cc`' Error Message: 's
yntax error' at position: '13'.
At line:1 char:1
+ Get-ADUser -Filter ('Surname -eq `aa` -and GivenName -eq `bb` -and De ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
   osoft.ActiveDirectory.Management.Commands.GetADUser

Because I want to call windows shell to run a command and to get the output from Python, I tried to encode the command string in Python, then run it by using

> powershell -EncodedCommand <base64 string from Python encode>

It will through an error because of the syntax.
The codes in Python looks like

s = '''Get-ADUser -Filter ('Surname -eq "aa" -and GivenName -eq "bb" -and Department -eq "cc"') | Select-Object -Property UserPrincipalName'''
bs = bytearray(s, 'utf-16')
base64.b64encode(bs)

But, when I use the Powershell function to encrypt my command string to a base64 string

PS > $bytes = [System.Text.Encoding]::Unicode.GetBytes("Get-ADUser -Filter ('Surname -eq `"aa`" -and GivenName -eq `"bb`" -and Department -eq `"cc`"') | Select-Object -Property UserPrincipalName")
PS > [Convert]::ToBase64String($bytes)

Afterward, I can get a valid base64 string to execute this command on normal windows shell successfully.

> powershell -EncodedCommand <base64 string encoded by Powershell in last two steps>

My question is what's the problem here?
Or do I have another option to address the problem? Like, use the PowerShell parameter of "-Command" to run directly?
Actually, I tried it with

> powershell -Command "Get-ADUser -Filter ('Surname -eq `"aa`" -and GivenName -eq `"bb`" -and Department -eq `"cc`"') | Select-Object -Property UserPrincipalName"

But it throughs an error of

Get-ADUser : Error parsing query: 'Surname -eq `aa` -and GivenName -eq `bb` -and Department -eq `cc`' Error Message: 's
yntax error' at position: '13'.
At line:1 char:1
+ Get-ADUser -Filter ('Surname -eq `aa` -and GivenName -eq `bb` -and De ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
   osoft.ActiveDirectory.Management.Commands.GetADUser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2025-01-24 09:41:11

请注意, bs = bytearray(s,'utf-16')添加

bs[0:2]
# bytearray(b'\xff\xfe')

要获得与PowerShell相同的结果,请使用

bs = bytearray(s, 'utf-16-le')

bs[0:20]
# bytearray(b'G\x00e\x00t\x00-\x00A\x00D\x00U\x00s\x00e\x00r\x00')

与PowerShell相同:

$bytes[0..19] -join ' '
# 71 0 101 0 116 0 45 0 65 0 68 0 85 0 115 0 101 0 114 0

Note that bs = bytearray(s, 'utf-16') adds Byte order mark:

bs[0:2]
# bytearray(b'\xff\xfe')

To get the same result as PowerShell, use

bs = bytearray(s, 'utf-16-le')

Then:

bs[0:20]
# bytearray(b'G\x00e\x00t\x00-\x00A\x00D\x00U\x00s\x00e\x00r\x00')

which is the same as PowerShell:

$bytes[0..19] -join ' '
# 71 0 101 0 116 0 45 0 65 0 68 0 85 0 115 0 101 0 114 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文