How to combine runas with pnputil?


@hdmi Thanks for all your efforts! But very complicated and long.
"It takes a little bit of suffering to be beautiful" is what Italians say.

You only need to run it one time, as it only creates the new task in Task Scheduler. So, the whole idea is to just keep the task in there after that. The command to run this task is simple and short, and should work even if you're logged in with a standard user account instead of an account that belongs to the Administrators group. It's this:
schtasks /run /tn Reset-Audio

@garlin had it right the first time, the task should ideally be run as SYSTEM, as this makes it easier to avoid pitfalls like the Access is denied error that you were experiencing. Setting the requested privilege to Highest causes it to request the highest. It doesn't mean that the highest will actually be granted.

For this specific intent/purpose (i.e. running pnputil), choosing SYSTEM avoids the need to expose Credentials, and it simplifies the logic as a bonus. There exist a lot of situations where choosing SYSTEM could cause serious problems (and could even be a security risk), but this isn't one of them.

Granting Authenticated Users permission to trigger the task by modifying the task's security descriptor is what avoids the Access is denied error without having to do anything else.

This also is why the command to run the task is just schtasks /run /tn Reset-Audio.

Just in case you still don't understand why Task Scheduler is the way to go, it's because otherwise you will be greeted with the annoying UAC prompt each time before your command resets the audio device. (Disabling UAC is most definitely NOT recommended excepting only maybe if you really, REALLY know what you're doing, so...)
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
"It takes a little bit of suffering to be beautiful" is what Italians say.

I noticed you use ` -ForegroundColor Yellow`. Is that needed for this to run properly?
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude
I noticed you use ` -ForegroundColor Yellow`. Is that needed for this to run properly?
Of course not. :cool:

0843134f41cc7e423d9d3a08edb7afd0.jpg
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
Are there other non-critical parts of the script? I'm not going for Italian beauty, just American functionality :D
Not really. You could always decide to make it shorter, but doing so would only make it harder to read for anyone who doesn't eat PowerShell for breakfast. You certainly wouldn't gain any disk space from making it shorter, and also you wouldn't notice a big enough difference in speed to be able to justify the effort and time spent on it. Remember that you only need to run it one time so, why bother?
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
Not really. You could always decide to make it shorter, but doing so would only make it harder to read

"shorter" you mean remove white space but keep all the same code? I want to reduce the amount of code, not the amount of white space.
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude
this is all I need. if I can minimize or eliminate password requests in a non-admin account I'm good

Powershell:
> $deviceID = "INTELAUDIO\FUNC_01&VEN_10EC&DEV_0295&SUBSYS_1028099F&REV_1000\5&1B578A47&0&0001"
> Disable-PnpDevice -InstanceId $deviceID -Confirm:$false
> Enable-PnpDevice -InstanceId $deviceID -Confirm:$false
 

My Computer

System One

  • OS
    Windows 11
    Computer type
    Laptop
    Manufacturer/Model
    Dell Latitude
if I can minimize or eliminate password requests in a non-admin account I'm good
This is exactly what the task in Task Scheduler lets you achieve, as you can simply run it without ever getting prompted in any way. So, after you run the batch script, this is all you'll ever need: schtasks /run /tn Reset-Audio

Here is a shorter version of my batch script:
Batch:
<# ::
@echo off
"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -Command ^$p = '%~dpnx0'; Invoke-Expression ((Get-Content '%~dpnx0') -join [Environment]::NewLine); Ele
goto :eof
#>
$c = "`$p = '$($p)'; Write-Host 'Processing: ' -ForegroundColor Yellow -NoNewline; Write-Host `$p; Invoke-Expression ((Get-Content `$p) -join [Environment]::NewLine); if (`$p -ne '') { Main }"
function Adm { return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) }
function Ele {
  $x = 'Start-Process "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe" "-NoProfile -ExecutionPolicy Bypass -Command `"$c`""'
  if (-not (Adm)) { $x = $x + " -Verb RunAs" }
  Invoke-Expression ($x)
}
function Main {
  $p
  if (Adm) {
    $n = "Reset-Audio"
    $i = '"INTELAUDIO\FUNC_01&VEN_10EC&DEV_0295&SUBSYS_1028099F&REV_1000\5&1B578A47&0&0001"'
    Register-ScheduledTask -TaskName $n `
                           -Action @((New-ScheduledTaskAction -Execute "pnputil.exe" -Argument "/disable-device $i"), (New-ScheduledTaskAction -Execute "pnputil.exe" -Argument "/enable-device $i")) `
                           -Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date)) `
                           -Principal (New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest) `
                           -Settings (New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable) `
                           -Force
    $s = New-Object -ComObject Schedule.Service
    $s.Connect()
    $t = $s.GetFolder("\").GetTask($n)
    $d = $t.GetSecurityDescriptor(0xF)
    if ($d -notmatch 'A;;GRGX;;;AU') {
      $d += '(A;;GRGX;;;AU)'
      $t.SetSecurityDescriptor($d, 0)
    }
  } else { Write-Host "Couldn't elevate the script." }
  pause
}
 

My Computers

System One System Two

  • OS
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Asus TUF Gaming F16 (2024)
    CPU
    i7 13650HX
    Memory
    16GB DDR5
    Graphics Card(s)
    GeForce RTX 4060 Mobile
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    512GB SSD internal
    37TB external
    PSU
    Li-ion
    Cooling
    2× Arc Flow Fans, 4× exhaust vents, 5× heatpipes
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF
  • Operating System
    11 Home
    Computer type
    Laptop
    Manufacturer/Model
    Medion S15450
    CPU
    i5 1135G7
    Memory
    16GB DDR4
    Graphics card(s)
    Intel Iris Xe
    Sound Card
    Eastern Electric MiniMax DAC Supreme; Emotiva UMC-200; Astell & Kern AK240
    Monitor(s) Displays
    Sony Bravia XR-55X90J
    Screen Resolution
    3840×2160
    Hard Drives
    2TB SSD internal
    37TB external
    PSU
    Li-ion
    Keyboard
    Logitech K800
    Mouse
    Logitech G402
    Internet Speed
    20Mbit/s up, 250Mbit/s down
    Browser
    FF

Latest Support Threads

Back
Top Bottom