欢迎购买我的课程:

https://edu.51cto.com/course/35509.html

前半部分免费,后半部分收费。


适合人群:

对Powershell和域有一定知识的运维和爱好者。

你将会学到:

利用Powershell创建管理计划任务和计划任务的监控及邮件报警

课程简介:

利用Powershell创建管理计划任务和计划任务的监控及邮件报警。

学会后可以用Powershell批量创建各种备份检查监控计划任务,并定期监控,如果遇到报错或任务失败,可以自动邮件报警,做到真正的自动化运维。

课程目录:

E01-Poweshell管理计划任务

E02-计划任务邮件报警

____________________________________________________________________


E01-Poweshell管理计划任务中免费的脚本,请结合视频使用。

#1.ScriptDemo

$name="Test"
$date=Get-Date -Format "yyyyMMdd" 
$parentpath=Split-Path $PSScriptRoot | Split-Path
$path="$parentpath\backups\$name"

#Optional
New-Item $path -ItemType Directory -Name $date
$path1="$parentpath\backups\$name\$date\"

#Scriptblock
New-Item $path1 -ItemType Directory -Name Test

#2.TaskDemo

#Save before run this script#
#run this script with "C:\...\demo.ps1" in another tab

$time= '3 am'
##Once
$trigger = New-ScheduledTaskTrigger -once -At "2024/1/2 5:16:39 AM"

##Once an hour/miniute/second
$trigger = New-ScheduledTaskTrigger -RepetitionInterval 01:00:00 -once -At $time

##Daily
$trigger = New-ScheduledTaskTrigger -daily -At $time

##Every 3 days
$trigger = New-ScheduledTaskTrigger -daily -At $time -DaysInterval 3 

##Every monday
$trigger = New-ScheduledTaskTrigger -Weekly  -DaysOfWeek Monday -At $time

##Every 3rd week on monday
$trigger = New-ScheduledTaskTrigger -Weekly  -DaysOfWeek Monday -At $time -WeeksInterval 3

##AtLogon
$trigger = New-ScheduledTaskTrigger -AtLogon
##AtStartup
$trigger = New-ScheduledTaskTrigger -AtStartup

##TaskDemo
<#
$taskpath="\AD"
$user="NT AUTHORITY\SYSTEM"
$name='GPOWMI'
$path1="$PSScriptRoot\Scripts\gpo\$name.ps1"
New-Item $PSScriptRoot\backups\gpo -ItemType Directory -Name $name   ###Optional CMDlet
$time="07:10 PM"
#>


##with System Account 
##This can cover most task but some still need use specified admin account
<#
$taskpath="\AD"
$user="NT AUTHORITY\SYSTEM"
$name="Delete"
$time="12:00 AM"
#New-Item $PSScriptRoot\backups -ItemType Directory -Name $name -ErrorAction SilentlyContinue
$path1="$PSScriptRoot\Scripts\$name\$name.ps1"

$actions = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path1
$trigger = New-ScheduledTaskTrigger -RepetitionInterval 01:00:00 -once -At $time
$principal = New-ScheduledTaskPrincipal -user $user -RunLevel Highest -LogonType ServiceAccount 
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Compatibility Win8 -DontStopIfGoingOnBatteries -Hidden -WakeToRun
$task = New-ScheduledTask -Action $actions -Trigger $trigger -Settings $settings  -Principal $principal
Register-ScheduledTask $name -InputObject $task -TaskPath $taskpath

#>


##with Administrator Account
##Some tasks only run sucessfully with admin authority
<#
$taskpath="\AD"
$user101="dhcp\administrator"
$password="Suzhou123"
$name='Test'
$time="12:00 AM"
New-Item $PSScriptRoot\backups -ItemType Directory -Name $name 
$path1="$PSScriptRoot\Scripts\$name\$name.ps1"
$actions = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path1
$trigger = New-ScheduledTaskTrigger -RepetitionInterval 01:00:00 -once -At $time
$principal = New-ScheduledTaskPrincipal -user $user101 -RunLevel Highest -LogonType ServiceAccount 
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Compatibility Win8 -DontStopIfGoingOnBatteries -Hidden -WakeToRun
$task = New-ScheduledTask -Action $actions -Trigger $trigger -Settings $settings  -Principal $principal
Register-ScheduledTask $name -InputObject $task -TaskPath $taskpath -User $user101 -Password $password
#>


<#Multiple Triggers & ActionsTask
$name='Hibernate'
$path1="$PSScriptRoot\$name.ps1"
$path2="$PSScriptRoot\abc.ps1"
$time1="02:00 AM"
$time2="04:00 AM"
#$user="NT AUTHORITY\SYSTEM"

$actions = (New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path1),(New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path2)
$trigger = (New-ScheduledTaskTrigger -Daily -At $time1),(New-ScheduledTaskTrigger -Daily -At $time2)
$principal = New-ScheduledTaskPrincipal -user $user -RunLevel Highest -LogonType ServiceAccount 
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Compatibility Win8 -DontStopIfGoingOnBatteries -Hidden -WakeToRun
$task = New-ScheduledTask -Action $actions -Trigger $trigger -Settings $settings  -Principal $principal
Register-ScheduledTask $name -InputObject $task -TaskPath $taskpath
#>


##SetTask
<#
$name='ErrorAlertMail'
$taskpath="\AD"
$path1="$PSScriptRoot\Scripts\MailNotification\$name.ps1"
$action=New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path1 
Set-ScheduledTask $name -Action $action  -TaskPath $taskpath
#>


##Test & TroubleShooting
<#
$taskname="$name"
$taskpath='\ad\'
Get-ScheduledTask -TaskName $taskname -TaskPath $taskpath
Start-ScheduledTask -TaskName $taskname  -TaskPath $taskpath
Get-ScheduledTaskinfo -TaskName $taskname -TaskPath $taskpath
Unregister-ScheduledTask -TaskName $taskname -TaskPath $taskpath -Confirm:$false
#>

#3.AddNewTask

$taskpath="\AD"
$user="NT AUTHORITY\SYSTEM"
$name="Test"
$time="12:00 AM"
New-Item $PSScriptRoot\backups -ItemType Directory -Name $name
$path1="$PSScriptRoot\Scripts\$name\$name.ps1"
$actions = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $path1
$trigger = New-ScheduledTaskTrigger -RepetitionInterval 01:00:00 -once -At $time
$principal = New-ScheduledTaskPrincipal -user $user -RunLevel Highest -LogonType ServiceAccount 
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -Compatibility Win8 -DontStopIfGoingOnBatteries -Hidden -WakeToRun
$task = New-ScheduledTask -Action $actions -Trigger $trigger -Settings $settings  -Principal $principal
Register-ScheduledTask $name -InputObject $task -TaskPath $taskpath





##TroubleShooting
<#
$taskname='taskname'
$taskpath='\ad\'
Get-ScheduledTask -TaskName $taskname -TaskPath $taskpath
Start-ScheduledTask -TaskName $taskname  -TaskPath $taskpath
Get-ScheduledTaskinfo -TaskName $taskname -TaskPath $taskpath
Unregister-ScheduledTask -TaskName $taskname -TaskPath $taskpath -Confirm:$false
#>