Search for file type on a list of Computers

This is one of the very first things I tried to do with PowerShell. It searches a list of computers
for Access Data bases and prints the ones it finds to an Excel spreadsheet.
 
#<— Start Script ———>
$a  = Get-Date -format g
$row = 2
$xl = New-Object -c excel.application
$xl.visible = $True
$wb = $xl.workbooks.add()
$sh = $wb.sheets.item(1)
$sh.Range("A1:T1").Font.Bold = $true
$sh.Range("A:A").Font.Bold = $True
$sh.Cells.Item(1, 1) = "Computer Name"
$sh.Cells.Item(1, 2) = "Date Run"  + $a
$sh.Cells.Item(1, 3) = "FullName"
$sh.Cells.Item(1, 4) = "Length"   
$sh.Cells.Item(1, 5) = "Owner"
$sh.Cells.Item(1, 6) = "Extension" 
$sh.Cells.Item(1, 7) = "LastAccessTime"   
$sh.Cells.Item(1, 8) = "CreationTime"
$sh.Cells.Item(1, 9) = "Group Access"
# you will need a list of computers, one computer name per line
$computerlist = Get-Content ‘c:scriptsServers.txt’
 foreach ($srv in $computerlist)
 {
$sh.Cells.Item($row, 1) = $srv
$response = Get-WmiObject -query "Select * From Win32_PingStatus Where Address = ‘$srv’"
 
  if( ($response -eq $null) -or ($response.StatusCode -ne 0)) {
    $sh.Cells.Item($row, 2).Font.ColorIndex = 3
    $sh.Cells.Item($row, 2) = "Does Not Ping"
    $row++
    } else { if ($response.TimeToLive -le 64)
       {
      $sh.Cells.Item($row, 2).Font.ColorIndex = 5
      $sh.Cells.Item($row, 2) = "probably is a Unix host"
      $row++
       } Else {
      # Change the path and the extension. If you want all files, remove the -include *.???
      # This might be faster get-childitem  -path \$srvc$ -filter "*.mdb" -r -ea continue |
     Get-ChildItem -path \$srvc$  -recurse -include *.mdb -ea continue |
      foreach {
    $file = New-Object -TypeName System.Management.Automation.PSObject
    Add-Member -InputObject $file -MemberType NoteProperty -Name "FullName" -Value $_.Fullname
    Add-Member -InputObject $file -MemberType NoteProperty -Name "LastAccessTime" -Value $_.LastAccessTime
    Add-Member -InputObject $file -MemberType NoteProperty -Name "LastWriteTime" -Value $_.LastWriteTime
    $acl = Get-Acl -Path $_.FullName
    Add-Member -InputObject $file -MemberType NoteProperty -Name "Owner" -Value $acl.Owner
    Add-Member -InputObject $file -MemberType NoteProperty -Name "AccessToString" -Value
    $acl.AccessToString
    Add-Member -InputObject $file -MemberType NoteProperty -Name "Group" -Value $acl.Group
    $sh.Cells.Item($row, 3) = $_.FullName
               $sh.Cells.Item($row, 4) = $_.Length      
              $sh.Cells.Item($row, 5) = $acl.Owner 
               $sh.Cells.Item($row, 6) = $_.Extension 
               $sh.Cells.Item($row, 7) = $_.LastAccessTime   
               $sh.Cells.Item($row, 8) = $_.CreationTime
               $sh.Cells.Item($row, 9) = $acl.Group
               $sh.Cells.EntireColumn.AutoFit()
              
                     $row++ }
  }
 }
}
$wb.SaveAs("C:tempfind_files.xls")
# close and release resources
$wb.close($false)
$xl.quit()
spps -n excel
 
#—- End Script ————–>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s