Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Null-valued expression after starting RemoteRegistry service

$
0
0

Got a workaround in place, but still no idea why it wasn’t working originally…

function OpenRemoteRegistryKey {
	Param(
		[Parameter(ValueFromPipelineByPropertyName=$false,Mandatory=$true)][string]$ComputerName,
		[int]$Iteration = 1
	)
	
	$reg = $null
	try {
		#Create an instance of the Registry Object and open the HKLM base key
		$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$ComputerName)
	}
	catch [Exception] {
		if ($_.Exception.Message -like "*The network path was not found*")
		{
			if ($Iteration -ge 3) {
				Write-Error "Unable to start RemoteRegistry service on $ComputerName"
				$reg = $null
			}
			else {
				# Try to start the RemoteRegistry service on the remote machine
				# http://www.sqlservercentral.com/blogs/timradney/2011/07/18/starting-and-stopping-a-remote-service-with-powershell/
				$response = (Get-WmiObject -ComputerName $ComputerName -Class win32_service -filter "Name='RemoteRegistry'").StartService()
				$Iteration++
				switch ($response.ReturnValue) {
					# Service started successfully
					0 {
						Start-Sleep -Seconds 10
						OpenRemoteRegistryKey -ComputerName $ComputerName -Iteration $Iteration
					}
					# Service was already running
					10 {
						# Stop the RemoteRegistry service, wait 30 seconds, and retry
						$response = (Get-WmiObject -ComputerName $ComputerName -Class win32_service -filter "Name='RemoteRegistry'").StopService()
						Start-Sleep -Seconds 10
						OpenRemoteRegistryKey -ComputerName $ComputerName -Iteration $Iteration
					}
					default {
						Write-Error "Unable to start RemoteRegistry service on $ComputerName. ReturnValue: $($response.ReturnValue)"
						$reg = $null
					}
				}
			}
		}
		else {
			Write-Error "$ComputerName : $_"
			$reg = $null
		}
	}
	finally {
		$reg
	}
}

function Get-InstalledSoftware {
	[CmdletBinding()]
	Param (
		[Parameter(ValueFromPipelineByPropertyName=$true,Position=0,Mandatory=$true)][string[]]$ComputerName
	)

	BEGIN {
	}
	PROCESS {
		$installedApps = @()
		foreach($comp in $ComputerName){
			#Define the variable to hold the location of Currently Installed Programs
			$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" 
			$reg = OpenRemoteRegistryKey -ComputerName $comp
			$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$ComputerName)
			if($reg) {
				#Drill down into the Uninstall key using the OpenSubKey Method and retrieve an array of strings that contain all the subkey names
				$subkeys = $reg.OpenSubKey($UninstallKey).GetSubKeyNames()
				#Open each Subkey and use GetValue Method to return the required values for each
				foreach($key in $subkeys){
					$thisSubKey = $reg.OpenSubKey($UninstallKey+"\\"+$key)
					$InstallDate = $($thisSubKey.GetValue("InstallDate"))
					if (($InstallDate -ne $null) -and ($InstallDate.Length -gt 0)) {
						$InstallDate = [datetime]::ParseExact($($thisSubKey.GetValue("InstallDate")), "yyyyMMdd", $null)
					}
					$obj = New-Object -TypeName PSObject -Property @{
						ComputerName=$comp
						DisplayName=$($thisSubKey.GetValue("DisplayName"))
						DisplayVersion=$($thisSubKey.GetValue("DisplayVersion"))
						InstallLocation=$($thisSubKey.GetValue("InstallLocation"))
						Publisher=$($thisSubKey.GetValue("Publisher"))
						InstallDate=$InstallDate
					}
					$installedApps += $obj
				}
			}			
		}
		$installedApps | Where-Object DisplayName | Select-Object ComputerName, DisplayName, DisplayVersion, InstallLocation, Publisher, InstallDate
	}
	END {
	}
}

Viewing all articles
Browse latest Browse all 13067

Trending Articles