Forum Discussion
sdo
Moderator
I'll let you work out how to send an email from PowerShell...
...and I have also tried to give you a simple "jacket / header" script for your own future PowerShell scripts...
################################################################################################################### # Vers Date Who Description # ---- ---- --- ----------- # v0.01 08-Jan-2019 sdo Simple script to list long running backup jobs. ################################################################################################################### # Assumptions # ----------- # 1) That the Windows system path has been modified to include the folder paths for NetBackup commands. # 2) That this script will be run on a Master Server, or on a Media Server that is a Server of a Master Server. # # Abstract # -------- # Creates a simple report file of long running jobs. ################################################################################################################### ################################################################################################################### # Script behaviour settings... # $ErrorActionPreference = "Stop" ################################################################################################################### # Functions... # Function Log( $myText ) { If ( $myText -eq $Null ) { Write-Host "" } Else { Write-Host $myText } If ( $myScriptLog -ne "" ) { If ( $myText -eq $Null ) { Add-Content -Path $myScriptLog -Value "" } Else { Add-Content -Path $myScriptLog -Value $myText } } } Function Test-IsAdmin { Try { $myIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() $myPrincipal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $myIdentity Return $myPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) } Catch { Throw "...failed to determine if the current user is an admin, error '{0}'..." -f $_ } } ################################################################################################################### # Display script name and location... # $myScriptSpec = [System.IO.FileInfo]$PSCommandPath $myScriptPath = $myScriptSpec.Directory $myScriptName = $myScriptSpec.BaseName $myUsername = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name $myComputerDomain = ( Get-WmiObject Win32_ComputerSystem ).Domain $myComputername = ( Get-WmiObject Win32_ComputerSystem ).Name $myScriptDat = Join-Path $myScriptPath "$myScriptName.dat" $myScriptLog = Join-Path $myScriptPath "$myScriptName.log" $myScriptTmp = Join-Path $myScriptPath "$myScriptName.tmp" $myScriptTxt = Join-Path $myScriptPath "$myScriptName.txt" If ( Test-Path $myScriptDat ) { Remove-Item $myScriptDat } If ( Test-Path $myScriptLog ) { Remove-Item $myScriptLog } If ( Test-Path $myScriptTmp ) { Remove-Item $myScriptTmp } If ( Test-Path $myScriptTxt ) { Remove-Item $myScriptTxt } Log "" Log "...script spec: $myScriptSpec" Log "...script path: $myScriptPath" Log "...script name: $myScriptName" Log "" Log "...user: $myUsername" Log "...computer name: $myComputerName" Log "...computer domain: $myComputerDomain" Log "" Log "" If ( -not ( Test-IsAdmin ) ) { Log "...script must be run as administrator, script aborting..." Exit 2 } ################################################################################################################### # Once more unto the breach, dear friends... # $myThreshold = 86400 bpdbjobs -most_columns -file $myScriptDat $myStatus = $LastExitCode If ( $myStatus -ne 0 ) { Log "...failed to collect job data, status '$myStatus'..." Exit $myStatus } Get-Content $myScriptDat | ForEach-Object { $myData = $_ -Split "," $jobID = $myData[0] $jobType = $myData[1] $jobState = $myData[2] $jobSchedule = $myData[5] $jobClient = $myData[6] $jobRunTime = [int]$myData[9] If ( $jobType -eq "0" ) { If ( $jobRunTime -gt $myThreshold ) { Log "job $jobID $jobClient $jobSchedule runtime is $jobRuntime" Add-Content -Path $myScriptTxt -Value "long job $jobID $jobClient $jobSchedule runtime is $jobRuntime" } } } Exit 0
Related Content
- 8 years ago