是否有安装和删除Windows驱动程序的Powershell cmdlet? [英] Are there Powershell cmdlets that install and remove Windows drivers?

查看:481
本文介绍了是否有安装和删除Windows驱动程序的Powershell cmdlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:对于此问题,当我提到 Windows驱动程序时,是指.inf和相关文件,否则可以通过右键单击.inf并在Windows资源管理器中单击安装来安装。我并不是说有任何setup.exe样式的可执行文件都可以安装驱动程序。

那里存在以下内容:

  • Get-WindowsDriver -online - a Powershell Cmdlet that outputs the currently-installed drivers of the running system
  • Add-WindowsDriver - a Powershell Cmdlet that adds a driver to an offline image. The corresponding Remove-WindowsDriver can be used to remove a driver from an offline image.
  • dpinst.exe - a command line tool that can be used to install a driver to the running system. dpinst.exe /u can be used with the to uninstall drivers.

但是,我还没有找到相应的Powershell Cmdlet,它支持在运行系统上安装和卸载驱动程序。我敢肯定我可以将 dpinst.exe 包装在一些powershell中,但是如果存在更多的Powershell本机方法,我想避免映射命令行参数和解析输出。

I have not, however, found a corresponding Powershell Cmdlet that supports installing and uninstalling drivers on the running system. I'm sure I could wrap dpinst.exe in some powershell, but I'd like to avoid mapping command line parameters and parsing output if a more Powershell-native method exists.

是否存在在运行的系统上安装和卸载Windows驱动程序的Powershell Cmdlet?还有其他使用Powershell安装和卸载Windows驱动程序的方式,该方法不涉及 dpinst.exe

Do Powershell Cmdlets exist that install and uninstall Windows drivers on the running system? Is there some other way to install and uninstall Windows drivers using Powershell that does not involve dpinst.exe?

推荐答案

不仅没有PowerShell cmdlet,我似乎没有托管代码在.Net框架中完成此操作(基本上是将该答案转换为PowerShell)。

Not only are there not PowerShell cmdlets for this, it seems there isn't even managed code to do it within the .Net framework (what follows is basically a translation of that answer into PowerShell).

幸运的是,.Net框架可以调用Windows API通过平台调用(p / invoke),PowerShell也可以这样做。

Luckily, the .Net framework can call windows APIs through platform invoke (p/invoke), and PowerShell can do so too.

链接的答案显示了如何在C#中做到这一点。为此,我们将使用该答案中生成的签名,并将其与 添加类型 cmdlet(请参见示例5)使其可用于您的脚本。

The linked answer shows how to do it in C#. To do it powershell we'll use the same signature that was generated in that answer, and use it with the Add-Type cmdlet (see example 5) to make it available to your script.

$signature = @"
[DllImport("Setupapi.dll", EntryPoint="InstallHinfSection", CallingConvention=CallingConvention.StdCall)]
public static extern void InstallHinfSection(
    [In] IntPtr hwnd,
    [In] IntPtr ModuleHandle,
    [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer,
    int nCmdShow);
"@
$Win32Functions = Add-Type -MemberDefinition $signature -UsingNamespace System.Runtime.InteropServices -Name Win32SInstallHinfSection -Namespace Win32Functions -PassThru 

$Win32Functions::InstallHinfSection([IntPtr]::Zero, [IntPtr]::Zero, "<section> <mode> <path>", 0)

请参见有关InstallHinfSection的MSDN文档,以获取有关参数的详细信息(尤其是字符串格式)。

See the MSDN documentation for InstallHinfSection for details on the parameters (particularly the string format).

这篇关于是否有安装和删除Windows驱动程序的Powershell cmdlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