我在 PowerShell 變數中有以下簡單陣列
索引: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 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 專業版 N
我試過使用 split-string 沒有運氣,我不知道如何繼續。
任何幫助,將不勝感激
uj5u.com熱心網友回復:
鑒于您有一個字串陣列而不是物件,因此需要進行一些決議才能將其轉換為[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://www.uj5u.com/shujuku/364498.html
