Reputation: 921
I have this script that I've made into a function so I can dot source it. The problem I'm having the
$computername
variable is not being printed when I run the script. Can anyone help me out?
param( [Parameter(Mandatory=$True)]
$ComputerName
)
function Get-InstalledApps {
$array = @()
foreach($basekey in ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')){
$remoteBaseKeyObject = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
$remoteBaseKey = $remoteBaseKeyObject.OpenSubKey($basekey)
$subKeys = $remoteBaseKey.GetSubKeyNames()
foreach($key in $subKeys){
$thisKey=$basekey+'\'+$key
$thisSubKey=$remoteBaseKeyObject.OpenSubKey($thisKey)
$psObject = New-Object PSObject
$psObject | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
$psObject | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName")) -ea SilentlyContinue
$psObject | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$psObject | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
$psObject | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
$psObject | Add-Member -MemberType NoteProperty -Name "Uninstallstring" -Value $($thisSubKey.GetValue("UninstallString"))
$array += $psObject
}
$array
}
}
Get-InstalledApps
Upvotes: 0
Views: 159
Reputation: 60918
if you save in Get-InstalledApps.ps1 file this:
param( [Parameter(Mandatory=$True)]
$ComputerName
)
$array = @()
foreach($basekey in ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')){
$remoteBaseKeyObject = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
$remoteBaseKey = $remoteBaseKeyObject.OpenSubKey($basekey)
$subKeys = $remoteBaseKey.GetSubKeyNames()
foreach($key in $subKeys){
$thisKey=$basekey+'\'+$key
$thisSubKey=$remoteBaseKeyObject.OpenSubKey($thisKey)
$psObject = New-Object PSObject
$psObject | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
$psObject | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName")) -ea SilentlyContinue
$psObject | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$psObject | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
$psObject | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
$psObject | Add-Member -MemberType NoteProperty -Name "Uninstallstring" -Value $($thisSubKey.GetValue("UninstallString"))
$array += $psObject
}
$array
}
you can call like this ( it must be in path ):
get-installedapps.ps1 -ComputerName mycomputername
and no need to dot source.
I suggest to read this https://technet.microsoft.com/en-us/magazine/hh360993.aspx if you want absolutly use functions in powershell.
Upvotes: 1