C#:如何通过 PowerShell.Invoke() 动态获取混合类型上的单个串联值?
我有以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过进一步测试扩展包结构的方法得到了它,从而认识到在脚本中使用单引号可以提高其在 C# 中的可读性:
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#: