标签: powershell

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
查看次数

Powershell 使用公共属性合并两个数组的最快方法?

使用公共属性合并两个数组的最快方法是什么?

Users | Select *
Username : Joe.Doe
Office   : Chicago
Email    :

Username : Mike.Smith
Office   : New York
Email    :
...
Run Code Online (Sandbox Code Playgroud)
UserEmails | Select *
AccountEmail  : Mike.Smith
EmailAddress  : mike-smith@example.com

AccountEmail  : Joe.Doe
EmailAddress  : jsmith12@example.com
...

Run Code Online (Sandbox Code Playgroud)

合并应该导致:

UsersCompleteList | Select *
Username : Joe.Doe
Office   : Chicago
Email    : jsmith12@example.com

Username : Mike.Smith
Office   : New York
Email    : mike-smith@example.com
...
Run Code Online (Sandbox Code Playgroud)

类似的东西for each ($user in $users) { ($user.Email = $userEmails | ? { $_.AccountEmail -eq …

lookup powershell performance

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

将 Powershell 核心设置为 Windows/Linux 上的默认 GNU Make shell

在 Windows 上的 makefile 中。

使用以下 make 版本:

PS C:\projects> make --version
GNU Make 4.1
Built for i686-w64-mingw32
Copyright (C) 1988-2014 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)

我试图在没有明确指定 shell 的情况下设置SHELL := pwsh/COMSPEC := pwsh和运行命令:

# COMSPEC := pwsh -c
SHELL := pwsh -c

VAR=asdf/asdf

.PHONY: get_var

get_var:
    @Write-Output $(VAR)
Run Code Online (Sandbox Code Playgroud)

没有成功。我有一个错误:

PS C:\projects\makefile_factory> make -f .\fi.makefile get_var
process_begin: CreateProcess(NULL, Write-Output asdf/asdf, ...) failed.
make (e=2): ?? ??????  ????? ????????? ????.
.\fi.makefile:10: recipe for target 'get_var' failed
make: *** [get_var] …
Run Code Online (Sandbox Code Playgroud)

powershell makefile gnu-make

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

PowerShell中`>`(重定向)和Out-File有什么区别

我已经找到了比较>反对Set-Content,而Set-Content反对Out-File,但我还没有找到如何任何说明>,并Out-File在PowerShell中有所不同。

“关于重定向”文件目前指出,

通常,当您需要使用其参数时,您会使用 Out-File cmdlet...

...这让我相信它们是相同的,这>是首选,因为它简洁,但| Out-File在某些情况下这是必要的,因为>它不带参数。

这是正确的解释,还是这两种重定向方法的运行时行为存在差异?例如,>由于| Out-File管道和命令行开关抽象的额外开销,效率更高吗?是否>有向后兼容性,为了与DOS或Unix中的默认设置任何区别>?

powershell

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

在 Powershell 中使用批处理式环境变量解释字符串

我有一个字符串,其中包含批处理样式表示法 ( %programdata%\App-V) 中的环境变量,我需要在 Powershell 中对其进行评估。可以%ENV%在 Powershell 中轻松评估符号吗?我知道 Powershell 中环境变量的通常表示法是$env:ProgramData,但是字符串是从另一个我无法更改的命令返回给我的。

背景信息:Powershell Cmdlet ( (Get-AppvClientConfiguration -Name PackageInstallationRoot).Value) 奇怪地返回带有批处理样式环境变量 ( %programdata%\App-V)的字符串。

windows powershell batch-file environment-variables appv

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

你如何在powershell中解密securestring

$Variable = Read-Host "Enter thing" -AsSecureString
Run Code Online (Sandbox Code Playgroud)

将提示您输入并将其保存为变量的安全字符串。如何解密安全字符串变量?

PS C:\Users\Todd> $Variable
System.Security.SecureString
Run Code Online (Sandbox Code Playgroud)

powershell

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

Windows 终端 - 使用不同的配置文件启动 powershell

如何配置 Windows 终端以使用标准配置以外的单独配置文件启动 Powershell $Profile

这将有助于导入模块,例如posh-git并且psreadline仅在我启动的 Powershell 实例中,wt.exe而不是从pwsh.exe

powershell powershell-core windows-terminal

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

Why does VS Code PowerShell terminal map a new drive?

I was making a PowerShell script in Visual Studio Code that used the command Get-PSDrive, and to my surprise, it seemed like while using VS Code, a new drive identical to my C:\ drive called Temp appeared.

图 1.1

I was taken aback by this result as as far as I knew, I only had 2 other drives besides my main C:\ drive connected. I tried to replicate this on other terminals with no success except for PowerShell 7:

图 1.2 Windows Terminal …

powershell powershell-5.0 visual-studio-code powershell-7.0

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

PowerShell 传递函数中接收到的所有参数,并处理带空格的参数

我是 PowerShell 的新手。在 Msys2(或 Lnux)中,我定义了一个函数npp

npp ()
{
    ${NPP_PATH} "$@"
}
Run Code Online (Sandbox Code Playgroud)

这样,如果我从命令提示符调用,npp它会启动 Notepad++(如 中所定义${NPP_PATH})。如果我调用npp "mydir/stage 1/a.txt"它会打开该文件进行编辑。一般来说,它允许:

  1. 任意数量的参数。
  2. 包含空格的参数,如果适当转义。

PowerShell 中的等价物是什么?

我想在 PS 我也应该去寻找一个函数来获得类似的行为。到目前为止,我可以接收未定义数量的参数,并在foreach循环中使用它们,请参阅下面的代码。但是我找不到简单的等价物"$@"来传递接收到的所有参数。此外,如果我在其中一个参数中使用引号,它们将被删除,因此文件路径可能会出现问题,包括空格。

function multi_params {
    param(
        [Parameter(
            ValueFromRemainingArguments=$true
        )][string[]]
        $listArgs
    )
    $count = 0
    foreach($listArg in $listArgs) {
        '$listArgs[{0}]: {1}' -f $count, $listArg
        $count++
    }
}
Run Code Online (Sandbox Code Playgroud)

parameters powershell function variadic-functions

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

PowerShell 无法识别安装在同一脚本中的 AWS CLI

我已经使用 powershell 脚本安装了 aws cli

 $command = "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12"
 Invoke-Expression $command
 Invoke-WebRequest -Uri "https://awscli.amazonaws.com/AWSCLIV2.msi" -Outfile C:\AWSCLIV2.msi
 $arguments = "/i `"C:\AWSCLIV2.msi`" /quiet"
 Start-Process msiexec.exe -ArgumentList $arguments -Wait
 aws --version
Run Code Online (Sandbox Code Playgroud)

当我尝试打印aws --version它时会出现以下错误。

aws : The term 'aws' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a path 
was included, verify that the path is correct and try again.
At line:1 char:1
+ aws
+ …
Run Code Online (Sandbox Code Playgroud)

powershell amazon-web-services

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