chadwicksracing / chad-public Goto Github PK
View Code? Open in Web Editor NEWScripts I want to share!
Scripts I want to share!
<#
Toast ScheduledTask by Chad
#>
[cmdletbinding()]
Param (
[Parameter()]
[string] $SchTActionProgram = "C:\Windows\System32\wscript.exe", # Program to run
[Parameter()]
[string] $SchTActionArgs = 'Hidden.vbs RunToastHidden.cmd', # Arguments
[Parameter()]
[String] $SchTFolder = "FeatureUpdate", # Folder Name
[Parameter()]
[string] $SchTName = "FeatureUpdate-Toast", # Task Name
[Parameter()]
[string] $SchTDes = "1909", # WaaS Update
[Parameter()]
[string] $SchTWrkPath = "$env:ProgramData\ToastNotificationScript", # Working path
[Parameter()]
[Switch] $Remediate, # Use to add task
[Parameter()]
[switch] $RemoveOnly, # want to remove it all?
[Parameter()]
[string] $TranscriptPath = "$env:windir\CCM\Logs\FeatureUpdate-ToastNotifcation.log"
)
Start-Transcript -Path $TranscriptPath -Append -Force -ErrorAction SilentlyContinue | Out-Null
#Region Begin Build Variables
$ScheduleObject = New-Object -ComObject Schedule.Service
$ScheduleObject.Connect()
$GetSchTFolders = $ScheduleObject.GetFolder($null) # Create the folders method
$SchTFolders = $GetSchTFolders.GetFolders(0) # Build a list of folders on this system
$GetSchT = Get-ScheduledTask -TaskName $SchTName -ea SilentlyContinue
# Build an array we can use for later
[Array]$ArrayFolders = @(
if($SchTFolders) {
foreach ($Fold in $SchTFolders) {
$Fold
}
}
)
# Building block for the Removal part
$RemoveBit = {
# Remove Task & Folder in Task Scheduler
try {
Unregister-ScheduledTask -TaskName $GetSchT.TaskName -Confirm:$false
$GetSchTFolders.DeleteFolder($SchTFolder,$null)
Write-Host "Task & Folder Removed"
}
catch [System.Exception] {
$_
Stop-Transcript | Out-Null
Exit 1
}
}
# Building block for Creating the ta
# See https://docs.microsoft.com/en-us/powershell/module/scheduledtasks to help build out your task the way you need
$CreateTaskBit = {
Write-Host "Action: $SchTActionProgram"
Write-Host "ActionArgs: $SchTActionArgs"
Write-Host "WorkDir: $SchTWrkPath"
# Splat trigger param settings
$TriggerSplatt = @{
Daily = $true
At = (Get-Date 09:00AM)
}
# Splatting for the New-ScheduledTaskSettingsSet commandlet
$SetSettingsSplatt = @{
WakeToRun = $true
AllowStartIfOnBatteries = $true
DontStopIfGoingOnBatteries = $true
ExecutionTimeLimit = (New-TimeSpan -Hours 1)
StartWhenAvailable = $true
}
# Splatt actions
# had to hard code Execute, the CI handler kept changing its value to "Compliant or Non-Comliant"
$ActionSplatt = @{
Execute = "C:\Windows\System32\wscript.exe"
Argument = $SchTActionArgs
WorkingDirectory = $SchTWrkPath
}
# Setup Toast Notification
$Action = New-ScheduledTaskAction @ActionSplatt
$Trigger = New-ScheduledTaskTrigger @TriggerSplatt
$Principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-545"
$SetSettings = New-ScheduledTaskSettingsSet @SetSettingsSplatt
$InputObj = New-ScheduledTask -Action $Action -Principal $Principal -Trigger $Trigger -Settings $SetSettings -Description $SchTDes
# After building task bits, we need to register it
Register-ScheduledTask -TaskName $SchTName -InputObject $InputObj -TaskPath $SchTFolder
Write-Host "Task Created"
}
#EndRegion
#Region Main Discovery
if (-NOT(($Remediate.IsPresent) -OR ($RemoveOnly.IsPresent))) {
$Compliance = $null
while ($null -eq $Compliance -or !($Compliance -eq "Error") -or !($Compliance -eq "3")) {
# Does the Folder Exist
try {
if (
[Int]$Compliance += "1"
}
else {
THROW "FOLDER NOT PRESENT"
}
}
catch [System.Exception] {
[String]$Compliance = "Error"
Break
}
# Does the Task Exist
try {
if (!($null -eq $GetSchT)) {
$Compliance += "1"
}
else {
THROW "TASK NOT PRESENT"
}
}
catch [System.Exception] {
[String]$Compliance = "Error"
Break
}
# Does the Task Make the Description? 1909.ect
try {
if (!($null -eq $GetSchT)) {
if ($GetSchT.Description -eq $SchTDes) {
$Compliance += "1"
}
else {
THROW "THE DESCRIPTION DOES NOT MATCH"
}
}
else {
THROW "TASK NOT PRESENT, THEREFORE NO DESCRIPTION WILL BE THERE"
}
}
catch [System.Exception] {
[String]$Compliance = "Error"
Break
}
Break
}
# Write Compliance for CI Discovery
if ($Compliance -eq "3") {
# $Compliance # For Testing
$ComplianceValue = "Compliant"
Write-Information "Compliant" | Out-Null
Return $ComplianceValue
}
else {
# $Compliance # For Testing
$ComplianceValue = "Non-Compliant"
Write-Information "Non-Compliant" | Out-Null
Write-Information "Compliance Issue: $($Error[0].Exception.Message)" | Out-Null
Return $ComplianceValue
}
}
#EndRegion
#Region Main Remediate
if ($Remediate.IsPresent) {
# Create a New Folder in Task Scheduler
if (!(
# Create the folder
try {
$GetSchTFolders.CreateFolder($SchTFolder)
Write-Host "Folder Created"
}
catch [System.Exception] {
if($.Exception.Message -like "Cannot create a file when that file already exists") {
Write-Host "Folder Already exists"
}
else {
$
Stop-Transcript | Out-Null
Exit 1
}
}
}
else {
Write-Host "Folder Already exists"
}
try {
if ($null -eq $GetSchT) {
& $CreateTaskBit
}
elseif ($GetSchT.Description -notmatch $SchTDes) {
Write-Host "The task description did not match Parameters in script."
Write-Warning "Removing Task and Folder.."
& $RemoveBit
$GetSchTFolders.CreateFolder($SchTFolder)
Write-Host "Folder Created"
& $CreateTaskBit
}
else {
Write-Host "Task Already exists"
}
}
catch [System.Exception] {
$_
Stop-Transcript | Out-Null
Exit 1
}
}
#EndRegion
#Region Remove
if ($RemoveOnly.IsPresent) {
& $RemoveBit
}
#EndRegion
Stop-Transcript | Out-Null
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.