Answered by:
How to add a error to UNC to the machine for the out-gridview.

Question
-
I have made the scripts to look a File Version on a file. The scripts works fine and no issues, but i was asking to add a error to UNC to the machine for the out-gridview.
$filename = "\Program Files\McAfee\Agent\cmdagent.exe" $obj = New-Object System.Collections.ArrayList $computernames = Get-Content C:\computers.txt foreach ($server in $computernames) { #$filepath = Test-Path "\\$server\c$\$filename" If (Test-Connection -ComputerName $server -Count 3 -Quiet) { if (Test-Path "\\$server\c$\$filename") { $file = Get-Item "\\$server\c$\$filename" $obj += New-Object psObject -Property @{'Computer'=$server;'FileVersion'=$file.VersionInfo|Select FileVersion;'LastAccessTime'=$file.LastWriteTime} } else { $obj += New-Object psObject -Property @{'Computer'=$server;'FileVersion'="File Missing"} } } else { $obj += New-Object psObject -Property @{'Computer'=$server;'MachineOnline'="offline"} } } $obj | select computer, FileVersion, MachineOnline |Out-GridView $obj | select computer, FileVersion, MachineOnline |Export-Csv -Path "c:\Version.csv"
Wednesday, March 25, 2020 6:57 PM
Answers
-
Let's write this in PowerShell instead of in chicken scratch:
$props = @{ ComputerName = $null FileName = $null FileVersion = $null LastWiteTime = $null Online = $false } Get-Content C:\computers.txt | ForEach-Object{ $h = $props.Clone() $h.ComputerName = $_ If(Test-Connection -ComputerName $_ -Quiet) { $h.Online = $true $h.Filename = "\\$_\c$\$filename" if(Test-Path $h.Filename) { $file = Get-Item $h.Filename $h.FileVersion = $file.VersionInfo.FileVersion $h.LastWriteTime = $file.LastWriteTime } } [pscustomobject]$h } | Tee-Object -OutVariable results | Out-GridView $results | Export-Csv c:\Version.csv
Easier to read. Easier to understand. Easier to edit. Easier to debug.
\_(ツ)_/
Wednesday, March 25, 2020 8:54 PM
All replies
-
Please try to avoid to post that much unnecessary whitespace. Thanks.
I streamlined your code a little bit.
$filename = "\Program Files\McAfee\Agent\cmdagent.exe" $computerList = Get-Content C:\computers.txt $ComputerList = $Env:COMPUTERNAME $Result = foreach ($ComputerName in $computerList) { $HashTable = [Ordered]@{ ComputerName = $ComputerName } If (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) { $HashTable.Online = $true $TargetPath = "\\$ComputerName\c$\$filename" if (Test-Path $TargetPath) { $file = Get-Item $TargetPath $HashTable.FileVersion = $file.VersionInfo.FileVersion $HashTable.LastWriteTime = $file.LastWriteTime } else { $HashTable.FileVersion = 'n/a' $HashTable.LastWriteTime = 'n/a' } } else { $HashTable.Online = $false } [pscustomobject]$HashTable } $Result | Out-GridView $Result | Export-Csv -Path "c:\Version.csv"
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
- Edited by BOfH-666 Wednesday, March 25, 2020 8:02 PM
- Proposed as answer by Yang YoungMicrosoft contingent staff Tuesday, March 31, 2020 9:52 AM
Wednesday, March 25, 2020 7:59 PM -
Let's write this in PowerShell instead of in chicken scratch:
$props = @{ ComputerName = $null FileName = $null FileVersion = $null LastWiteTime = $null Online = $false } Get-Content C:\computers.txt | ForEach-Object{ $h = $props.Clone() $h.ComputerName = $_ If(Test-Connection -ComputerName $_ -Quiet) { $h.Online = $true $h.Filename = "\\$_\c$\$filename" if(Test-Path $h.Filename) { $file = Get-Item $h.Filename $h.FileVersion = $file.VersionInfo.FileVersion $h.LastWriteTime = $file.LastWriteTime } } [pscustomobject]$h } | Tee-Object -OutVariable results | Out-GridView $results | Export-Csv c:\Version.csv
Easier to read. Easier to understand. Easier to edit. Easier to debug.
\_(ツ)_/
Wednesday, March 25, 2020 8:54 PM -
Let's write this in PowerShell instead of in chicken scratch:
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Thursday, March 26, 2020 2:58 AM -
Oops!
\_(ツ)_/
Thursday, March 26, 2020 3:47 AM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Yang Yang
Tuesday, March 31, 2020 9:52 AM