我正在尝试使用 -Command 选项执行一个包含 powershell 脚本的小字符串,但它没有被执行?如何解决这个问题?

问题描述

我正在使用 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 }”

输出为:

Command and output

但是,如果我使用命令运行脚本

pwsh -NoLogo -NonInteractive -InputFormat text -OutputFormat text -Command absolute_file_path

它按预期工作。

我应该进行哪些更改以使用 -Command 选项使其作为字符串运行?

编辑:删除 {{ 之后的双 -scriptblock 并将其替换为单个 { 没有帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)