在PowerShell变量中有以下简单数组
索引:1
名称: Windows 10教育
指数:2
名称: Windows 10 Education N
指数:3
名称: Windows 10 Enterprise
指数:4
名称: Windows 10 Enterprise N
指数:5
名称: Windows 10 Pro
指数:6
名称: Windows 10 Pro N
指数:7
名称: Windows 10专业教育
指数:8
名称: Windows 10 Pro Education N
指数:9
名称: Windows 10 Pro用于工作站
指数: 10
名称: Windows 10 Pro N,用于工作站
..。我想在下面的例子中重新排序
索引名
1.初级教育-免费教育
2.Windows=‘Windows 10’>教育.
3.中外合资企业
4._
5试纸机
6._
7.附属学校附属教育
8.Windows=‘Windows 3’>教育.
9.用于工作站的专用产品
10台专用设备
我尝试过使用分裂字符串,没有运气,我不知道如何进行。
如能提供任何帮助,将不胜感激。
发布于 2021-11-23 05:22:35
如果您有一个字符串数组,而不是一个对象,则需要进行一些解析才能将其转换为[pscustomobject]
。
解析array
的方法有很多,这里我使用的是ConvertFrom-StringData
,但是通过使用split
和trim
也可以完成同样的任务。
注意,$imageInfo
只是为了测试代码,在您的示例中,您应该使用包含来自dism
的结果的变量。
$imageInfo = @'
Index : 1
Name : Windows 10 Education
Index : 2
Name : Windows 10 Education N
Index : 3
Name : Windows 10 Enterprise
Index : 4
Name : Windows 10 Enterprise N
Index : 5
Name : Windows 10 Pro
Index : 6
Name : Windows 10 Pro N
Index : 7
Name : Windows 10 Pro Education
Index : 8
Name : Windows 10 Pro Education N
Index : 9
Name : Windows 10 Pro for Workstations
Index : 10
Name : Windows 10 Pro N for Workstations
'@ -split '\r?\n'
for($i = 0; $i -lt $imageInfo.Count; $i += 2)
{
[pscustomobject]$(
$imageInfo[$i].Replace(':','='),
$imageInfo[$i+1].Replace(':','=') |
Out-String |
ConvertFrom-StringData
)
}
结果
Index Name
----- ----
1 Windows 10 Education
2 Windows 10 Education N
3 Windows 10 Enterprise
4 Windows 10 Enterprise N
5 Windows 10 Pro
6 Windows 10 Pro N
7 Windows 10 Pro Education
8 Windows 10 Pro Education N
9 Windows 10 Pro for Workstations
10 Windows 10 Pro N for Workstations
https://stackoverflow.com/questions/70074677
复制相似问题