我有一个脚本,可以在订阅内的 Azure VM 列表上运行,并为每个 VM 输出一个文件,以下是其内容示例:
Value[0] :
Code : ComponentStatus/StdOut/succeeded
Level : Info
DisplayStatus : Provisioning succeeded
Message : Directory: C:\variasuit_windows\HARMOR\windows_server\package
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 12/23/2023 5:10 AM HARMOR
"Server Name","GPO_HARMOR","BIOMEDIC_AGENT","EDWARD_STATUS","SENSOR_STATUS","SENTINEL_STATUS"
"AZ-TSTAPP1D","OK","OK","OK","OK","KO"
Value[1] :
Code : ComponentStatus/StdErr/succeeded
Level : Info
DisplayStatus : Provisioning succeeded
Message :
Status : Succeeded
Capacity : 0
Count : 0
我想使用 powershell 解析文件以获得这一行:
"AZ-TSTAPP1D","OK","OK","OK","OK","KO"
然后将每个文件中的值收集到另一个 txt 文件,得到类似这样的内容
"AZ-TSTAPP1D","OK","OK","OK","OK","KO"
"AZ-TSTAPP2D","OK","KO","KO","KO","KO"
"AZ-TSTAPP3D","OK","OK","OK","OK","KO"
我尝试获取该特定行的内容,但我发现行号可以从一个文件更改为另一个文件
我试过这个:
$scriptResult = Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -CommandId 'RunPowerShellScript' -ScriptPath $scriptPath
$scriptResult | Out-File $tempFile
$contentFromFile = Get-Content -Path $tempFile
$line12 = $contentFromFile | Select-Object -Index 11
$line12 | Out-File $outputFile -Append
Remove-Item $tempFile
请问有什么帮助吗?
谢谢
一般说明:
Invoke-AzVMRunCommand
是否能够中继(序列化)对象,但是如果是的话,您最好对对象进行操作,而不是对其 用于显示字符串表示形式。至于你的代码:
Out-String
和 -Stream
参数来逐行获取用于显示的格式化表示;如果 Invoke-AzVMRunCommand
无论如何只发出 text,你甚至不需要它。
-match
运算符和 regex 来过滤掉感兴趣的行。
您可以使用
>>
重定向 运算符作为管道到 Out-File
-Append
的快捷方式。
@($scriptResult | Out-String -Stream) -match '^"AZ-TSTAPP' >> $outputFile