【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维

简介: 【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维

问题描述

编写PowerShell脚本,以多维(3维)数组中第二维度的值进行过滤,并打印出结果

#三维数组源数据
“A”, “11”, “Cheng Du”
“B”, “21”, “Chong Qing”
“C”, “31”, “Shang Hai”
“D”, “41”, “Bei Jing”
“E”, “51”, “Nan Jing”
#从地址中过滤以Chong开头的地区, 结果需要为
B, 21, Chong Qing

PowerShell脚本为:

$CityList = [System.Collections.ArrayList]::new()
$CityList.Add(@(“A”,“11”,“Cheng Du”)) | Out-Null
$CityList.Add(@(“B”,“21”,“Chong Qing”)) | Out-Null
$CityList.Add(@(“C”,“31”,“Shang Hai”)) | Out-Null
$CityList.Add(@(“D”,“41”,“Bei Jing”)) | Out-Null
$CityList.Add(@(“E”,“51”,“Nan Jing”)) | Out-Null
Write-Host "==== 开始过滤 Chong  ==== " -ForegroundColor DarkYellow
$FinalCityList = $CityList | Where-object -filterScript {$_[2] -like "Chong*"}
Write-Host "Final City List 类型" -ForegroundColor Green
$FinalCityList.GetType()
Write-Host "Final City 数量为: " $FinalCityList.Count -ForegroundColor Green
Write-Host ""
Write-Host "==== 循环打印List中所有元素结果 ==== "  -ForegroundColor DarkYellow
foreach( $aaa in $FinalCityList)
{
    Write-Host $aaa[0]','$aaa[1]','$aaa[2]
}
Write-Host ""
Write-Host "==== 直接输出对象的结果 ==== " -ForegroundColor red
$FinalCityList

输出结果 :当过滤结果大于1时,显示正常,当过滤结果为1时,结果显示异常。结果变成了一个一位数组,Count结果为3。

 

这是为什么呢?

 

问题分析

从 $FinalCityList 的打印结果分析,当 Where-Object 查看到结果只有一个的时候,就把结果对象进行了多维到一维的转换。所以结果变为了一个包含三行内容的一位数组。

问题解决

$FinalCityList = @($CityList | Where-object -filterScript {$_[2] -like "Chong*"})

把 Where-Object 方法用 @()  进行对象转换即可。

 

 

完整的PowerShell脚本为:

$CityList = [System.Collections.ArrayList]::new()
$CityList.Add(@(“A”,“11”,“Cheng Du”)) | Out-Null
$CityList.Add(@(“B”,“21”,“Chong Qing”)) | Out-Null
$CityList.Add(@(“C”,“31”,“Shang Hai”)) | Out-Null
$CityList.Add(@(“D”,“41”,“Bei Jing”)) | Out-Null
$CityList.Add(@(“E”,“51”,“Nan Jing”)) | Out-Null
foreach( $ccc in $CityList)
{
    Write-Host $ccc[0]','$ccc[1]','$ccc[2]
}
Write-Host "==== 开始过滤 CityList 中包含 Chong 的城市  ==== " -ForegroundColor Yellow
$FinalCityList = @($CityList | Where-object -filterScript {$_[2] -like "Chong*"})
Write-Host "Final City List 类型" -ForegroundColor Green
$FinalCityList.GetType()
Write-Host "Final City 数量为: " $FinalCityList.Count -ForegroundColor Green
Write-Host ""
Write-Host "==== 循环打印List中所有元素结果 ==== "  -ForegroundColor DarkYellow
foreach( $aaa in $FinalCityList)
{
    Write-Host $aaa[0]','$aaa[1]','$aaa[2]
}
Write-Host ""
Write-Host "==== 直接输出对象的结果 ==== " -ForegroundColor red
$FinalCityList

 

参考文档

数组子表达式运算符:  https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.2#the-array-sub-expression-operator

What does the "@" symbol do in PowerShell?:https://stackoverflow.com/questions/363884/what-does-the-symbol-do-in-powershell

 

 

相关文章
|
24天前
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
|
21天前
|
存储 JSON JavaScript
|
24天前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
24天前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
24天前
|
Java 开发工具
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到User信息,如Object ID
|
17天前
|
UED 开发工具 iOS开发
Uno Platform大揭秘:如何在你的跨平台应用中,巧妙融入第三方库与服务,一键解锁无限可能,让应用功能飙升,用户体验爆棚!
【8月更文挑战第31天】Uno Platform 让开发者能用同一代码库打造 Windows、iOS、Android、macOS 甚至 Web 的多彩应用。本文介绍如何在 Uno Platform 中集成第三方库和服务,如 Mapbox 或 Google Maps 的 .NET SDK,以增强应用功能并提升用户体验。通过 NuGet 安装所需库,并在 XAML 页面中添加相应控件,即可实现地图等功能。尽管 Uno 平台减少了平台差异,但仍需关注版本兼容性和性能问题,确保应用在多平台上表现一致。掌握正确方法,让跨平台应用更出色。
24 0
|
24天前
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
|
24天前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
24天前
|
API C++ Python
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
|
24天前
|
数据安全/隐私保护 异构计算 Windows
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter