我正在尝试使用 power shell 脚本来读取文件的内容并从中选择特定类型的单词。我需要将找到的单词加载为我打算在下游进一步使用的变量。
这是我的输入文件的样子:
{
"AvailabilityZone": "ap-northeast-1b",
"VolumeType": "gp2",
"VolumeId": "vol-087238f9",
"State": "creating",
"Iops": 100,
"SnapshotId": "",
"CreateTime": "2016-09-15T12:17:27.952Z",
"Size": 10
}
我想选择的具体词是vol-xxxxxxxx
. 我使用此链接编写脚本
如何在 powershell 的选择字符串中传递变量
这就是我的做法:
$Filename = "c:\reports\volume.jason"
$regex = "^[vol-][a-z0-9]{8}$"
$newvolumeid=select-string -Pattern $regex -Path $filename > C:\Reports\newVolumeid.txt
$newVolumeid
当我运行这个脚本时,它运行但没有给出任何响应。似乎以某种方式选择字符串的输出未加载到变量$newvolumeid
中。
知道如何解决这个问题吗?或者我错过了什么?
PS:上面提到的帖子大约有3年的历史了,所以我重新发布了。