• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

Powershell Remote Registry Help!

Can someone help me integrate a distribution list with this script?

All of this works just how I want it when I input a servername in the XXXXX or run it locally but I want to use a distribution list with this. I was helped the other day and that was great. just  ran into a snag and i just don't know the syntax to do it.
I have text files with lists of server names.
#>
	[CmdletBinding(SupportsShouldProcess=$true)]
	param
	(
		[Parameter(Position=0, Mandatory=$false)]
		[System.String]
		$Server = "xxxxxxx",
		[Parameter(Position=1, Mandatory=$false)]
		[ValidateSet("ClassesRoot","CurrentConfig","CurrentUser","DynData","LocalMachine","PerformanceData","Users")]
		[System.String]
		$Hive = "LocalMachine",
		[Parameter(Position=2, Mandatory=$false, HelpMessage="Enter Registry key in format System\CurrentControlSet\Services")]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$Key = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
		[Parameter(Position=3, Mandatory=$false)]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$Name = "BGInfo",
		[Parameter(Position=4, Mandatory=$false)]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$Value = "C:\support\bginfo\Bginfo.exe C:\support\bginfo\standard.bgi /TIMER:0 /silent /NOLICPROMPT",
		[Parameter(Position=5, Mandatory=$false)]
		[ValidateSet("String","ExpandString","Binary","DWord","MultiString","QWord")]
		[System.String]
		$Type = "String",
		[Parameter(Position=6, Mandatory=$false)]
		[Switch]
		$Force
	)
	
	If ($pscmdlet.ShouldProcess($Server, "Open registry $Hive"))
	{
	#Open remote registry
	try
	{
			$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive, $Server)
		
	}
	catch 
	{
		Write-Error "The computer $Server is inaccessible. Please check computer name. Please ensure remote registry service is running and you have administrative access to $Server."
		Return
	}
	}

	If ($pscmdlet.ShouldProcess($Server, "Check existense of $Key"))
	{
	#Open the targeted remote registry key/subkey as read/write
	$regKey = $reg.OpenSubKey($Key,$true)
		
	#Since trying to open a regkey doesn't error for non-existent key, let's sanity check
	#Create subkey if parent exists. If not, exit.
	If ($regkey -eq $null)
	{	
		Write-Warning "Specified key $Key does not exist in $Hive."
		$Key -match ".*\x5C" | Out-Null
		$parentKey = $matches[0]
		$Key -match ".*\x5C(\w*\z)" | Out-Null
		$childKey = $matches[1]

		try
		{
			$regtemp = $reg.OpenSubKey($parentKey,$true)
		}
		catch
		{
			Write-Error "$parentKey doesn't exist in $Hive or you don't have access to it. Exiting."
			Return
		}
		If ($regtemp -ne $null)
		{
			Write-Output "$parentKey exists. Creating $childKey in $parentKey."
			try
			{
				$regtemp.CreateSubKey($childKey) | Out-Null
			}
			catch 
			{
				Write-Error "Could not create $childKey in $parentKey. You  may not have permission. Exiting."
				Return
			}

			$regKey = $reg.OpenSubKey($Key,$true)
		}
		else
		{
			Write-Error "$parentKey doesn't exist. Exiting."
			Return
		}
	}
	
	#Cleanup temp operations
	try
	{
		$regtemp.close()
		Remove-Variable $regtemp,$parentKey,$childKey
	}
	catch
	{
		#Nothing to do here. Just suppressing the error if $regtemp was null
	}
	}
	#If we got this far, we have the key, create or update values
	If ($Force)
	{
		If ($pscmdlet.ShouldProcess($ComputerName, "Create or change $Name's value to $Value in $Key. Since -Force is in use, no confirmation needed from user"))
		{
			$regKey.Setvalue("$Name", "$Value", "$Type")
		}
	}
	else
	{
		If ($pscmdlet.ShouldProcess($ComputerName, "Create or change $Name's value to $Value in $Key. No -Force specified, user will be asked for confirmation"))
		{
		$message = "Value of $Name will be set to $Value. Current value `(If any`) will be replaced. Do you want to proceed?"
		$regKey.Setvalue("$Name", "$Value", "$Type")
		}
	}
	
	#Cleanup all variables
	try
	{
		$regKey.close()
		Remove-Variable $Server,$Hive,$Key,$Name,$Value,$Force,$reg,$regKey,$yes,$no,$caption,$message,$result
	}
	catch
	{
		#Nothing to do here. Just suppressing the error if any variable is null
	}

Open in new window


I knew how to do this with the new-item registry key creator in powershell except that remote powershell sessions are not enabled on just about anything on our network >_<.

Thank you
0
Tsz109
Asked:
Tsz109
1 Solution
 
QlemoC++ DeveloperCommented:
Since the code starts with a closing multi-line comment tag, is there something important missing? Like a function definition, maybe?

Anyway, you can call that script or function by using something along the lines of
get-content YourTextFile.txt | foreach-object {ScriptOrFunction $_}

Open in new window

That does not need any modification on your code.
0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now