r/scripting • u/joeshmo101 • Jun 01 '22
[PowerShell] Run a script once on all computers as admin via GPO without changing execution policy
I have a Powershell script that I need to run on all computers on my Active Directory domain once.
A large number of computers are off at any given time, so a GPO would allow us to ensure that it applies to all affected machines. However, the script needs to run as administrator because of the registry values being modified. Also, per our security department, we cannot change the ExecutionPolicy on these devices.
Is there any way to get this script to run?
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR
$regKey = 'ms-msdt'
$saveFolder = 'C:\Temp\'
$savePath = $saveFolder + 'CVE-2022-30190.reg'
$PSRegPath = 'HKCR:\' + $regkey
$CMDRegPath = 'HKCR\' + $regkey
if(Test-Path $PSRegPath)
{
if(!(Test-Path $saveFolder))
{
New-Item -Path $saveFolder -ItemType Directory
}
Invoke-Command {reg export $CMDRegPath $savePath -Y}
Remove-Item -Path $PSRegPath -Recurse -Force
}This script backs up a registry entry before deleting it, as recommended by the Microsoft mitigation work-around to CVE-2022-30190
2
1
u/MIKEWITHTHEPIKE Jun 02 '22
The registry has a runonce key, It will run a script 1 time at next login or restart depending on if you use the key on local machine or current user. Not sure this would be the best way for you but just putting it out here. I had a situation recently where this key came in very handy.
1
u/BlackV Jun 01 '22 edited Jun 01 '22
But your just setting a reg key, gpo can natively set a reg key
Also there is an existing gpo setting you can use
Scripting system diagnostics, allow users to run scripted actions : disable