Put whatever commands you want to run as TrustedInstaller in a text file in the same path as your script file, make sure PsExec.exe is in the Path, then use these commands:
$PwSh=(Get-Process -Id $pid).path
psexec -S $pwsh -file $psscriptroot\tiworker.txt
To run the commands that need TrustedInstaller privilege in another PowerShell process, the process will exit automatically after the execution is completed, allowing the main script execution to continue.
Fixed a minor mistake that caused the PowerShell process to start without TrustedInstaller privilege.
The above method somehow didn't work properly, as when I tried it to run these commands:
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\WinDefend" -Name "Start" -Type DWord -Value 4
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\WdBoot" -Name "Start" -Type DWord -Value 4
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\WdFilter" -Name "Start" -Type DWord -Value 4
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\WdNisDrv" -Name "Start" -Type DWord -Value 4
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\WdNisSvc" -Name "Start" -Type DWord -Value 4
I got the error:
Set-ItemProperty: Attempted to perform an unauthorized operation.
If I use reg add
ERROR: Access is denied.
However the commands before them all returned:
The operation completed successfully.
Specifically these commands:
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender" -Name "DisableAntiSpyware" -Type DWord -Value 1
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender" -Name "DisableRoutinelyTakingAction" -Type DWord -Value 1
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender" -Name "ProductStatus" -Type DWord -Value 0
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\Real-Time Protection" -Name "DisableAntiSpywareRealtimeProtection" -Type DWord -Value 1
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\Real-Time Protection" -Name "DisableRealtimeMonitoring" -Type DWord -Value 1
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\Scan" -Name "AutomaticallyCleanAfterScan" -Type DWord -Value 0
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\Scan" -Name "ScheduleDay" -Type DWord -Value 8
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\UX Configuration" -Name "AllowNonAdminFunctionality" -Type DWord -Value 0
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows Defender\UX Configuration" -Name "DisablePrivacyMode" -Type DWord -Value 1
When run as Administrator and not TrustedInstaller, each will return this error:
Set-ItemProperty: Requested registry access is not allowed.
When run using PsExec, this error will not be generated.
But this error:
Set-ItemProperty: Attempted to perform an unauthorized operation.
Will still be generated.
I guess this is because PsExec relies on remote stuff and I have disabled "Remote Assistance", "Remote Desktop" and "Remote Registry";
I used NSudoLC.exe and it successfully disabled Windows Defender without errors:
NSudoLC.exe -U:T -P:E $pwsh -file $home\desktop\tisvc.txt
When using NSudo both the errors mentioned above are not generated.
get-contentto read from it in your other session. Wam, bam! – Vomit IT - Chunky Mess Style Jan 24 '21 at 03:37SYSTEM"whether is logged on or not", and then execute that task scheduler job and NOT use the PSExec and then the rest is trivial and PowerShell. That's my thought but I'm trying to play with CentOS8 on WSL a little but someone with your skillset with PowerShell, you can most certainly figure that out. I'm sure Keith Miller or one of the other PS experts have some methods too. I say if it time to expand your PS and use Task Scheduler and like all other things I've mentioned. – Vomit IT - Chunky Mess Style Jan 24 '21 at 03:461.Try to not use PS aliases (e.g.%) with 3rd party tool and2.make sure it's a valid value array defined iteration of the loop passed i.e."PrintWorkflowUserSvc","RmSvc","SCardSvr","SecurityHealthService","Sense","SgrmBroker","wscsvc" | ForEach-Object { $_ };– Vomit IT - Chunky Mess Style Jan 24 '21 at 04:06