Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

PowerShell script performance

$
0
0

Hello all,

I have been asked to write a script to list all the success and failures of daily backup exec task from remote servers (by querying the Windows Event log) and I have put together the below script.

Even though the script is running as expected, I feel it could be more efficient. As I am on a quest to improve my PowerShelling skills I would welcome your feedback to enhance the script both syntactically as well as performance-wise.

As far as I can see there is redundant code between the try catch block which is adding to the delay. How can I eliminate it?

$HowFarBack = 7 #enter here the number of days in the past the script has to check for the backup exec event logs
$InputCSV='C:\Scripts\backupexec.csv' # enter the Windows path to the CSV containing all the Backup Exec servers you want to target this script on

$ErrorLog = @() 
$Output=@()

Import-CSV $InputCSV | ForEach { 

                                       $Comp_Name=$_.ServerName                                        
                                       Write-Host "Processing $Comp_Name......"

                                       if (!(Test-Connection  $Comp_Name -Quiet))                                 
                                            {
                                            $ErrorLog += New-Object PSObject -Property @{Message="The machine is  not reachable. Are you sure it's on the network?";ComputerName= $Comp_Name }
                                            }
                                       else 
                                            {
                                                    try {

                                                         $IsthereAnyResult= @()
                                                         $IsthereAnyResult= Get-WinEvent -ComputerName $Comp_Name  -FilterHashTable @{LogName='application'; StartTime=(Get-Date).AddDays($HowFarBack * (-1))} |
                                                                             Where-Object { ($_.ID -eq 57755) -or  ($_.ID -eq 34113) -or  ($_.ID -eq 34112)}

                                                        if ($IsthereAnyResult.Count -lt 1 ) {
                                                        
                                                         $ErrorLog += New-Object PSObject -Property @{Message="There are no BackupExec events logged in the last $HowFarBack days on this computer";ComputerName=$Comp_Name}    

                                                        }

                                                        else

                                                        {
                                                         $Output +=Get-WinEvent -ComputerName $Comp_Name  -FilterHashTable @{LogName='application'; StartTime=(get-date).AddDays($HowFarBack * (-1))} | 
                                                                            Where-Object { ($_.ID -eq 57755) -or  ($_.ID -eq 34113) -or  ($_.ID -eq 34112)}   | 
                                                                                    Select-Object MachineName,
                                                                                                  ID, 
                                                                                                  @{Name="Result"; Expression = {
                                                                                                                            Switch ($_.ID)  {
                                                                                                                                    '57755' {"Success - Skipped"} 
                                                                                                                                    '34113' {"Failed"} 
                                                                                                                                    '34112' {"Success"} } } },
                                                                                                  TimeCreated 
                                                        } #end if
                                                  } #end try
                                                 Catch {
                                                         $ErrorLog += New-Object PSObject -Property @{Message=$_.Exception.Message + " (Is this a pre-Vista/Windows 2008 machine?)";ComputerName=$Comp_Name}    
                                                       } #end Catch
                                            }  # end test-connection if condition
                                   } # end main foreach

if ($Output.Count -gt 0) { $Output | Out-GridView -Title "Results at $(Get-Date)"}

if ($ErrorLog.Count -gt 0) { $ErrorLog | Out-GridView -title "Error Log at $(Get-Date)"}





Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>