How to Capture Cluster info Remotely from a non-Clustered Node - powershell

I've been banging my head on this for a few days now, and I just can't figure out the best way to do it. I've got a script where I collect a bunch of data and output it to an html file (using PSWriteHTML Module, which is where the New-HTMLTable at the end comes from).
I've piecemealed the script together over time so I can gather the data from multiple servers at once, and for the most part, it all works great. As I've added data to the script to collect new info, there's a few parts that I just can't get to work right remotely. I know the piecemeal approach has left me with some redundant code, but I'm just trying to make it all work right before I re-write it again to clean it up, so my apologies for its current state.
The following code works great when I run the script from a server in a Windows Cluster, but I want things to work from any server, not necessarily a Cluster Node.
Here's orig code for this section:
try
{
$ClusterIPInfo = Invoke-command -computer $Computer {
Get-Cluster | Get-ClusterResource | %{
$_ | select Name,
#{ Name = "Address"; Expression = { $_ | Get-ClusterParameter -Name Address -ErrorAction SilentlyContinue | select -ExpandProperty Value } },
#{ Name = "SubnetMask"; Expression = { $_ | Get-ClusterParameter -Name SubnetMask -ErrorAction SilentlyContinue | select -ExpandProperty Value } }
}
} | Select -Property * -ExcludeProperty PSComputerName, RunSpaceID, PSShowComputerName
$ClusterResourceInfo = Invoke-command -computer $Computer {
Get-ClusterResource | Select Cluster, Name, State, ResourceType, OwnerGroup, OwnerNode, ID, IsCoreResource, IsNetworkClassResource, IsStorageClassResource | Sort-Object -Property OwnerGroup, Name
} | Select -Property * -ExcludeProperty PSComputerName, RunSpaceID, PSShowComputerName
$ResourceInfo = #()
foreach ($rec in $ClusterResourceInfo)
{
$Owner = (Get-ClusterResource | Sort-Object -Property OwnerGroup, Name | Get-ClusterOwnerNode | %{
$_ | select #{ Name = "Name"; Expression = { $_.ClusterObject } },
#{ Name = "PossibleOwners"; Expression = { $_.OwnerNodes } }
} | Where { $_.Name -eq $rec.Name }).PossibleOwners
$Dependency = (Get-ClusterResource | Sort-Object -Property OwnerGroup, Name | Get-ClusterResourceDependency | %{
$_ | select #{ Name = "Name"; Expression = { $_.Resource } },
#{ Name = "Dependency"; Expression = { $_ | Select-Object -ExpandProperty "DependencyExpression" } }
} | Where { $_.Name -eq $rec.Name }).Dependency
$address = ($ClusterIPInfo | Where { $_.Name -eq $rec.Name }).Address
$subnetmask = ($ClusterIPInfo | Where { $_.Name -eq $rec.Name }).SubnetMask
$recObj = New-Object PSObject
$recObj | Add-Member NoteProperty -Name "Cluster" -Value $rec.Cluster
$recObj | Add-Member NoteProperty -Name "Name" -Value $rec.Name
$recObj | Add-Member NoteProperty -Name "State" -Value $rec.State
$recObj | Add-Member NoteProperty -Name "Resource Type" -Value $rec.ResourceType
$recObj | Add-Member NoteProperty -Name "Owner Group" -Value $rec.OwnerGroup
$recObj | Add-Member NoteProperty -Name "Owner Node" -Value $rec.OwnerNode
$recObj | Add-Member NoteProperty -Name "Possible Owners" -Value $Owner
$recObj | Add-Member NoteProperty -Name "Dependency" -Value $Dependency
$recObj | Add-Member NoteProperty -Name "IP Address" -Value $address
$recObj | Add-Member NoteProperty -Name "Subnet Mask" -Value $subnetmask
$recObj | Add-Member NoteProperty -Name "Is Core Resource" -Value $rec.IsCoreResource
$recObj | Add-Member NoteProperty -Name "Is Network Resource" -Value $rec.IsNetworkClassResource
$recObj | Add-Member NoteProperty -Name "Is Storage Resource" -Value $rec.IsStorageClassResource
$ResourceInfo += $recObj
}
New-HTMLTable -DataTable $ResourceInfo -HideFooter -HideButtons -DisableSearch
The parts that don't work correctly remotely are the Dependency and PossibleOwners. I know the reason it doesn't work is because when the server running the script isn't a Cluster Node, it doesn't recognize the command under the Foreach loop Get-ClusterResource. But I just can't figure out how to make those pass correctly from within the Foreach loop, but still use the info from $ClusterResourceInfo.
I've re-written this a hundred different ways, i.e. make a single Invoke-command with basically one big Get-Cluster variable (couldn't get it to capture the Dependency/PossOwners, always $null), splitting up the Dependency and PossOwners to their own separate Invoke-Command (best I can get it to do is display System.Object[], or when I did get it to display, it captured ALL of the Dependencies for all objects and displayed on every line instead of splitting it up correctly).
I've tried every possible way I can think of or found online, but just can't get it to work correctly remotely.
Here's how the orig code above outputs (which is what I want, but I just want to fix it so it works remotely):
I am desperately hoping for some brilliance or guidance to set me on the right track. I tried so many ways, but just never quite got it where it needs to be, so any help is most appreciated and welcome. Thanks.

