Quantcast
Channel: Group Policy forum
Viewing all articles
Browse latest Browse all 19997

Programmatically (powershell) adding startup/shutdown scripts to a Group Policy

$
0
0

I'm attempting to automate some group policy tasks and am roadblocked now. I am on an active directory domain controller, attempting to create a new group policy and assign startup and showdown scripts to it. My code block below nearly works (I think). I am able to create the group policy, copy ls.ps1 to the startup and shutdown folders inside the group policy, and create the psscripts.ini file.

When I look at this new GPO in the Group Policy Management editor the settings do not show the proper Computer Configuration. However, if I edit this new gpo, and go to add the powershell scripts through the ui, once I reach the Startup/Shutdown menu for the powershell scripts it sees my ls.ps1 script there and updates the group policy. Without making any changes the settings now show up as expected for the Computer Configuration, ls.ps1 running on startup and shutdown. How do I trigger the gpo to read the psscripts.ini or reload itself programmatically?

I attempted to update the GPT.ini version to '1'  with the hope gpupdate would then work, but am having no luck even with gpupdate /force.

$gpo = New-GPO -Name Testgpo
Start-Sleep -s 5
$guid = $gpo.id.ToString().ToUpper()
Write-Host "Group Policy Created: $guid"
$domain = Get-ADDomain
$forest = $domain.forest

md "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\Machine\Scripts\Shutdown"
md "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\Machine\Scripts\Startup"
Copy-Item .\ls.ps1 "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\Machine\Scripts\Shutdown"
Copy-Item .\ls.ps1 "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\Machine\Scripts\Startup"

$pshellscript = @"

[Startup]
0CmdLine=ls.ps1
0Parameters=
[Shutdown]
0CmdLine=ls.ps1
0Parameters=
"@

$psfilename = "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\Machine\Scripts\psscripts.ini"
$pshellscript | Out-File $psfilename -Encoding unicode
$psfile = Get-Item $psfilename -force
$psfile.attributes="Hidden"

$gptini = @"
[General]
Version=1
displayName=New Group Policy Object"@

$gptinifilename = "C:\Windows\SYSVOL\sysvol\$forest\Policies\{$guid}\GPT.ini"
$gptini | Out-File $gptinifilename -Encoding utf8

Any suggestions appreciated.


Viewing all articles
Browse latest Browse all 19997

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>