Last time we will say this. The select statement and format-table have t go outside any loops or you will get repeated headers and unreliable results.
This same question has been asked by the OP repeatedly. The answers are not being understood for some reason.
Hi Souza,
where to begin ...
- You do not need to invoke a command on another computer to open a registry remotely. Seriously, the built in tools are performing just great. Here's an example in the gallery that should work just fine for remote queries.
- Test-Connection with count 4 will take quite a long time, are you working over a horrible connection to justify having to take 4 samples?
- Your else-block that runs on 32bit targets will not select a thing, which will cause trouble when exporting to csv
- Your if-block uses convertto-csv. Drop that commandlet, avoid output formatting well ahead of doing the actual output in a script. This is responsible for the multiple headers.
- Instead of Out-File, use Export-Csv.
Cheers,
This same question has been asked by the OP repeatedly. The answers are not being understood for some r
Invoke-Command returns the computername in the batch. We showed you that before. You do not need to add it.
Here is how we would do this in PowerShell.
$file= "C:\wklist.txt" $sb64={ Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*,HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,InstallDate,@{N='MachineName';E={$env:ComputerName}} | Sort MachineName,DisplayName | ConvertTo-Csv -NoTypeInformation } $sb32={Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*} $cre1 = Get-Credential "Enter Credential" Get-Content $file | ForEach-Object { If(Test-Connection -ComputerName $_ -count 4 -Quiet ){ $OS=Get-WmiObject Win32_OperatingSystem -ComputerName $_ -Credential $cre1 If($OS.OSArchitecture -eq '64-bit'){ Invoke-Command -cn $_ -ScriptBlock $sb64 -Credential $cre1 }else{ Invoke-Command -cn $_ -ScriptBlock $sb32 -Credential $cre1 } }else{ New-Object PSObject -Property @{DisplayName="OFFLINE";DisplayVersion="N/A";InstallDate="N/A";MachineName="$_"} } } | Select-Object DisplayName,DisplayVersion,InstallDate,@{N='MachineName';E={$_.PsComputerName} | Export-Csv c:\temp\software_result.csv -NoTypeInformation
I am not writing this for you. I am sure there are typos. It is just to show you how we would do this in PowerShell.
Remember that you are dealing with an object system. Try to understand how objects work. Try to avoid unnecessary and redundant comments. THey add coding issues by making code hard to read and see.
Invoke-Command returns the computername in the batch. We showed you that before. You do not need to add it.
Here is how we would do this in PowerShell.
$file= "C:\wklist.txt" $sb64={ Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*,HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,InstallDate,@{N='MachineName';E={$env:ComputerName}} | Sort MachineName,DisplayName | ConvertTo-Csv -NoTypeInformation } $sb32={Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*} $cre1 = Get-Credential "Enter Credential" Get-Content $file | ForEach-Object { If(Test-Connection -ComputerName $_ -count 4 -Quiet ){ $OS=Get-WmiObject Win32_OperatingSystem -ComputerName $_ -Credential $cre1 If($OS.OSArchitecture -eq '64-bit'){ Invoke-Command -cn $_ -ScriptBlock $sb64 -Credential $cre1 }else{ Invoke-Command -cn $_ -ScriptBlock $sb32 -Credential $cre1 } }else{ New-Object PSObject -Property @{DisplayName="OFFLINE";DisplayVersion="N/A";InstallDate="N/A";MachineName="$_"} } } | Select-Object DisplayName,DisplayVersion,InstallDate,@{N='MachineName';E={$_.PsComputerName} | Export-Csv c:\temp\software_result.csv -NoTypeInformation
I am not writing this for you. I am sure there are typos. It is just to show you how we would do this in PowerShell.
Remember that you are dealing with an object system. Try to understand how objects work. Try to avoid unnecessary and redundant comments. THey add coding issues by making code hard to read and see.
Hello again, I need some extra help!
I have this script in powershell that collects all installed software from machines in a list:
#Set list path $file= "C:\wklist.txt" #Get credential $cre1 = Get-Credential "Enter Credential" #Loads list content Get-Content $file | ForEach-Object { #ICMP test 4 packets If(Test-Connection -ComputerName $_ -count 4 -Quiet ){ #Check SO Architecture and query installed software $OSArchitecture = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ -Credential $cre1 | Select-Object OSArchitecture -ErrorAction Stop).OSArchitecture If($OSArchitecture -eq '64-bit'){ Invoke-Command -cn $_ -ScriptBlock { Get-ItemProperty HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*,HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,InstallDate,@{N='MachineName';E={$env:ComputerName}} | Sort MachineName,DisplayName | ConvertTo-Csv -NoTypeInformation } -Credential $cre1 } Else{ Invoke-Command -cn $_ -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* } -Credential $cre1 } } #If ICMP fails Else{New-Object PSObject -Property @{DisplayName="OFFLINE";DisplayVersion="N/A";InstallDate="N/A";MachineName="$_"} | Select-Object DisplayName,DisplayVersion,InstallDate,MachineName } } | Out-File "c:\temp\software_result.csv"
It's outputing the header for every single object in the loop! ATTENTION: I know that I must place the Select-Object outside the loop, then it will work just fine!
The thing is that those registry key do not contain machine names and so I made a custom column with the name of the current workstation in the loop:
@{N='MachineName';E={$env:ComputerName}}
My problem is, if I place the select outside the loop with the Out-File, the custome column will print the my workstation name, not the name of the current workstation inside the loop.
How can I fix it?
Thanks in Advance.
- Edited by H Souza Friday, August 21, 2015 5:52 AM