Couple of things i can suggest.
The "Get-ClusterResource" cmdlet fails because it is not installed on the server.
You may try to load the Failover cluster module using Import-Module, and if it fails (on a non-cluster Node), you can add the Failover Cluster Module for Windows PowerShell Feature, using the following PowerShell cmd:
Add-WindowsFeature RSAT-Clustering-PowerShell
You may try connecting to to the remote cluster node where the resource is hosted, using WMI ?
You have enough info about the resource to be able to write a filtered WMI query.

So the piecemeal approach is what got me in trouble. In trying to merge things together, I kept breaking it (mainly because I think had doubled up the %{}). So instead of merging, I just replicated the parts that were already working as intended.
Ultimately this code worked fine:
$ClusterInfo = Invoke-command -computer $Computer {
Get-Cluster | Get-ClusterResource | %{
$_ | select Name,
#{ Name = "Address"; Expression = { $_ | Get-ClusterParameter -Name Address -ErrorAction SilentlyContinue | select -ExpandProperty Value } },
#{ Name = "SubnetMask"; Expression = { $_ | Get-ClusterParameter -Name SubnetMask -ErrorAction SilentlyContinue | select -ExpandProperty Value } },
#{ Name = "PossibleOwners"; Expression = { $_ | Get-ClusterOwnerNode | select OwnerNodes | select -ExpandProperty OwnerNodes } },
#{ Name = "Dependency"; Expression = { $_ | Get-ClusterResourceDependency | select -ExpandProperty "DependencyExpression" } }
}
} | Select -Property * -ExcludeProperty PSComputerName, RunSpaceID, PSShowComputerName
$ClusterResourceInfo = Invoke-command -computer $Computer {
Get-ClusterResource | Select Cluster, Name, State, ResourceType, OwnerGroup, OwnerNode, IsCoreResource, IsNetworkClassResource, IsStorageClassResource | Sort-Object -Property OwnerGroup, Name
} | Select -Property * -ExcludeProperty PSComputerName, RunSpaceID, PSShowComputerName
$ResourceInfo = #()
foreach ($rec in $ClusterResourceInfo)
{
$Owner = ($ClusterInfo | Where { $_.Name -eq $rec.Name }).PossibleOwners
$Dependency = ($ClusterInfo | Where { $_.Name -eq $rec.Name }).Dependency
$address = ($ClusterInfo | Where { $_.Name -eq $rec.Name }).Address
$subnetmask = ($ClusterInfo | Where { $_.Name -eq $rec.Name }).SubnetMask
$recObj = New-Object PSObject
$recObj | Add-Member NoteProperty -Name "Cluster" -Value $rec.Cluster
$recObj | Add-Member NoteProperty -Name "Name" -Value $rec.Name
$recObj | Add-Member NoteProperty -Name "State" -Value $rec.State
$recObj | Add-Member NoteProperty -Name "Resource Type" -Value $rec.ResourceType
$recObj | Add-Member NoteProperty -Name "Owner Group" -Value $rec.OwnerGroup
$recObj | Add-Member NoteProperty -Name "Owner Node" -Value $rec.OwnerNode
$recObj | Add-Member NoteProperty -Name "Possible Owners" -Value $Owner
$recObj | Add-Member NoteProperty -Name "Dependency" -Value $Dependency
$recObj | Add-Member NoteProperty -Name "IP Address" -Value $address
$recObj | Add-Member NoteProperty -Name "Subnet Mask" -Value $subnetmask
$recObj | Add-Member NoteProperty -Name "Is Core Resource" -Value $rec.IsCoreResource
$recObj | Add-Member NoteProperty -Name "Is Network Resource" -Value $rec.IsNetworkClassResource
$recObj | Add-Member NoteProperty -Name "Is Storage Resource" -Value $rec.IsStorageClassResource
$ResourceInfo += $recObj
}
New-HTMLTable -DataTable $ResourceInfo -HideFooter -HideButtons -DisableSearch

