首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell使用前缀作为标头重新排序数组

Powershell使用前缀作为标头重新排序数组
EN

Stack Overflow用户
提问于 2021-11-23 02:25:07
回答 1查看 70关注 0票数 0

在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台专用设备

我尝试过使用分裂字符串,没有运气,我不知道如何进行。

如能提供任何帮助,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-23 05:22:35

如果您有一个字符串数组,而不是一个对象,则需要进行一些解析才能将其转换为[pscustomobject]

解析array的方法有很多,这里我使用的是ConvertFrom-StringData,但是通过使用splittrim也可以完成同样的任务。

注意,$imageInfo只是为了测试代码,在您的示例中,您应该使用包含来自dism的结果的变量。

代码语言:javascript
运行
复制
$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
    )
}

结果

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70074677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档