[英]How do I get windows registry property values using a PowerShell script run through a .Net PowerShell object?
I am using the following PowerShell script to return information about apps installed on a PC:我正在使用以下 PowerShell 脚本返回有关安装在 PC 上的应用程序的信息:
Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
When I run this script in the Windows Powershell ISE, I see something like this for results:当我在 Windows Powershell ISE 中运行此脚本时,我看到类似这样的结果:
Hive: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
Name Property
名称属性
---- -------- Connection Manager SystemComponent: 1
---- -------- 连接管理器系统组件:1
DirectDrawExDirectDrawEx
Docker Desktop DisplayIcon: C:\Program Files\Docker\Docker\Docker Desktop Installer.exeDocker 桌面显示图标:C:\Program Files\Docker\Docker\Docker Desktop Installer.exe
DisplayName: Docker Desktop显示名称:Docker 桌面
DisplayVersion: 2.1.0.4显示版本:2.1.0.4
Version: 39773版本:39773
InstallLocation: C:\Program Files\Docker\Docker安装位置:C:\Program Files\Docker\Docker
NoModify: 1不修改:1
NoRepair: 1无修复:1
Publisher: Docker Inc.出版商:Docker Inc.
ChannelName: stable频道名称:稳定
ChannelUrl: https://download.docker.com/win/stable/appcast.xmlChannelUrl: https://download.docker.com/win/stable/appcast.xml
UninstallString: "C:\Program Files\Docker\Docker\Docker DesktopUninstallString: "C:\Program Files\Docker\Docker\Docker Desktop
Installer.exe" uninstallInstaller.exe”卸载
I am calling the script from a C#.Net application as follows:我从 C#.Net 应用程序调用脚本如下:
using (var ps = PowerShell.Create())
{
ps.Runspace = p_runspace;
ps.AddScript("Get-ChildItem ""HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall""");
IEnumerable<PSObject> commandOutput = ps.Invoke();
}
How do I programmatically get access in C# to the properties returned when I run the script (Publisher, DisplayName, DisplayVersion) returned when I run the script in the ISE?我如何以编程方式在 C# 中访问在 ISE 中运行脚本时返回的属性(Publisher、DisplayName、DisplayVersion)? I examined the properties on the PSObject in my app, they do not match up with the ISE.
我在我的应用程序中检查了 PSObject 的属性,它们与 ISE 不匹配。
Thanks, JohnB谢谢,约翰B
Objects returned from PowerShell when invoked using PowerShell.Invoke()
don't don't actually have all of the same members at the top level of the object, a bit of PowerShell "magic" makes it work that way from within a PowerShell session.当使用
PowerShell.Invoke()
调用时,从 PowerShell 返回的对象实际上并没有在 882730795402888 的顶层拥有所有相同的成员,PowerShell 的一点“魔力”使它在 88273079441418868188818888 中以这种方式工作.
If you want a property value you need to inspect the Properties
property of the returned PSObject
:如果你想要一个属性值,你需要检查返回的
PSObject
的Properties
属性:
foreach(PSObject obj in commandOutput) {
Object val = obj.Properties['PropertyName'].Value
}
where PropertyName
is the name of the property you want.其中
PropertyName
是您想要的属性的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.