简体   繁体   English

如何使用通过 a.Net PowerShell object 运行的 PowerShell 脚本获取 windows 注册表属性值?

[英]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
DirectDrawEx DirectDrawEx
Docker Desktop DisplayIcon: C:\Program Files\Docker\Docker\Docker Desktop Installer.exe Docker 桌面显示图标: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.xml ChannelUrl: https://download.docker.com/win/stable/appcast.xml
UninstallString: "C:\Program Files\Docker\Docker\Docker Desktop UninstallString: "C:\Program Files\Docker\Docker\Docker Desktop
Installer.exe" uninstall Installer.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 :如果你想要一个属性值,你需要检查返回的PSObjectProperties属性:

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.

相关问题 如何在Windows窗体应用程序中运行PowerShell命令? - How do I Run PowerShell command in windows form application? 如何使用通过.net传递的参数从Powershell调用BCP? - How do I call BCP from powershell using parameters passed in through .net? 如何在PowerShell上的JSON输出中获取特定值? - How do I get the specific values in a JSON Output on PowerShell? 如何在不使用Powershell类的情况下从C#调用powershell脚本 - How do I invoke a powershell script from C# without using the Powershell class “尝试使用模拟在远程计算机上运行PowerShell脚本时,不允许”请求注册表访问“ - “Requested registry access is not allowed” When Attempting to Run PowerShell Script on Remote Machine Using Impersonation 如何为PowerShell创建RunspaceConnectionInfo对象? - How do I Create a RunspaceConnectionInfo object for PowerShell? C#,如何在Windows Powershell(Windows 10)上配置Mono? (环境变量PATH) - C#, How Do I Get Mono Configured on Windows Powershell (Windows 10)? (Environmental Variable PATH) 如何将 wpf 文本值传递给 powershell 脚本并运行该 powershell 脚本 - How to pass wpf text value to powershell script and run that powershell script 如何将json对象作为C#中脚本的参数传递给PowerShell的实例? - How do I pass the json object to an instance of the PowerShell as a parameter to the script in C#? 如何使用 C# 在其中运行带有重命名计算机的 Powershell 脚本 - How to run a Powershell script with Rename-Computer in it using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM