Hello Everyone!
This is my first post so let me know if I am doing anything incorrectly. I am trying to find all of the applications installed on the servers in my domain. I usually would use the Get-WmiObject cmdlet but apparently this is BAD as it kicks off a consistency check against the application being queried. I ran across this article from scriptingguy using registry key values to find installed application on a remote server.
I created a csv file some of my remote servers and try to run the script but it only shows me applications that are installed on my local computer. If I change the value $computers to use a static value instead of importing a .csv file I still get a list of installs from my local computer.
ie: "$computers = "dc01.domain.com"
The only way I have gotten the script to query a remote server is to change the line "$computername=$pc.computername" to "$computername=$pc" and leave computer name off.
I guess the question I have is why does my script not work on the remote servers listed in the .csv. Also could someone please explain what is happening in the following block of code. I am unsure what the variable $pc is in $computers and why it is referenced in the nest line?
{foreach($pc in $computers){
$computername=$pc.computername}
$computers = Import-Csv "C:\PowerShell_Scripts\servers_rivetsw.csv"
$array = @()
foreach($pc in $computers){
$computername=$pc.computername
#Define the variable to hold the location of Currently Installed Programs
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
#Create an instance of the Registry Object and open the HKLM base key
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
#Drill down into the Uninstall key using the OpenSubKey Method
$regkey=$reg.OpenSubKey($UninstallKey)
#Retrieve an array of string that contain all the subkey names
$subkeys=$regkey.GetSubKeyNames()
#Open each Subkey and use GetValue Method to return the required values for each
foreach($key in $subkeys){
$thisKey=$UninstallKey+"\\"+$key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
$obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
$array += $obj
}
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher | ft -auto
--------------------------------
I am a scripting newbie so any help would be awesome to have. Heck any resources that you all can throw my way would also be great especially about for loops!
Regards,
Antony