有件事我真的搞不懂,我需要帮助。我有一个包含这些ID加载的文件

我需要在powershell中创建一个json有效负载,如下所示,其中包含每个ID
{
"assetIds": [
"3fa85f64-5717-4562-b3fc-2c963f66afa6",
"f86354c4-8e69-4cee-b85c-11b9534019e0",
"6829c1ad-17d8-4d9c-93c7-2a4b6acfc201"
],
"sendNotification": true
}发布于 2021-10-19 15:53:31
相当简单:
# define object
$object = [pscustomobject]@{
assetIds = @(
Get-Content path\to\file.txt
) -as [string[]]
sendNotification = $true
}
# convert to JSON
$object |ConvertTo-Json@() (“子表达式运算符”)确保assetIds属性的值始终是一个数组(即使Get-Content返回0或仅返回1 ID)
-as [string[]]转换可防止PowerShell序列化可能已由Get-Content附加到字符串的扩展属性
https://stackoverflow.com/questions/69633945
复制相似问题