【发布时间】:2021-07-17 10:31:30
【问题描述】:
我正在使用 PowerShell Core 来执行此操作。 命令:
pwsh -NoLogo -NonInteractive -InputFormat text -OutputFormat text -Command “& { $Password = ConvertTo-SecureString 'pswd' -AsPlainText -Force;$skipCN = New-PSSessionOption -SkipCNCheck -SkipCACheck;$Credential = New-Object System.Management.Automation.PsCredential 'Administrator’ , $Password;Invoke-Command -ComputerName hostname -Authentication Basic -Credential $remote_credential -ScriptBlock {{ Get-WmiObject -Class Win32_PerfRawData_PerfOS_Memory | Format-List * }} -useSSL -SessionOption $skipCN }”
输出为:
但是,如果我使用命令运行脚本
pwsh -NoLogo -NonInteractive -InputFormat text -OutputFormat text -Command absolute_file_path
它按预期工作。
我应该进行哪些更改以使其使用 -Command 选项作为字符串运行?
编辑:删除 -scriptblock 之后的双 {{ 并用单个 { 替换它没有帮助。
【问题讨论】:
-
一方面,停止使用大引号(尤其是不要像
'Administrator’那样混合使用它们)而是直引号。这些所谓的“智能引用”在 Word 文档中可能看起来不错,但在代码中可能会做一些奇怪的事情。另外,我建议您不要将所有内容都塞进一行代码中,这会使调试变得非常困难。 -
@Theo 我尝试使用直引号,但没有成功,整个代码作为字符串传递,每一行用分号 ' 分隔; ' .
标签: powershell powershell-remoting winrm powershell-core