注意:对于这个问题,当我提到“Windows 驱动程序”时,我指的是 .inf 和相关文件,否则可以通过右键单击 .inf 并单击 Windows 资源管理器中的“安装”来安装这些文件。我指的不是任何一种可能会安装驱动程序的 setup.exe 样式的可执行文件。
存在以下内容:
-
Get-WindowsDriver -online
- 一个 Powershell Cmdlet,它输出正在运行的系统的当前安装的驱动程序 -
Add-WindowsDriver
- 将驱动程序添加到离线镜像 的 Powershell Cmdlet。对应的Remove-WindowsDriver
可用于从离线镜像中删除驱动程序。 -
dpinst.exe
- can be used to install a driver to the running system 的命令行工具.dpinst.exe/u
可与卸载驱动程序一起使用。
但是,我还没有找到支持在正在运行的系统 上安装和卸载驱动程序的相应 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# 中执行此操作。为此,我们将使用在该答案中生成的相同签名,并将其与 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有关参数的详细信息(特别是字符串格式)。
关于windows - 是否有安装和删除 Windows 驱动程序的 Powershell cmdlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320013/