如何在 PowerShell 中创建 Json?
我认为这与在 PowerShell 中创建 Xml 类似,但它不起作用:
[Json]$MyJsonVariable = @"
{
"MyList"{
"Item1" {
"Name": "AMD Ryzen 5 3600x"
"Type":"CPU"
"Price":"`$69.99"
"Where":"Amazon.com"
}
}
}
"@
Run Code Online (Sandbox Code Playgroud)
但这没有用:
Unable to find type [Json].
At line:1 char:1
+ [Json]$MyJsonVariable = @"
+ ~~~~~~
+ CategoryInfo : InvalidOperation: (Json:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
Run Code Online (Sandbox Code Playgroud)
请帮助
如果我尝试在 ISE 中保存文件,默认扩展名是Powershell Files带括号(*.ps1, *.psm1, *.psd1, *.ps1xml, *.pssc, *.psrc, *.cdxml)和描述
Powershell Scripts (*.ps1)
Powershell Modules (*.psm1)
Powershell Data Files (*.psd1)
Powershell Session Configuration Files (*.pssc)
Powershell Role Capability Files (*.psrc)
Powershell Xml Files (*.psxml, *.cdxml)
Run Code Online (Sandbox Code Playgroud)
它们分别是做什么的?
如果我使用[char]数据类型,它要求值是一个字符,例如
[char]"a"
a
Run Code Online (Sandbox Code Playgroud)
因为如果我将它与多个字符一起使用,它会给我一个错误:
[char]"ab"
Cannot convert value "ab" to type "System.Char". Error: "String must be exactly one
character long."
At line:1 char:1
+ [char]"ab"
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastParseTargetInvocation
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用
[char[]]"ab"
Run Code Online (Sandbox Code Playgroud)
我得到输出
a
b
Run Code Online (Sandbox Code Playgroud)
如果我比较Get-Member两者,我不会得到任何结果:
PS C:\Users\nijoh> Compare-Object -ReferenceObject $([char] | gm) -DifferenceObject $([char[]] | gm) -PassThru
PS C:\Users\nijoh>
Run Code Online (Sandbox Code Playgroud)
但我可以看到它们是两种不同的类型,因为它们的显示方式不同:
PS C:\Users\nijoh> ([char[]]"a").GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Char[] System.Array
PS C:\Users\nijoh> …Run Code Online (Sandbox Code Playgroud) 在我使用过的大多数其他语言中,正则表达式始终区分大小写,但是 powershell 似乎不正统,正则表达式不区分大小写。我该如何改变这个?
"a" -match "A"
Run Code Online (Sandbox Code Playgroud)
预期输出:
False
Run Code Online (Sandbox Code Playgroud)
实际输出:
True
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变这个?