Avatar of Harrison Aten
Harrison Aten
Flag for United States of America asked on

Cannot get results needed from file last modified to CSV.

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++>
Powershell

Avatar of undefined
Last Comment
Harrison Aten

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Harrison Aten

ASKER
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 Aten

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck