Powershell ForEach循环为什么会产生不正确的输出

huangapple go评论90阅读模式
英文:

Why Powershell ForEach loop gives incorrect output

问题

我计划生成一份关于我们应用程序代理版本之一的报告,所以我想编写一个流程,该流程将遍历每个订阅中的每个虚拟机并将自定义脚本扩展部署到每个虚拟机资源,然后检索CSE输出值并将其放入表格中,之后我想将其作为CSV文件发送到电子邮件中。脚本代码如下:

  1. # ... 变量初始化已省略
  2. $subscriptions | ForEach-Object {
  3. Select-AzSubscription -Subscription $_.Name
  4. $vms = Get-AzVm -Status | Where-Object {$_.StorageProfile.OsDisk.OsType -eq "Windows" -and $_.Name -notlike "azbecdns*" -and $_.Name -notlike "SF160SV0*" -and $_.PowerState -eq "VM running";}
  5. forEach ($vm in $vms) {
  6. $CSEvalidation = Get-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName | where-object {$_.Extensiontype -like "CustomScript*"}
  7. if ($CSEvalidation -ne $null)
  8. {
  9. Remove-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Name $CSEvalidation.name -Force -ErrorAction Continue
  10. }
  11. Set-AzVMCustomScriptExtension -TypeHandlerVersion 1.10 `
  12. -ResourceGroupName $vm.resourceGroupName `
  13. -VMName $vm.Name `
  14. -FileName $fileName `
  15. -ContainerName $containerName `
  16. -StorageAccountName $storageAccountName `
  17. -StorageAccountKey $storageAccountKey `
  18. -Run $fileName `
  19. -Location "West Europe" `
  20. -Name "CustomScriptExtension"
  21. # 这个变量解析有效,并列出所有虚拟机
  22. $cseOutput = "test"
  23. # 这不起作用,只列出了最初的最后一个虚拟机实例
  24. $cseOutput = ((Get-AzVm -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Status).Extensions | Where {$_.Type -eq 'Microsoft.Compute.CustomScriptExtension'}).SubStatuses.Message[0]
  25. [PSCustomObject] @{
  26. VmName = $vm.name
  27. AgentVer = $cseOutput
  28. }
  29. }
  30. } | Select-Object -Property VmName, AgentVer | Where-Object {$_.VmName -ne $null} | export-csv -NoTypeInformation -delimiter ';' -Path $(Pipeline.Workspace)\***.csv

CSV文件只包含最后一个输入,但是当我在循环内部检查$hash变量的内容时,它显示了每个虚拟机及其相应值的列表。我已经尝试了好几天,但仍然无法得到我想要的结果。我还使用了数组对象,但据我所知,这不是一个足够的方法,因为它是一个非常耗时的方法,但结果仍然是一样的 - CSV文件只包含最后一个输入。我应该如何修改脚本以获得我想要的结果 - 即包含所有订阅中所有虚拟机名称和CSE子状态消息的CSV文件的两列。

英文:

I'm planning to generate a report on one of our app agent version, so I want to write a pipeline, that will go through every VM in every subscription and deploy a Custom Script Extension to every VM resource, retrieve CSE output value and put in into table, which later I want to send as a CSV file in email. Script code looks like this:

  1. # ... variable initializations omitted
  2. $subscriptions | ForEach-Object {
  3. Select-AzSubscription -Subscription $_.Name
  4. $vms = Get-AzVm -Status | Where-Object {$_.StorageProfile.OsDisk.OsType -eq "Windows" -and $_.Name -notlike "azbecdns*" -and $_.Name -notlike "SF160SV0*" -and $_.PowerState -eq "VM running"}
  5. forEach ($vm in $vms) {
  6. $CSEvalidation = Get-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName | where-object {$_.Extensiontype -like "CustomScript*"}
  7. if ($CSEvalidation -ne $null)
  8. {
  9. Remove-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Name $CSEvalidation.name -Force -ErrorAction Continue
  10. }
  11. Set-AzVMCustomScriptExtension -TypeHandlerVersion 1.10 `
  12. -ResourceGroupName $vm.resourceGroupName `
  13. -VMName $vm.Name `
  14. -FileName $fileName `
  15. -ContainerName $containerName `
  16. -StorageAccountName $storageAccountName `
  17. -StorageAccountKey $storageAccountKey `
  18. -Run $fileName `
  19. -Location "West Europe" `
  20. -Name "CustomScriptExtension"
  21. #this var parse works and lists all vms
  22. $cseOutput = "test"
  23. #this doesn't work and only list last vm instance like in the very beginning
  24. $cseOutput = ((Get-AzVm -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Status).Extensions | Where {$_.Type -eq 'Microsoft.Compute.CustomScriptExtension'}).SubStatuses.Message[0]
  25. [PSCustomObject] @{
  26. VmName = $vm.name
  27. AgentVer = $cseOutput
  28. }
  29. }
  30. } | Select-Object -Property VmName, AgentVer | Where-Object {$_.VmName -ne $null} | export-csv -NoTypeInformation -delimiter ';' -Path $(Pipeline.Workspace)\***.csv

CSV file only consists a last input, but when I check within the loop the content of a $hash variable, it shows a list of every VM and it's coressponding value. I've been trying to figure this out for days and still can't get a result I want. I also used an array object, but afair it's not really sufficient way of doing it because it's a very process time consuming method, yet still result was the same - csv file only consisted of last input. How should I modify script to get the result I want - being a CSV file with two columns - list of all vm names from all subscriptions and cse substatus message.

答案1

得分: 1

  1. **更新**:
  2. * `$cseOutput` 的值中存在*隐藏控制字符*,导致了问题,通过使用 `$cseOutput = $cseOutput -replace ''\W''` 将其消除解决了问题。
  3. * 请注意,这个特定的基于正则表达式的 [`-replace`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#replacement-operator) 操作会从字符串中移除所有不是_单词_字符的字符,即不是字母数字字符或 `_` 的任何字符。
  4. * 要限制移除仅限于_控制字符__隐藏格式字符_(参见[正则表达式中的字符类](https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#SupportedUnicodeGeneralCategories)),请使用:
  5. `$cseOutput = $cseOutput -replace ''[\p{Cc}\p{Cf}]''`
  6. ---
  7. #### 原始答案:
  8. 您的症状有点神秘:如果哈希表中有多个条目,您*应该*看到多行,尽管列名是固定的 `"Name","Key","Value"`;尝试使用 `@{ one = 1; two = 2; three = 3 }.GetEnumerator() | ConvertTo-Csv` 作为一个快速示例。
  9. ---
  10. > 我应该如何修改脚本以获得我想要的结果 - 一个包含所有订阅的所有虚拟机名称和 cse 子状态消息的 CSV 文件。
  11. 注意:**问题已更新,包含以下代码,并进行了修改。**
  12. 在您的 `foreach` 循环中,发出具有所需 CSV 列名的属性的 `[pscustomobject]` 实例,让 PowerShell 将它们收集到一个数组中 - 或者更好地,切换到 `ForEach-Object` 并将这些对象直接管道到 `Export-Csv`
  13. ```shell
  14. # ... 变量初始化省略
  15. $subscriptions |
  16. ForEach-Object {
  17. Select-AzSubscription -Subscription $_.Name
  18. $vms = Get-AzVm -Status | Where-Object { $_.StorageProfile.OsDisk.OsType -eq ''Windows'' -and $_.PowerState -eq ''VM running'' }
  19. forEach ($vm in $vms) {
  20. $CSEvalidation = Get-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -ErrorAction Continue | Where-Object { $_.Extensiontype -like ''CustomScript*'' }
  21. if ($CSEvalidation -ne $null) {
  22. $null = Remove-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Name $CSEvalidation.name -Force -ErrorAction Continue
  23. }
  24. $null = Set-AzVMCustomScriptExtension -TypeHandlerVersion 1.10 `
  25. -ResourceGroupName $vm.resourceGroupName `
  26. -VMName $vm.Name `
  27. -FileName $fileName `
  28. -ContainerName $containerName `
  29. -StorageAccountName $storageAccountName `
  30. -StorageAccountKey $storageAccountKey `
  31. -Run $fileName `
  32. -Location ''West Europe'' `
  33. -Name ''CustomScriptExtension'' `
  34. -ErrorAction Continue
  35. $cseOutput = ((Get-AzVm -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Status).Extensions | where { $_.Type -eq ''Microsoft.Compute.CustomScriptExtension'' }).SubStatuses.Message[0]
  36. # 构造并输出一个具有所需属性的 [pscustomobject] 实例。属性名称将成为 CSV 列名。
  37. [pscustomobject] @{
  38. VmName = $vm.Name
  39. CseSubstatus = $cseOutput
  40. }
  41. }
  42. } |
  43. Export-Csv -Delimiter '';'' -NoTypeInformation -Path $(Pipeline.Workspace)\***.csv
  1. <details>
  2. <summary>英文:</summary>
  3. &lt;!-- language-all: sh --&gt;
  4. **Update**:
  5. * *Hidden control characters* in the value of `$cseOutput` turned out to cause the problem, and eliminating them with `$cseOutput = $cseOutput -replace &#39;\W&#39;` solved it.
  6. * Note that this specific regex-based [`-replace`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#replacement-operator) operation removes all characters that aren&#39;t _word_ characters from the string, i.e. anything isn&#39;t either an alphanumeric character or `_`.
  7. * To limit removal to _control characters_ and _hidden format characters_ only (see [Character classes in regular expressions](https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#SupportedUnicodeGeneralCategories)), use:
  8. `$cseOutput = $cseOutput -replace &#39;[\p{Cc}\p{Cf}]&#39;`
  9. ---
  10. #### Original answer:
  11. Your symptom is a bit of mystery: If the hashtable has multiple entries, you *should* see multiple rows, albeit with fixed column names `&quot;Name&quot;,&quot;Key&quot;,&quot;Value&quot;`; try with ` @{ one = 1; two = 2; three = 3 }.GetEnumerator() | ConvertTo-Csv` as a quick example.
  12. ---
  13. &gt; How should I modify script to get the result I want - being a CSV file with two columns - list of all vm names from all subscriptions and cse substatus message.
  14. Note: **The question has since been updated to include the code below, with modifications.**
  15. Emit `[pscustomobject]` instances with properties named for the desired CSV column names from your `foreach` loop and let PowerShell collect them in an array for you - or, preferable - switch to `ForEach-Object` and pipe those objects directly to `Export-Csv`:

... variable initializations omitted

$subscriptions |
ForEach-Object {
Select-AzSubscription -Subscription $.Name
$vms = Get-AzVm -Status | Where-Object { $
.StorageProfile.OsDisk.OsType -eq 'Windows' -and $.PowerState -eq 'VM running' }
forEach ($vm in $vms) {
$CSEvalidation = Get-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -ErrorAction Continue | Where-Object { $
.Extensiontype -like 'CustomScript*' }
if ($CSEvalidation -ne $null) {
$null = Remove-AzVmExtension -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Name $CSEvalidation.name -Force -ErrorAction Continue
}

  1. $null = Set-AzVMCustomScriptExtension -TypeHandlerVersion 1.10 `
  2. -ResourceGroupName $vm.resourceGroupName `
  3. -VMName $vm.Name `
  4. -FileName $fileName `
  5. -ContainerName $containerName `
  6. -StorageAccountName $storageAccountName `
  7. -StorageAccountKey $storageAccountKey `
  8. -Run $fileName `
  9. -Location &#39;West Europe&#39; `
  10. -Name &#39;CustomScriptExtension&#39; `
  11. -ErrorAction Continue
  12. $cseOutput = ((Get-AzVm -VmName $vm.name -ResourceGroupName $vm.resourceGroupName -Status).Extensions | where { $_.Type -eq &#39;Microsoft.Compute.CustomScriptExtension&#39; }).SubStatuses.Message[0]
  13. # Construct and output a [pscustomobject] instance with the desired
  14. # properties. The property names will become the CSV colum names.
  15. [pscustomobject] @{
  16. VmName = $vm.Name
  17. CseSubstatus = $cseOutput
  18. }
  19. }

} |
Export-Csv -Delimiter ';' -NoTypeInformation -Path $(Pipeline.Workspace)***.csv

  1. </details>

huangapple
  • 本文由 发表于 2023年6月5日 23:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定