Powershell 需要 UAC,我需要帮助将此函数转换为 PSexec 的 BAT 文件

Powershell Requires UAC, I need help translating this function to a BAT file for PSexec

$i=0;
$pnp = pnputil -e;$matched = [regex]::matches($pnp, ".......................................Lexmark International");
$split = $matched -split (".........inf");
$replace = $split -replace " Driver package provider :   Lexmark International","";
$replace1 = $replace -replace " ","`n";
write-output $replace1;
foreach ($i in $replace1){;
$pnpdel = pnputil -f -d $i;$pnpdel;
};
Reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3\Lexmark Universal v2 XL" /f;
net stop spooler;
net start spooler;
$PrinterPath = "\officechicprt51w-2w-bprn-07";
$net = new-Object -Com WScript.Network;
$net.AddWindowsPrinterConnection($PrinterPath)

我知道它不漂亮,但每次我都尝试过它。如果您好奇,在我们的环境中,Lexmark 驱动程序经常损坏,这实际上是 Microsoft 的问题。在注册表中,Dependent Files 是 t运行cated,因此打印机永远不会打印,通常会强制打印机出现乱码。我们发现解决此问题的唯一方法是完全删除驱动程序,并阅读我们的要点和打印驱动程序。这个脚本就是这样做的,但不幸的是需要 UAC 提升。我已经尝试将 bat 文件与 运行 一起:

@ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
timeout /t 10

但不幸的是,它给用户留下了困惑的表情和 UAC 提示。是否可以通过 bat 文件中的 PSexec 以某种方式 运行 这?我不想 运行 通过 RDP 进入数百台机器(去过那里,做过)。我更喜欢可重复的过程,这个问题在这里很流行。

再次感谢

你把事情搞得太复杂了。不要启动 PowerShell 以带参数启动 PowerShell。直接带参数启动PowerShell就可以了

powershell.exe -NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1"" -Verb RunAs

如果您需要 运行 当您的用户不是管理员组的成员时具有更高权限的 PowerShell 脚本,您应该通过 [=12] 启用 PS Remoting 和 运行 它=] 在远程主机上:

Invoke-Command -Computer 'hostA', 'hostB', ... -ScriptBlock {
  # your PowerShell code here
}