Help with remote powershell script - gets lastwrite date

Trying to create a script that will:

1) look at the list of computers in my file

2) Remote to that computer and fild the last write date of that file

Here's what I have, of course it's not working yet - 

       

$results = @()
$Computers = Get-Content 'C:\Users\myusername\Desktop\script\list.txt' 
$files = get-content ''

    Foreach ($Computer in $Computers) 
       {
            set-location "\\$computer\c$\folder\install"
            $filecol =  (Get-Item  FILE_backup.pl).LastWriteTime |
            foreach ($file in $filecol)
       {
                   $results +=New-Object psObject
                -Property @{'Computer'=$computer;'FileName'=$file;'LastWriteTime'=$file.lastwritetime}
        }
        }
        
   Export-Csv test.csv -NoTypeInformation

July 29th, 2013 10:33am

Try this:

$results = @()
$Computers = Get-Content 'C:\Users\myusername\Desktop\script\list.txt' 
Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\folder\install"
    $file = Get-Item  FILE_backup.pl
    $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'LastWriteTime'=$file.lastwritetime}
}
$results | Export-Csv test.csv -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 1:30pm

I'm trying it out and making a few changes. I'll let you know what I came up with, I'll also have to try and figure out how to add a line with the computer name if it doesn't come back with a result

July 29th, 2013 1:50pm

here with a Little error handling for File on the Computer not found!

$results = @()
$Computers = Get-Content 'C:\Users\myusername\Desktop\script\list.txt' 
Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\folder\install"
    if (Test-Path FILE_backup.pl) {
        $file = Get-Item  FILE_backup.pl
        $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"} 
    }
}
$results | Export-Csv test.csv -NoTypeInformation

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 1:58pm

I've got this part working - It's a little changed from what you gave me but works good. Just need to add IF ELSe statements, but keeps coming up with noting in the output

$results = New-Object System.Collections.ArrayList
$Computers = Get-Content 'C:\Users\USER\Desktop\Script\list.txt' 

Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\FOLDER\script"
    
        $file = Get-Item  FILE_backup.pl
        $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'LastWriteTime'=$file.lastwritetime}
   


$results | select-object computer, FileName, LastWriteTime |write-output > 'C:\Users\USER\Desktop\emsOUT.csv' }


July 29th, 2013 2:39pm

yes, you write in each ForEach Loop the file new!

remove the char > after Write-Output and move the last char in the script  } a line before $result | se...

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 2:45pm

and you should read the error Messages and unterstand this!

You can not use Write-Output, this not worked in the Pipe.

why you not use Export-Csv?

You can also use out-file

$results | select-object computer, FileName, LastWriteTime | out-file  '.\test1.csv'


July 29th, 2013 2:56pm

Something's wrong it doesn't print out anything to the text file
$results = New-Object System.Collections.ArrayList
$Computers = Get-Content 'C:\Users\USERNAME\Desktop\EMS_Script\emslist.txt' 

Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\FOLDER\script"
     if (Test-Path FILE_backup.pl) {
        $file = Get-Item  FILE_backup.pl
        $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"} 
    }
}
}
$results | select-object computer, FileName, LastWriteTime |out-file 'C:\Users\USERNAME\Desktop\EMS_Script\emsOUT.csv' 

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 3:15pm

Your code is not correct, look at the {},

and you have a problem if the first System not have the search file, then is your object not correct!
And a tip, learn the PowerShell Basics before writing scripts.

$Computers = Get-Content 'C:\Users\USERNAME\Desktop\EMS_Script\emslist.txt' 

Foreach ($Computer in $Computers) {
    set-location "\\$computer\c$\FOLDER\script"
    if (Test-Path FILE_backup.pl) {
        $file = Get-Item  FILE_backup.pl
        $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"} 
    }
}
$results | select-object computer, FileName, LastWriteTime |out-file 'C:\Users\USERNAME\Desktop\EMS_Script\emsOUT.csv' 


you can fix the object Problem in this way. Replace the line after else with this line

$results += New-Object psObject -Property @{'Computer'=$computer;'FileName'="File not found";'LastWriteTime'="xx.xx.xx xx:xx:xx"}




Please adjust the date format for your region.

July 29th, 2013 3:29pm

Thanks and yes, I'm still reading the Microsoft guide to writing scripts.

Thanks again!

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2013 3:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics