相关疑难解决方法(0)

pwsh -命令正在删除引号

在 pwsh 中调用以下命令:

Write-Host '{"drop_attr": "name"}' 
Run Code Online (Sandbox Code Playgroud)

结果正常:

{"drop_attr": "name"}
Run Code Online (Sandbox Code Playgroud)

现在通过 pwsh 执行相同操作:

pwsh -Command Write-Host '{"drop_attr": "name"}'
Run Code Online (Sandbox Code Playgroud)

结果缺少引号和方括号?

drop_attr: name
Run Code Online (Sandbox Code Playgroud)

powershell

4
推荐指数
1
解决办法
3501
查看次数

PowerShell JSON 字符串转义(反斜杠)

我需要使用 PowerShell 脚本将 Json 主体 HttpPost 发送到 ASP.NET Core Web Api 端点(控制器)。

$CurrentWindowsIdentity = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$CurrentPrincipalName = $CurrentWindowsIdentity.Identity.Name

# Build JSON payload
$JsonString = @"
{
    "CurrentPrincipalName":"$CurrentPrincipalName"
}
"@

$response = Invoke-RestMethod -Uri "https://webapiendpoint.tld/api/somecontroller" -Method Post -Body $JsonString -ContentType "application/json"
Run Code Online (Sandbox Code Playgroud)

由于变量 $CurrentPrincipalName 的值可以是域\用户名,由于反斜杠未正确转义,json get 无效。

web api 的日志中的错误:

  JSON input formatter threw an exception: 'C' is an invalid escapable character within a JSON string. The string should be correctly escaped. Path: $.CurrentPrincipalName | LineNumber: 15 | BytePositionInLine: 36.
  System.Text.Json.JsonException: …
Run Code Online (Sandbox Code Playgroud)

powershell json

3
推荐指数
1
解决办法
3158
查看次数

将参数字符串转换为 Here 字符串

我想用 Start-Process 和 -Wait 调用 ffmpeg,$ArgumentList 有问题

function convertgif {
  
  $ArgumentList = @'
 -i $($args[0]) -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$([IO.Path]::ChangeExtension($args[0], 'mp4'))"
'@
  Start-Process -FilePath "C:\ffmpeg\bin\ffmpeg.exe" -ArgumentList $ArgumentList -Wait -NoNewWindow;
}
Run Code Online (Sandbox Code Playgroud)

我找不到如何修复错误

$args[0]: No such file or directory
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
56
查看次数

'kubectl patch' works on Linux Bash but not in Windows Powershell ISE

The following command works fine on Ubuntu bash:

kubectl patch deployment wapi-backend-d1 --patch '{"spec": {"template": {"metadata": {"labels": {"date": "test"}}}}}'
Run Code Online (Sandbox Code Playgroud)

The same command does not work in Windows Powershell Console (ISE).

The error is:

kubectl : Error from server (BadRequest): invalid character 's' looking for beginning of object key string
At line:1 char:1
+ kubectl patch deployment wapi-backend-d1 --patch '{"spec": {"template ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error from serv...ject key string:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)

The powershell …

syntax powershell parameter-passing quoting kubectl

2
推荐指数
2
解决办法
423
查看次数

带有管道的 powershell 和 cmd 的问题

我有这个在 powershell 上运行正常的命令

Compare-Object (Get-Content "tex1.txt") (Get-Content "tex2.txt") | Where-Object{$_.SideIndicator -eq "<="} | select inputobject |  ft -hidetableheaders
Run Code Online (Sandbox Code Playgroud)

我正在尝试通过执行以下操作在 cmd 中运行:

powershell -Command " & {Compare-Object (Get-Content "tex1.txt") (Get-Content "tex2.txt") | Where-Object{$_.SideIndicator -eq "<="} | select inputobject |  ft -hidetableheaders}"
Run Code Online (Sandbox Code Playgroud)

但它说的是:名称、目录或卷语法不正确(是西班牙语,所以我不知道确切的翻译)

我认为问题在于管道,因为在管道之前运行所有内容:Compare-Object (Get-Content "tex1.txt") (Get-Content "tex2.txt")有效

PD:我也尝试^在管道之前写,但我没有成功。

powershell cmd

1
推荐指数
1
解决办法
1162
查看次数

标签 统计

powershell ×5

cmd ×1

json ×1

kubectl ×1

parameter-passing ×1

quoting ×1

syntax ×1