是否有安装和删除 Windows 驱动程序的 Powershell cmdlet?

Are there Powershell cmdlets that install and remove Windows drivers?

注意:对于这个问题,当我提到 "Windows drivers" 时,我指的是 .inf 和相关文件,否则可以通过右键单击 .inf 并单击 "Install" 在 Windows 资源管理器中。我不是指任何类型的 setup.exe 风格的可执行文件,它可能会安装驱动程序。


存在以下内容:

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

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

不仅没有用于此的 PowerShell cmdlet,我t seems there isn't even managed code to do it within the .Net framework(接下来基本上是将该答案翻译成 PowerShell)。

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

链接的答案显示了如何在 C# 中执行此操作。为此,powershell 我们将使用在该答案中生成的相同签名,并将其与 Add-Type cmdlet (see example 5) 一起使用以使其可用于您的脚本。

$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)

有关参数(尤其是字符串格式)的详细信息,请参阅 MSDN documentation for InstallHinfSection

我要补充你可以调用pnputil.exe命令:

pnputil /add-driver "path\to\driver\*inf" /install

有一个 PowerShell module named DeviceManagement,可通过 Install-Module 从 PowerShell 库安装,其中包含 Install-DeviceDriver cmdlet。示例:

Install-Module -Name DeviceManagement -Force
Install-DeviceDriver -InfFilePath C:\Drivers\LAN\acmelan.inf
Get-Device | Where-Object Name -like 'ACME Network Adapter*' | Select-Object Name,DriverProvider,DriverVersion,DriverDescription,HasProblem