Error Checking


so have nifty little script deletes temp files on pc.  

$tempfiles = @("c:\windows\temp\*", "c:\users\*\appdata\local\temp\*")  remove-item $tempfiles -force -recurse -erroraction silentlycontinue -warningaction silentlycontinue | out-null

it works great.  however, our security admin wants use part of incident response task.  he text file created when process starts, , if errors generated, wants errors listed in text file. when task complete, wants line saying temp files have been deleted.  

i created file through script:

write-verbose "creating remediation logfile" -verbose  $rlogfile = "$sub2\remediationlog.txt"  new-item $rlogfile -type file -force | out-null  add-content $rlogfile "remediation log $env:computername started $(get-date -displayhint datetime )`r" | out-null  add-content $rlogfile "***** begin remediation *****`r`r" | out-null

i have played around try - catch - , $?, cannot seem output need.  i expect @ least 1 or 2 errors when task run (file in use or no access)but not want process stop.  

anyone have ideas how accomplish log file?

thanks in advance,

matt 

  

matt dillon

hi matt,

here's can play with:

$logfile = 'c:\temp\deletetemplog\{0}-{1}_deletetemplog.txt' -f $env:computername,(get-date -format mmddyyyy)  write-output "-----start - $(get-date)-----" | add-content $logfile write-output "pc: $($env:computername)`r`n" | add-content $logfile  $filepaths = 'c:\folder1','c:\folder2'  $filepaths | foreach {       get-childitem -path $_ -file -recurse | foreach {          try {              remove-item -path $_.fullname -erroraction stop          } catch {              write-output $_.exception.message | add-content $logfile          }      }  }  write-output "`r`ntemp file deletion completed" | add-content $logfile write-output "-----end - $(get-date)-----" | add-content $logfile




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...