Related

PowerShell Hyper-V Add network elements to VM object

We're gathering system information for a fairly large installation including Hyper-V virtual machines. As part of that effort, we're inventorying the attributes of the VMs, and we'd like to associate the IP address (or first IP address) with the VM.
To that end, we have the following script, which details the items we want to collect. Then, for each element of the group, we would like to add the (first) network adapter information. However, the Add-Member that you see in the middle part of this script seems to have no effect; the script outputs the same whether we include that ForEach or omit it, and I don't understand why. Is it not valid to use Add-Member for this sort of object (there is no error, though...)? Can you point out where this script might be going wrong?
$VMs = Get-VM `
| Select #{Name='Hostname';Expression={$(hostname)}} `
, VMName, VMId, Id `
, AutomaticStartAction, Uptime, OperationalStatus `
, PrimaryOperationalStatus, Status, ReplicationHealth, ReplicationState `
, CheckpointType, VirtualMachineType, VirtualMachineSubType `
, State, HardDrives, MemoryMaximum, MemoryMinimum,MemoryStartup `
, ProcessorCount, Path,SizeOfSystemFiles, ReplicationMode `
, ResourceMeteringEnabled, Version, FibreChannelHostBusAdapters `
, DynamicMemoryEnabled, CreationTime, IsDeleted
ForEach ($VM in $VMs) {
$Adapters=($VM | Get-VMNetworkAdapter)
$firstAdapter = "Yes"
ForEach ($Adapter in $Adapters) {
if ($firstAdapter -eq "Yes") { <# Prevent fail if more than one adapter is present. #>
$ip4 = $Adapter.IPAddresses[0]
$ip6 = $Adapter.IPAddresses[1]
$VM | Add-Member -MemberType NoteProperty -Name Adapter -value $Adapter.Name
$VM | Add-Member -MemberType NoteProperty -Name IPAddress -value $Adapter.IPAddresses[0]
$VM | Add-Member -MemberType NoteProperty -Name IP6 -value $Adapter.IPAddresses[1]
$firstAdapter = "No"
}
}
}
$VMs | ConvertTo-Csv -NoTypeInformation `
| Set-Content -Path \path\to\myfile.csv
By the way, if my newbie-ness shows from the use of ForEach() with a test to ignore all but the first... I'd love to see suggestions for a better way.
Thanks for your time.
This required a restructure of the script. Since Network Adapter information was not passed into Get-VMNetworkAdapter, it produced no output.
This script works:
$VMs = Get-VM
ForEach ($VM in $VMs) {
$Adapters = $VM | Get-VMNetworkAdapter
$firstAdapter = "Yes"
ForEach ($Adapter in $Adapters) {
if ($firstAdapter -eq "Yes") { <# Prevent fail if more than one adapter has IP4. #>
if (-not $Adapter.IPAddresses -eq $null) {
}
else {
$ip4 = $Adapter.IPAddresses[0]
$ip6 = $Adapter.IPAddresses[1]
if (-not [string]::IsNullOrEmpty($ip4) ) {
$VM | Add-Member -MemberType NoteProperty -Name NetAdapter -value $Adapter.Name
$VM | Add-Member -MemberType NoteProperty -Name IPAddress -value $ip4
$VM | Add-Member -MemberType NoteProperty -Name IP6 -value $ip6
$firstAdapter = "No"
}
}
}
}
}
$VMS | Select #{Name='Hostname';Expression={$(hostname)}} `
, VMName, VMId, Id `
, NetAdapter, IPAddress, IP6 `
, AutomaticStartAction, Uptime, OperationalStatus `
, PrimaryOperationalStatus, Status, ReplicationHealth, ReplicationState `
, CheckpointType, VirtualMachineType, VirtualMachineSubType `
, State, HardDrives, MemoryMaximum, MemoryMinimum,MemoryStartup `
, ProcessorCount, Path,SizeOfSystemFiles, ReplicationMode `
, ResourceMeteringEnabled, Version, FibreChannelHostBusAdapters `
, DynamicMemoryEnabled, CreationTime, IsDeleted `
| Select * -ExcludeProperty PSComputerName `
| Select * -ExcludeProperty ComputerName `
| ConvertTo-Csv -NoTypeInformation `
| Set-Content -Path \\gotprmry07\PerfLogs\SystemInfo\$(hostname)-VM-details.csv

Powershell script calendar size issue

Trying to run the following script to get calendar size in Exchange 2016:
# Get-MailboxFolderSize.ps1 edit as required for folder you need stats on.
#$mailboxes = #(Get-Mailbox -ResultSize Unlimited)
$mailboxes = #(Get-MailboxServer | where-object {$_.AdminDisplayVersion.Major -eq 15} | Get-Mailbox -ResultSize Unlimited)
#$mailboxes = #(Get-MailboxServer | where-object {$_.AdminDisplayVersion.Major -eq 15} | Get-Mailbox)
#$mailboxes = #(Get-Mailbox hatfiemh)
$report = #()
foreach ($mailbox in $mailboxes)
{
$inboxstats = Get-MailboxFolderStatistics $mailbox -FolderScope Calendar | Where {$_.FolderPath -eq "/Calendar"}
$mbObj = New-Object PSObject
$mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
$mbObj | Add-Member -MemberType NoteProperty -Name "Calendar Size (gb)" -Value $inboxstats.FolderandSubFolderSize.ToGB()
$mbObj | Add-Member -MemberType NoteProperty -Name "Calendar Items" -Value $inboxstats.ItemsinFolderandSubfolders
$report += $mbObj
}
$report
I get the following error message:
You cannot call a method on a Null-valued expression.
at c:\Get-MailboxFolderSize.psi:13 char:5
$mbObj | Add-Member -MemberType NoteProperty -Name "inbox Size

Issue with foreach loop (Combining Commands)

