none
LastWriteTime help needed RRS feed

  • Question

  • Hi

    I have searched around for a while and decided best to ask.

    The Environment and what I have done thus far:

    I have a lot of computers that are part of 1 domain

    I have all of the computer names listed in a notepad file (1 per line)

    I have very little experience with powershell, I'm on a steep learning curve.

    I have executed this script to get the information of OS Service Pack version:

                       

    # Author: Manoj R. Nair 
    # www.manojnair.net 

    $erroractionpreference = "SilentlyContinue" 

    # Create a New Excel Object for storing Data 

    $a = New-Object -comobject Excel.Application 
    $a.visible = $True  

    $b = $a.Workbooks.Add() 
    $c = $b.Worksheets.Item(1) 

    # Create the title row 

    $c.Cells.Item(1,1) = "Machine Name" 
    $c.Cells.Item(1,2) = "OS" 
    $c.Cells.Item(1,3) = "Description" 
    $c.Cells.Item(1,4) = "Service Pack" 

    $d = $c.UsedRange 
    $d.Interior.ColorIndex = 23 
    $d.Font.ColorIndex = 2 
    $d.Font.Bold = $True 
    $d.EntireColumn.AutoFit($True) 

    $intRow = 2 

    $colComputers = get-content C:\Scripts\Servers.txt 

    # Run through the Array of Computers 

    foreach ($strComputer in $colComputers) 


    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 

    # Get Operating System Info 

    $colOS =Get-WmiObject -class Win32_OperatingSystem -computername $Strcomputer 

    foreach($objComp in $colOS) 

    $c.Cells.Item($intRow, 2) = $objComp.Caption 
    $c.Cells.Item($intRow, 3) = $objComp.Description 
    $c.Cells.Item($intRow, 4) = $objComp.ServicePackMajorVersion 


    $intRow = $intRow + 1 


    $intRow = $intRow + 1 

    # Save workbook data 

    $b.SaveAs("C:\Scripts\Checklist.xlsx") 

    I changed this a bit and This worked very well and I gathered my information.

    What I need to do now is very similar to the above script instead of OS Service pack version i need to get the LastWriteTime to be written in the .xlx worksheet. this is what I have mustered together

    $a = New-Object -comobject Excel.Application 
    $a.visible = $True  

    $b = $a.Workbooks.Add() 
    $c = $b.Worksheets.Item(1)

    $c.Cells.Item(1,1) = "Machine Name" 
    $c.Cells.Item(1,2) = "Date Modified"

    $d=$c.UsedRange
    $d.Interior.ColorIndex = 23 
    $d.Font.ColorIndex = 2 
    $d.Font.Bold = $True 
    $d.EntireColumn.AutoFit($True) 

    $intRow = 2 

    ###############################

    $Computers = Get-Content .\ServersROTest.txt

    foreach ($strComputer in $Computers)
    {
    $c.Cells.Item($intRow, 1) = $strComputer.ToUpper()

    $colDate = (get-item c:\program files\xxx\xxx\xxx.rpl).LastWriteTime

    $c.cells.Item($intRow, 2) = ???

    how to I get it to display the results in .csv like this "PCname;LastWriteTime;"

    thank you in advance

    Tuesday, September 30, 2014 10:07 PM

Answers

  • It is amazing that that works at all. It is recreating the file on each trip through the loop.  THisis not necessary and that method would fail under different circumstances.

    To do this try to understand how and why the following is different.

    Get-Content C:\Scripts\stations.txt |
        ForEach-Object{
             $p=@{
                   Station=$_
                   LastWriteTime=(Get-Item "\\$_\C$\Program Files\xxx\xxx\xxx.rpl").LastWriteTime
              }
              New-Object PsObject -Property $p
         } | 
         Export-Csv C:\Scripts\testrun.csv -NoTypeInformation
    

    This writes the file once only.  It also writes a record even if the file does not exist or the computer is not found so if the LastWriteTime is blank you know the file was not found.

    This is how we would do this using PowerShell. Testing for the computer response would also bea good addition.


    ¯\_(ツ)_/¯

    Friday, October 3, 2014 9:17 AM

All replies

  • HELP Export-Csv

    Read the examples.  One of them is what you want.


    ¯\_(ツ)_/¯

    Tuesday, September 30, 2014 10:31 PM
  • I would also note that your request makes no sense.  A computer OS does not have a lastwritetime.

    I recommend that you contact a consultant trained in Windows technology to help you determine what it is you are trying to do.  THe script you posted does not match your request in any way that I can see.


    ¯\_(ツ)_/¯

    Tuesday, September 30, 2014 10:35 PM
  • Jrv, thank you for replying.

    I do not want the lastWriteTime of the Computer OS.

    I want the lastwritetime of file located in 'C:\Program files\XXX\XXX\XX.rpl' this file is located in the same path on 100+ pc's. 

    Maybe I need to rephrase.

    What I want and clearly struggling with is the following:

    For each pc name in textfile.txt  (\\pcname.domainname)

    execute (get-item c:\program files\xxx\xxx\xxx.rpl).LastWriteTime

    return results to .csv file on my local computer in the following format:

    PCNAME;DATE&TIMEMODIFIED

    I appreciate the assistance and I apologize if my original post confuses you. as I said I am on a incredibly steep learning curve and would not attempt this if I had an alternative to resort to.

    again, thank you for any assistance

    Wednesday, October 1, 2014 8:15 AM
  • Hi, something like this should work

    $res = @()
    $stations = gc C:\stations.txt
    foreach ($station in $stations) {
    
    $lastwrite = (get-item "\\$station\c$\program files\xxx\xxx\xxx.rpl").LastWriteTime
    
    $res += "" | Select @{N="Station"; E={$Station}},
                        @{N="Last Write"; E={$lastwrite}}
    
                        }
    
    $res | Export-Csv C:\csv.csv -NoTypeInformation
    


    Wednesday, October 1, 2014 9:12 AM
  • Hi Braham

    Thank you for the script, it got the results but the output was a bit funky, ended up modifying it a bit and it worked like a charm. here is the modified script

    $res = @()

    $stations = get-content 'C:\Scripts\stations.txt'

    foreach ($station in $stations) 
    {
    $lastwrite = (Get-Item "\\$station\C$\Program Files\xxx\xxx\xxx.rpl").LastWriteTime
    $res += "" | Select @{N="Station";E={$station}}, @{N="Last Write";E={$lastwrite}}
    $res | export-csv 'C:\Scripts\testrun.csv' -NoTypeInformation
    }

    Friday, October 3, 2014 7:31 AM
  • It is amazing that that works at all. It is recreating the file on each trip through the loop.  THisis not necessary and that method would fail under different circumstances.

    To do this try to understand how and why the following is different.

    Get-Content C:\Scripts\stations.txt |
        ForEach-Object{
             $p=@{
                   Station=$_
                   LastWriteTime=(Get-Item "\\$_\C$\Program Files\xxx\xxx\xxx.rpl").LastWriteTime
              }
              New-Object PsObject -Property $p
         } | 
         Export-Csv C:\Scripts\testrun.csv -NoTypeInformation
    

    This writes the file once only.  It also writes a record even if the file does not exist or the computer is not found so if the LastWriteTime is blank you know the file was not found.

    This is how we would do this using PowerShell. Testing for the computer response would also bea good addition.


    ¯\_(ツ)_/¯

    Friday, October 3, 2014 9:17 AM