gpt4 book ai didi

powershell - 从类型为system.string的变量中读取值

转载 作者:行者123 更新时间:2023-12-02 23:52:39 26 4
gpt4 key购买 nike

我在下面的输出中有一个$gh变量。我的意思是当我键入$gh时。我将获得如下所示的值。

PS C:\Windows\system32>$gh

No. Department Name Environment Id Location Name Ready Status Machine Pure Status
1 EMA-MET-CAT5-KY04 Environment-k2345 EMA Enabled GREEN
2 EMA-MET-CAT5-KY03 Environment-k89 EMA Enabled GREEN
3 EMA-EFT-JIU-FC Environment-k3456 EMA Enabled GREEN
4 EMA-MET-CAT5-KY08 Environment-k7890 EMA Not Ready UNKNOWN
5 EMA-MET-CAT5-KY02-ED Environment-k9 EMA Enabled GREEN
$gh的类型是 System.String,如 $gh.GetType().fullname所示。

我想将部门名称和环境ID的值放入数组或变量中,这样,如果我查询EMA-MET-CAT5-KY08,我就可以获取环境ID Environment-k7890。

最佳答案

  • 另一种方法是将表拆分为列
    通过用逗号
  • 替换两个或多个空格
  • ,因为您有适当的 header ,然后可以ConvertFrom-Csv
  • Select-Object所需属性

  • $gh = "No.  Department Name          Environment Id           Location Name   Ready Status          Machine Pure Status   
    1 EMA-MET-CAT5-KY04 Environment-k2345 EMA Enabled GREEN
    2 EMA-MET-CAT5-KY03 Environment-k89 EMA Enabled GREEN
    3 EMA-EFT-JIU-FC Environment-k3456 EMA Enabled GREEN
    4 EMA-MET-CAT5-KY08 Environment-k7890 EMA Not Ready UNKNOWN
    5 EMA-MET-CAT5-KY02-ED Environment-k9 EMA Enabled GREEN
    "

    $Data = $gh -split [environment]::newline | ForEach-Object {
    $_ -replace ' {2,}',','
    } | ConvertFrom-csv | Select-Object "Department Name","Environment Id"

    $Data | Where-Object 'Department Name' -eq 'EMA-MET-CAT5-KY08'

    样本输出:
    Department Name   Environment Id
    --------------- --------------
    EMA-MET-CAT5-KY08 Environment-k7890

    关于powershell - 从类型为system.string的变量中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55482597/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com