Powershell 使用前缀重新排序数组 headers
Powershell reorder an array using prefixes as headers
我在 PowerShell 变量中有以下简单数组
索引:1
姓名 : Windows 10 学历
索引:2
姓名 : Windows 10 学历 N
索引:3
名称:Windows 10 家企业
索引:4
名称:Windows 10 企业 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
...我想在下面的示例中重新排序
索引名称
----- ----
1 Windows 10 教育程度
2 Windows 10 教育 N
3 Windows 10 家企业
4 Windows 10 企业 N
5 Windows 10 专业版
6 Windows 10 专业 N
7 Windows 10 专业教育
8 Windows 10 专业教育 N
9 Windows 10 工作站专业版
10 Windows 10 Pro N 工作站
我已经尝试使用 split-string 但没有成功,我不确定如何继续。
任何帮助将不胜感激
鉴于您有一个字符串数组而不是对象,因此需要进行一些解析才能将其转换为 [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
我在 PowerShell 变量中有以下简单数组
索引:1
姓名 : Windows 10 学历
索引:2
姓名 : Windows 10 学历 N
索引:3
名称:Windows 10 家企业
索引:4
名称:Windows 10 企业 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
...我想在下面的示例中重新排序
索引名称
----- ----
1 Windows 10 教育程度
2 Windows 10 教育 N
3 Windows 10 家企业
4 Windows 10 企业 N
5 Windows 10 专业版
6 Windows 10 专业 N
7 Windows 10 专业教育
8 Windows 10 专业教育 N
9 Windows 10 工作站专业版
10 Windows 10 Pro N 工作站
我已经尝试使用 split-string 但没有成功,我不确定如何继续。
任何帮助将不胜感激
鉴于您有一个字符串数组而不是对象,因此需要进行一些解析才能将其转换为 [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