Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Unable to register Windows Shutdown Script via GPO using Powershell

$
0
0

Hi,

I'm trying to register a Windows 2012 Server shutdown powershell script using  powershell to modify the registry & create the psscripts.ini file, but the shutdown script does not run. The script below is called during booting of an AWS instance. The $scriptPath parameter contains the name of another powershell script file that contains the actual shutdown script and the $parameters parameter contains a string the shutdown script expects. 

If I manually register the shutdown script using gpedit.msc and then shutdown the instance then the shutdown script executes as expected. I did a visual comparision of the register when I run my register code and when I use gpedit and they look the same at least in the scripts path. I also found that unless I created the psscripts.ini file the entry would not appear in the gpedit. Just to be clear, after I run my register code, if I open gpedit.msc, I see the same entry as if I entered in gpedit directly, yet the script does not run. 

If there's a way to register the script using powershell using some GPO object, please let me know where to find info on it as I'd rather not be writing to the registry directly. This is  running on an AWS virtual machine I control, so I know there will not be any other scripts registered (that I would be clobbering with the code below)

function RegisterWindowsShutdownScript([string]$regionName, [string]$scriptPath, [string]$parameters)
{
	Write-Debug "RegisterWindowsShutdownScript([string]$regionName, [string]$scriptPath, [string]$parameters)"
	Set-DefaultAWSRegion $regionName
	$key = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Shutdown\0'
	New-Item -Path $key -Force
	New-ItemProperty -Path $key -Name GPO-ID -Value LocalGPO -Force
	New-ItemProperty -Path $key -Name SOM-ID -Value Local -Force
	New-ItemProperty -Path $key -Name FileSysPath -Value "C:\Windows\System32\GroupPolicy\Machine" -Force
	New-ItemProperty -Path $key -Name DisplayName -Value "Local Group Policy" -Force
	New-ItemProperty -Path $key -Name GPOName -Value "Local Group Policy" -Force
	New-ItemProperty -Path $key -Name PSScriptOrder -Value 1 -PropertyType "DWord" -Force

	$key = "$key\0"
	New-Item -Path $key -Force
	New-ItemProperty -Path $key -Name "Script" -Value $scriptPath -Force
	New-ItemProperty -Path $key -Name "Parameters" -Value $parameters -Force
	New-ItemProperty -Path $key -Name "IsPowershell" -Value 1 -PropertyType "DWord" -Force
	New-ItemProperty -Path $key -Name "ExecTime" -Value 0 -PropertyType "QWord" -Force
	$psScriptsFile = "C:\Windows\System32\GroupPolicy\Machine\Scripts\psscripts.ini"
	New-Item $psScriptsFile -type file -force"[Shutdown]" | Out-File $psScriptsFile "0CmdLine=$scriptPath" | Out-File $psScriptsFile -Append"0Parameters=$parameters" | Out-File $psScriptsFile -Append
}

Shutdown Powershell script:

param([string]$regionName, [string]$stackName)"param([string]$regionName, [string]$stackName)" | Add-Content 'c:\cfn\log\remove.txt'
Write-S3Object -BucketName MyCompany.CFTemplates-RDS/1.2/Database -Key remove.txt -File 'c:\cfn\log\remove.txt'	

Set-DefaultAWSRegion $regionName

$stackInfo = Get-CFNStack -StackName $stackName

$stackInfo | Add-Content 'c:\cfn\log\remove.txt'
$stackInfo.StackStatus | Add-Content 'c:\cfn\log\remove.txt'

Write-S3Object -BucketName MyCompany.CFTemplates-RDS/1.2/Database -Key remove.txt -File 'c:\cfn\log\remove.txt'	

if ($stackName.StackStatus -ieq 'DELETE_IN_PROGRESS')
{
	$optionGroupName = $stackName+"-OptionGroup"
	Write-Output "Stack deletion detected; deleting $optionGroupName""Stack deletion detected; deleting $optionGroupName" | Add-Content 'c:\cfn\log\remove.txt'
	Write-S3Object -BucketName MyCompany.CFTemplates-RDS/1.2/Database -Key remove.txt -File 'c:\cfn\log\remove.txt'	
	try {
		Remove-RDSOptionGroup `
		-Region $regionName `
		-OptionGroupName $optionGroupName `
		-Force
	} catch [Exception] {
		Write-Output $_.Exception.GetType().FullName; 
		Write-Output $_.Exception.Message; 
	}
}	
else
{
	Write-Output 'Instance terminating without stack'
	'Instance terminating without stack'| Add-Content 'c:\cfn\log\remove.txt'
	Write-S3Object -BucketName MyCompany.CFTemplates-RDS/1.2/Database -Key remove.txt -File 'c:\cfn\log\remove.txt'	
}





Viewing all articles
Browse latest Browse all 15028

Trending Articles



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