C#:如何通过 PowerShell.Invoke() 动态获取混合类型上的单个串联值?

发布于 2025-01-18 07:53:25 字数 1770 浏览 2 评论 0原文

我有以下PowerShell脚本:

Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
        $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
        $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
        $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
        $res = $ver + " (" + $min + " - "  + $max +  ")"
    }
    $_, $res
}

其输出包含软件包信息($ _)以及串联字符串($ res)。

虽然我能够通过powershell.invoke()在c#中读取/映射软件包信息,但我遇到了阅读串联字符串的挣扎。它仅向我显示字符串的长度,但不显示动态变量中的字符串本身。我还尝试读取baseObject,tockiperbaseObject,属性[0]和值,但到目前为止尚无成功。

这是C#代码:

public static void Test() { 
  string script = @"Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
      $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
      $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
      $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
      " + 
      "$res = $ver + \" (\" + $min + \" - \"  + $max +  \")\"" + @"
    }
    $_, $res
  }";    

  PowerShell powerShell = PowerShell.Create().AddScript(script);
  bool packageData = false;
  foreach (dynamic item in powerShell.Invoke().ToList()) {
    packageData ^= true;
    if (packageData) {
      // handle package data:
      // this works
    } else {
      // handle concatenated string
      // does not work - how to get its value?
    }
  }
}

或者,如果可能的话,我想知道如何将PowerShell软件包输出结构直接扩展。我尝试了这种方法通过使用add> add-member>选择,但它不起作用,因为我真的不知道将-Name -value-passthru放在哪里。

I have the following PowerShell script:

Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
        $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
        $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
        $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
        $res = $ver + " (" + $min + " - "  + $max +  ")"
    }
    $_, $res
}

Its output contains the package information ($_) as well as a concatenated string ($res).

While I am able to read/map the package information by PowerShell.Invoke() in C#, I have struggles reading the concatenated string. It shows me only the length of the string but not the string itself within the dynamic variable. I also tried reading BaseObject, ImmediateBaseObject, Properties[0] and Value but no success, so far.

Here is the C# Code:

public static void Test() { 
  string script = @"Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
      $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
      $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
      $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
      " + 
      "$res = $ver + \" (\" + $min + \" - \"  + $max +  \")\"" + @"
    }
    $_, $res
  }";    

  PowerShell powerShell = PowerShell.Create().AddScript(script);
  bool packageData = false;
  foreach (dynamic item in powerShell.Invoke().ToList()) {
    packageData ^= true;
    if (packageData) {
      // handle package data:
      // this works
    } else {
      // handle concatenated string
      // does not work - how to get its value?
    }
  }
}

Alternatively, I would like to know how to extend the PowerShell package output structure directly with the concatenated string, if possible. I tried this approach by using Add-Member or Select but it did not work as I do not really know where to put the -Name -Value and -PassThru.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2025-01-25 07:53:26

通过进一步测试扩展包结构的方法得到了它,从而认识到在脚本中使用单引号可以提高其在 C# 中的可读性:

public static void Test() {
  string script = @"Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
      $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
      $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
      $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
      $res = $ver + ' (' + $min + ' - '  + $max + ')'
    }
    Add-Member -InputObject $_ -MemberType NoteProperty -Name 'Dependency' -Value $res -PassThru
  }";    

  PowerShell powerShell = PowerShell.Create().AddScript(script);
  foreach (dynamic item in powerShell.Invoke().ToList()) {
    string dependency = item.Dependency;
    // further package handling ...
  }
}

Got it by further testing the approach to extend the package structure and thereby recognized that using single quotes within the script improves its readabilty in C#:

public static void Test() {
  string script = @"Get-AppxPackage | % {
    Get-AppxPackageManifest -Package $_ | % {
      $ver = $_.Package.Dependencies.TargetDeviceFamily.Name
      $min = $_.Package.Dependencies.TargetDeviceFamily.MinVersion
      $max = $_.Package.Dependencies.TargetDeviceFamily.MaxVersionTested
      $res = $ver + ' (' + $min + ' - '  + $max + ')'
    }
    Add-Member -InputObject $_ -MemberType NoteProperty -Name 'Dependency' -Value $res -PassThru
  }";    

  PowerShell powerShell = PowerShell.Create().AddScript(script);
  foreach (dynamic item in powerShell.Invoke().ToList()) {
    string dependency = item.Dependency;
    // further package handling ...
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文