Brent S
Brent S

Reputation: 93

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 = "\\officechicprt5\111w-2w-bprn-07";
$net = new-Object -Com WScript.Network;
$net.AddWindowsPrinterConnection($PrinterPath)

I know it's not pretty, but it works every time I have tried it. In case you are curious, in our environment, Lexmark drivers corrupt frequently, which is actually a Microsoft issue. In the registry, Dependent Files is truncated, so the printer will never print, often forcing gibberish to the printer. The only way we have found to fix this is to remove the driver completely, and read our point and print driver. This script does that, but unfortunately requires UAC elevation. I have attempted a bat file to run alongside this:

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

But unfortunately it leaves the user with a confused expression and a UAC prompt. Is it possible to run this somehow through PSexec in a bat file? I do not want to run this by RDP'ing into hundreds of machines (been there, done that). I would prefer a repeatable process, this issue is a pandemic here.

Thanks again

Upvotes: 0

Views: 218

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

You're overcomplicating things. Don't start PowerShell to start PowerShell with parameters. Just start PowerShell directly with parameters.

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

If you need to run the PowerShell script with elevated privileges when your users are not members of the administrators group you should rather enable PS Remoting and run it via Invoke-Command on the remote hosts:

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

Upvotes: 1

Related Questions