一行中的多个命令-Powershell,Escape“

时间:2018-11-01 08:04:36

标签: powershell-v4.0

我试图通过以下命令通过命令提示符在一行中运行多个powershell命令-

powershell-命令“ $ auth = [System.Convert] :: ToBase64String([System.Text.Encoding] :: UTF8.GetBytes('admin'+':'+'admin'));调用WebRequest- Uri“ http://somehost:8080/xyz”-方法:获取-Headers @ {“ Authorization” =“基本$ auth”;“ Accept” =“ application / xml”} -ContentType“ application / xml”“

但是,我收到以下错误-

Basic:术语“ Basic”不被视为cmdlet,函数, 脚本文件或可操作程序。检查名称的拼写或是否包含路径 已包含在内,请验证路径是否正确,然后重试。 在线:1个字符:253 + ... {Authorization = Basic $ auth; Accept = application / xml} -ContentType 应用程序/ xml + ~~~~~     + CategoryInfo:ObjectNotFound :(基本:字符串)[],CommandNotFou    ndException     + FullyQualifiedErrorId:CommandNotFoundException

Invoke-WebRequest中的双引号引起了问题。我试图使用`进行转义,给出多个双引号,并尝试了不同的组合,但徒劳。尝试了整整3天,最后还是作为最后的手段来到这里。请帮助构建以上内容。我只能访问CMD,并且只需要从那里执行powershell。因此,我需要使用powershell -Command,并在其后跟随上述powershell命令。

2 个答案:

答案 0 :(得分:0)

解决奇怪的字符序列的最简单方法是使用encoded command

$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

您可以将其与format string运算符结合使用以创建所需的字符串

答案 1 :(得分:0)

不用担心。.我终于做到了-

powershell -Command“ $ auth = [System.Convert] :: ToBase64String([System.Text.Encoding] :: UTF8.GetBytes('admin'+':'+'admin'));”调用WebRequest -Uri'http://someurl:8080/xyz'-method:get -Headers @ {'Authorization'='Basic'“ +” $ auth“}”;