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

Last Boot up Time AND SP install time


I wanted to know the last bootup time and the last time a service pack installed AND I wanted the output as a HTML file.

Here are the codes for the colors that I used:
Red = #FF0000
Green = #00FF00
Blue = #0000FF
Cyan (blue and green) = #00FFFF
Magenta (red and blue) = #FF00FF
Yellow (red and green) = #FFFF00

###### Start Posh Script ########
clear 
$pingResults =('C:ScriptsPingResultspingResults.HTM') 
$RunDate  = (get-date).tostring("MM_dd_yyyy") 
$PingTime = (Get-Date -format 'hh:mm') 

#Write the preamble of the report 
Set-Content -Path $pingResults ('<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0//EN http://www.w3.org/TR/REC-html40/strict.dtd">http://www.w3.org/TR/REC-html40/strict.dtd>') #<---- all one line
Add-Content -Path $pingResults ("<HTML> <p>") 
Add-Content -Path $pingResults ("<head> <p>") 
Add-Content -Path $pingResults ("<title> Ping Results </title>") 
Add-Content -Path $pingResults ("</head>") 
Add-Content -Path $pingResults ("<h3>Report Generated " + $RunDate + " @ " + $PingTime + "</h3> <p>") 

$PingMachines = Gc "C:\MachineList.txt" 
ForEach($MachineName In $PingMachines) 
{$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$MachineName'" | 
Select-Object StatusCode 
If ($PingStatus.StatusCode -eq 0) { 
   $wmi = gwmi -Class Win32_OperatingSystem -EA silentlycontinue -ComputerName $MachineName 
   $lastBootTime = $wmi.ConvertToDateTime($wmi.LastBootUpTime) 
   $t1 = Get-WmiObject -Class Win32_QuickFixEngineering -EA silentlycontinue -ComputerName $MachineName | 
   Sort InstalledOn -Descending | 
   Select InstalledOn -First 1 

Add-Content -Path $pingResults ("<pre><h3>Server Name: <FONT color = #00FF00>" + $MachineName + "</FONT></h3> Last Boot Time: " + $lastBootTime + " Latest installed update: " + $t1 + "</pre>")  <font color="#ff0000"></font> #<---- all one line
$lastBootTime = " " } 

Else { 
Add-Content -Path $pingResults ("<pre><h3>Server Name: <FONT color = #FF0000>" + $MachineName + "</FONT></h3></pre>")}  <font color="#ff0000"></font> #<---- all one line
     } 

############# End POSH Script ###############

Handy script to delete files from the temp directory

This is handy.  It will delete all files of the given extension from the temp folder that are
more than the given number of days old


 

Get-ChildItem -Path $env:Temp | where{($_.LastAccessTime -lt
((Get-Date).AddDays(-2))) -and ($_.Extension -eq ".tmp"
)} | Remove-Item -WhatIf

 

Change .tmp to another extension and change the number of days to match your needs.

Remove the -Whatif if you like the results.