The script below works out great for identifying licensing for each individual host across multiple vCenters. What I am trying to include is the tag for each host as well. When I run the command individually it works fine, however when I run it as part of the code it is not functioning correctly. I highlighted the section if anyone can please take a look thanks. The line of code with the issue is commented out within the script below.
I attempted pushing this into a variable outside and insideof the foreach loop but I am receiving either 0 output, or the same output across each object.
Below is the actual command I put inside the foreach loop which is not functional.
(Get-VMhost | where{$_.Category -like "*Host*"})
$sw = [Diagnostics.Stopwatch]::StartNew()
# Declare our list of vCenters
[array]$vclistall = "vcenter01"
# Ensure were not connected to any vcenters
if ($DefaultVIServer.Count -gt 0) {
Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Force:$true > $null
}
[array]$report = $null
foreach ($ScriptVCInstance in $vclistall) {
$connection = Connect-VIServer $ScriptVCInstance -ErrorAction SilentlyContinue
if ($connection) {
Write-Host "Collecting License Assets on vCenter $($ScriptVCInstance)"
# Get the license manager assets
$LicenseManager = Get-view LicenseManager
$LicenseAssignmentManager = Get-View $LicenseManager.LicenseAssignmentManager
$licenses = $LicenseAssignmentManager.GetType().GetMethod("QueryAssignedLicenses").Invoke($LicenseAssignmentManager, #($null))
#Format the asset into an object
foreach ($license in $Licenses) {
$object = New-Object -TypeName PSObject
$object | Add-Member -MemberType NoteProperty -Name "vCenter" -Value $($connection.name)
$object | Add-Member -MemberType NoteProperty -Name "Entity" -Value $($license.EntityDisplayName)
$object | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $($license.Properties | where{$_.Key -eq 'ProductName'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Product Version" -Calue $($License.Properties | where{$_.Key -eq 'FileVersion'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "License" -Value $($license.AssignedLicense.LicenseKey)
$object | Add-Member -MemberType NoteProperty -Name "License Name" -Value $($license.AssignedLicense.Name)
$object | Add-Member -MemberType NoteProperty -Name "Cost Unit" -Value $($license.Properties | where{$_.Key -eq 'CostUnit'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Used License" -Value $($license.Properties | where{$_.Key -eq 'EntityCost'} | select -ExpandProperty Value)
$object | Add-Member -MemberType NoteProperty -Name "Total Licenses" -Value $($license.AssignedLicense.Total)
# Issue--> $object | Add-Member -MemberType NoteProperty -Name "Tag" -Value $(Get-VMhost | where{$_.Category -like "*Host*"})
$report += $object
if ($DefaultVIServer.Count -gt 0) {
Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Force:$true > $null
}
} #end foreach $license
} else { # Else for if $connection
Write-warning "Not connected to vCenter $($ScriptVCInstance)"
} # endif $connection
} # End foreach $ScriptVCInstance
# write-out as a CSV file
Write-host "Exporting CSV $($env:USERPROFILE)\Licensed-Assets.csv"
$report | Sort-object "vCenter","License","Entity" | Export-csv "$($env:USERPROFILE)\Licensed-Assets.csv" -NoTypeInformation -UseCulture
$sw.Stop()
$sw.Elapsed

esxi detailed Information

I want to get all the information from ESXi. I extracted the information in different CSV files, but once I want to merge them, it does not show all. But I would rather to create foreach to gather same information.
Add-PsSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue"
Import-Module ‚C:\Program Files\Microsoft Virtual Machine Converter\MvmcCmdlet.psd1‘
$datetime = Get-Date -Format "ddMMyyyy";
# Configuration Block
$User =
$Password =
$ESXiServer = "172.17.1.171"
# Connect to ESXi
$PWD = ConvertTo-SecureString -AsPlainText -Force -String $Password;
$SourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$PWD;
$sourceConnection = New-MvmcSourceConnection -Server $ESXiServer -SourceCredential $sourceCredential
$SourceVMName = (Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection).Name
$Datacenter = Get-Datacenter
$Datastore = Get-Datastore
$DataStoreLocation = $Datastore.ExtensionData.info.url
$Datastore = Get-Datastore
# Get-VMHostNetworkAdapter | fl *
Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, DHCPEnabled, DeviceName | Export-Csv C:\VMHostNetworkDetails_$datetime.csv -Delimiter ";"
Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection | select MemorySizeBytes, OperatingSystem, UsedSpacebytes | Export-Csv C:\VMRAmDetails_$datetime.csv -Delimiter ";"
$Global:DefaultVIServers | Select ProductLine,Version,Build, Port | Export-Csv C:\GlobalDetails_$datetime.csv -Delimiter ";"
#(Import-Csv C:\VMHostNetworkDetails_$datetime.csv) + #(Import-Csv C:\VMRAmDetails_$datetime.csv) + #(Import-Csv C:\GlobalDetails_$datetime.csv) | Export-Csv C:\ESxiDetails_$datetime.csv -Delimiter ";"
Note: get-vm does not work for me.
EDIT:
I tried to get the info by using foreach loop, but cannot get IP, SubnetMask, Mac, DHCPEnabled.$VMSysInfo.IPAddressdoes not give me any IP, but Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, DHCPEnabled, DeviceName gives me IP.
$VmInfo = vmware.vimautomation.core\Get-VM
$VMS = ($VmInfo).Name
$VCenter = #()
foreach ($VM in $VMS)
{
$HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
$VMSysInfo = Get-VMGuest -VM $VM
$MyObject = New-Object PSObject -Property #{
VMName = $VM
#VMHostName = $VMSysInfo.HostName
VMIP = $VMSysInfo.IPAddress
VMInstalledOS = $VMSysInfo.OSFullName
PowerState = ($VmInfo | ? {$_.Name -eq $VM}).PowerState
NumberOfCPU = ($VmInfo | ? {$_.Name -eq $VM}).NumCpu
MemoryGB = (($VmInfo | ? {$_.Name -eq $VM}).MemoryMB/1024)
VMDataS = (Get-Datastore -VM $VM).Name
#HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
#HostCluster = (Get-Cluster -VMHost $HostServer).Name
Datacenter = (Get-Datacenter -VM $vm).Name
#Notes = $vm | Select -ExpandProperty Description
Portgroup = (Get-VirtualPortGroup -VM $vm).Name
}
$VCenter += $MyObject
}
$VCenter | Select VMName,
#{N='VMIPAddress';E={$_.VMIP -join '; '}},
VMInstalledOS, PowerState, NumberOfCPU, MemoryGB,
#{N='VMDataStore';E={$_.VMDataS -join '; '}},
HostServer, HostCluster,Datacenter, Notes, Portgroup |
Export-Csv C:\test.csv -NoTypeInformation -Delimiter ";"
I changed the foreach loop and it works:
$VmInfo = vmware.vimautomation.core\Get-VM
#$VmInfo = (Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection).Name
$VMS = ($VmInfo).Name
$Data = #()
foreach ($VM in $VMS)
{
$Datacenter = Get-Datacenter
$Datastore = Get-Datastore
$SourceIP = ($global:DefaultVIServer).name
$DataStoreLocation = $Datastore.ExtensionData.info.url
$VMNetwork = Get-VMHostNetworkAdapter
$PortalGroup = Get-VirtualPortGroup -VM $vm
$MvmcSourceVirtualMachine = Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection
$VMCustom = New-Object System.Object
$VMCustom | Add-Member -Type NoteProperty -Name DataCenter -Value $Datacenter.Name
$VMCustom | Add-Member -Type NoteProperty -Name DataStoreName -Value $Datastore.Name
$VMCustom | Add-Member -Type NoteProperty -Name DataStoreLocation -Value $DataStoreLocation
$VMCustom | Add-Member -Type NoteProperty -Name NumberOfCPU -Value $VmInfo.NumCpu
$VMCustom | Add-Member -Type NoteProperty -Name PowerState -Value $VmInfo.PowerState
$VMCustom | Add-Member -Type NoteProperty -Name MemoryGB -Value $VmInfo.MemoryGB
$VMCustom | Add-Member -Type NoteProperty -Name VMHost -Value $VMNetwork.VMhost
$VMCustom | Add-Member -Type NoteProperty -Name DHCP -Value $VMNetwork.DHCPEnabled
$VMCustom | Add-Member -Type NoteProperty -Name SubnetMask -Value $VMNetwork.SubnetMask
$VMCustom | Add-Member -Type NoteProperty -Name Client -Value $VMNetwork.Client
$VMCustom | Add-Member -Type NoteProperty -Name IP -Value $SourceIP
$VMCustom | Add-Member -Type NoteProperty -Name MacAddress -Value $VMNetwork.Mac
$VMCustom | Add-Member -Type NoteProperty -Name PortalGroupName -Value $PortalGroup.Name
$VMCustom | Add-Member -Type NoteProperty -Name OperatingSystem -Value $MvmcSourceVirtualMachine.OperatingSystem
$Data += $VMCustom
}
$Data | Export-CSV "C:\ESXiInfo.csv" -Delimiter ";" -NoTypeInformation

PowerShell to fetch installed programs

I will be hosting a file on a remote server (read-only) and asking people to run the file on their machines to gather installed program information. I want the file to be saved to their Desktop in their user space, so that I can then have them send it to us.
I have the script, but I'm not managing to obtain information from both "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", and "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" in the same output file. I'm obviously missing something inherently obvious, as PowerShell is clearly able to do this, and I'm asking that someone please save me from my PEBKAC issue!
Thank you in advance, appreciated!
Here is my code;
$computers = "$env:computername"
$array = #()
foreach($pc in $computers){
$computername=$pc
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
$regkey=$reg.OpenSubKey($UninstallKey)
$subkeys=$regkey.GetSubKeyNames()
Write-Host "$computername"
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 | export-csv C:\Users\$env:username\Desktop\Installed_Apps.csv
Right now the following two lines set the same variable:
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
Use this:
$UninstallKey = #(
'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)
Then wrap the real logic in:
$UninstallKey | ForEach-Object {
$regkey=$reg.OpenSubKey($_)
# the rest of your logic here
}