Cannot get results needed from file last modified to CSV.

Harrison Aten
Harrison Aten used Ask the Experts™
on
Can someone please help with this power-shell script. I am trying to get the out put of an array to report the results to a csv. This is the script and the results. Thank you in advance.

Script:
$Computers = Get-Content 'C:\powershell\list.txt'

Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\program files\nsclient++"
    if (Test-Path nsclient.ini) {
        $file = Get-Item  nsclient.ini
        $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'LastWriteTime'=$file.lastwritetime}
    }                
   else {
       $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'="File not found";'LastWriteTime'="xx.xx.xx xx:xx:xx"}
    }
}
$results | select-object computer, LastWriteTime |out-file -append 'C:\powershell\filedateini.csv'

Results:
PS C:\powershell> C:\Powershell\getfiledate.ps1
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
set-location : Cannot find path '\\3MAPPPROD\c$\program files\nsclient++' because it does not exist.
At C:\Powershell\getfiledate.ps1:4 char:5
+     set-location "\\$computer\c$\program files\nsclient++"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\3MAPPPROD\c$\program files\nsclient++:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
 
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At C:\Powershell\getfiledate.ps1:7 char:9
+         $results += New-Object psObject -Property @{'Computer'=$compu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
 

PS Microsoft.PowerShell.Core\FileSystem::\\CherwellDBDev\c$\program files\NSClient++>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try it like this:
Get-Content 'C:\powershell\list.txt' | ForEach-Object {
	$computer = $_
	Write-Host "Processing $($computer)"
	Try {
		Get-Item -Path "\\$($computer)\c$\program files\nsclient++\nsclient.ini" -ErrorAction Stop |
			Select-Object -Property @{n='ComputerName'; e={$computer}}, @{n='FileName'; e={$_.FullName}}, LastWriteTime, Error
	} Catch {
		$_.Exception.Message |
			Select-Object -Property @{n='ComputerName'; e={$computer}}, FileName, LastWriteTime, @{n='Error'; e={$_}}
	}
} | Export-Csv -NoTypeInformation -Path 'C:\powershell\filedateini.csv'

Open in new window

Harrison AtenSystems Engineer

Author

Commented:
Thank you very much work as needed and we can close this post, I really appreciate the time and effort that you spent helping me with this issue. Good to be back at the exchange.
Harrison AtenSystems Engineer

Author

Commented:
Thank you very much work as needed and we can close this post, I really appreciate the time and effort that you spent helping me with this issue. Good to be back at the exchange.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial