📌  相关文章
📜  powershell 转换为 json - Shell-Bash 代码示例

📅  最后修改于: 2022-03-11 14:50:40.897000             🧑  作者: Mango

代码示例1
You could try some string manipulation to get it in an expected JSON format, and then use ConvertFrom-Json to convert it to a PSCustomObject.

Simple Example: (simple because this assumes that these characters being replaced will only be delimiters)

# First, clean up the string.
PS C:\> $mystring = "@{Account='User01';Domain='Domain01';Admin='True'}"
PS C:\> $mystring = $mystring -replace "^@", ""
PS C:\> $mystring = $mystring -replace "=", ":"
PS C:\> $mystring = $mystring -replace ";", ","
PS C:\> $mystring
{Account:'User01',Domain:'Domain01',Admin:'True'}

# Afterwards, convert to PSCustomObject.
PS C:\> $myobject = $mystring | ConvertFrom-Json
PS C:\> $myobject

Account                                 Domain                                  Admin
-------                                 ------                                  -----
User01                                  Domain01                                True