我需要從輸入下方拆分 ips 和主機名,并使用 powershell 轉換為陣列物件我有一個如下所示的輸入
$docu_results = 'existing IPs: "192.168.1.12","","192.168.1.15","192.168.1.16"
existing hostname: "node.example.com","node1.example.com","node2.example.com",""'
我的預期輸出如下
$existing_ips = "192.168.1.12","","192.168.1.15","192.168.1.16"
$existing_hosts = "node.example.com","node1.example.com","node2.example.com",""
上述變數 existing_ips 和 existing_hosts 可能具有空值或空值及其在這些變數之間的等效值,我需要將其保存在單獨的變數中
最終輸出如下
192.168.1..12 :: node.example.com
192.168.1..15
192.168.1..15 :: node2.example.com
192.168.1..16
missing data:
value is missing for '' -> 'node1.example.com'
value is missing for '192.168.1.16' -> ''
uj5u.com熱心網友回復:
雖然可能有更簡潔的解決方案,但為了概念清晰,我將使用基于-split和的多步驟方法-replace:
$docu_results = 'existing IPs: "192.168.1.12","","192.168.1.15","192.168.1.16"
existing hostname: "node.example.com","node1.example.com","node2.example.com",""'
# Extract the two sub-lists from the input string.
$ipList, $hostList =
$docu_results -split 'existing IPs:|existing hostname:' -ne ''
# Split each list into an array of its elements.
$existing_ips = $iplist.Trim() -split ',' -replace '"'
$existing_hosts = $hostList.Trim() -split ',' -replace '"'
筆記:
- 使用陣列作為 LHS,PowerShell 比較運算子充當過濾器(請參閱此答案),這意味著
-ne ''上面從-split操作輸出的陣列中過濾掉任何空標記;由于分隔符之一位于輸入字串的最開頭,因此會產生一個空(-string)標記。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348770.html
上一篇:獲取傳遞給cli的第一個引數
