Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active November 2, 2022 21:58
Show Gist options
  • Select an option

  • Save Rugby-Ball/ac36ccbdafa607ac36bb73fc3ca730fe to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/ac36ccbdafa607ac36bb73fc3ca730fe to your computer and use it in GitHub Desktop.
Run across all running Windows Instances across all Regions in AWS and pull in the Scheduled Task /Task Scheduler info from Task Path \ #Inventory #Windows #AWS #Scheduled_Tasks #Task_Scheduler #Public
# inventory-of-Windows-Scheduled-Task-in-AWS.ps1
<#
Description: Run across all running Windows Instances across all Regions in AWS and pull in the Scheduled Task /Task Scheduler info from Task Path \
Written: Ed Walsh
PowerShell.Core tested: Not Tested
Version: 1.3
Create Date: 09/30/2021
Revised Date: 11/02/2022
#>
#Getting Running Windows Instances
$servers = @()
Get-EC2Region -RegionToCall us-east-1 <# -RegionToCall used because of https://github.com/aws/aws-tools-for-powershell/issues/46 #> | Foreach-Object {
#Start of pulling EC2 information
$Region = $_.RegionName
$subnets = Get-EC2Subnet -region $region
$vpc = Get-EC2VPC -region $region
$instances = (Get-EC2Instance -Region $Region).Instances | Where-Object { $_.Platform -eq "Windows" -and $_.state.name -eq 'running' }
foreach ($i in $instances) {
$region = $region
$iName = ($i.tags | Where-Object -Property key -EQ 'Name').Value
$iprivateip = $i.PrivateIpAddress
$iInstanceId = $i.instanceID
$o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{
'InstanceID' = $iInstanceId;
'Region' = $region;
'Instance_Name' = $iName;
'Private_IP' = $iprivateip;
})
$servers += $o
}
}
##Check if c:\temp exists, if it doesnt create it.
If (-not(Test-Path -Path "c:\temp"))
{ New-Item -ItemType Directory -Force -Path C:\temp }
#Pull schedueld task info from each server
$out = @()
$ec2count = (($servers|Measure-Object).count)
#/ Set progress bar variables
$j=0
Foreach ($server in $servers){
#/ Set up progress bar
$j++
$status = "{0:N0}" -f ($j / $ec2count * 100)
Write-Progress -Activity "Working on EC2's Now" -status "Scanning server # $j of $ec2count EC2's : $status% Completed" -PercentComplete ($j / $ec2count * 100)
try {
# Make the server name a FQDN
$remote_hostname = ($server).Instance_Name + ".mrirdp.com"
#Get the servers OS Version Number
$fullversion = $(((gcim Win32_OperatingSystem -ComputerName $remote_hostname).version).split(‘|’)[0])
#Shorten OS Version Number to a single decimal point, so it can become a Decimal Type.
[decimal]$shortVersion = $fullversion.split('.')[0]+'.'+ $fullversion.split('.')[-2]
#Make sure the server is Windows 2012 or better (Win 2008 R2 = 6.1) Get-ScheduledTask came out with Windows 2012.
If ( $shortversion -gt 6.1) {
#If this is Windows 2012 Server or greater, pull the Scheduled Task information from Server.
$WSTS = (Get-ScheduledTask -CimSession $remote_hostname -TaskPath "\" |`
Select-Object @{Name="LastRunTime";Expression={ ($_ |Get-ScheduledTaskInfo).LastRunTime }}`
, @{Name="CountMissedRuns";Expression={ ($_ |Get-ScheduledTaskInfo).NumberOfMissedRuns }} `
, @{Name="LastTaskResult";Expression={ ($_ |Get-ScheduledTaskInfo).LastTaskResult }} `
, Description, TaskName,State,Principal,Execute -ExpandProperty Principal) |`
Select-Object @{N ='Region'; E={($server).region}},@{N ='Server';E = {$remote_hostname}},LastRunTime, State, TaskName, Description, CountMissedRuns, LastTaskResult , @{N ='RunAs';E = {$_.UserID}},@{n='OS_Version';e={$fullversion}}, @{n = 'Error'; e = { "No" } }
#Go through each pulled Scheduled Task from server to add to the Array.
ForEach ($WST in $WSTS) {
$i = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{
'Region' = ($server).region ;
'EC2-ID' = ($server).InstanceID;
'Server' = ($WST).Server;
'LastRunTime' = ($WST).LastRunTime;
'State' = ($WST).State;
'TaskName' = ($WST).TaskName;
'Description' = ($WST).Description;
'CountMissedRuns' = ($WST).CountMissedRuns;
'LastTaskResult' = If( ($WST).CountMissedRuns -eq 0) {"Success"} Else {"Error - Code 0x$(($WST).LastTaskResult)"};
'RunAs' = ($WST).RunAs;
'OS_Version' = ($WST).OS_Version;
'Error' = ($WST).Error;
})
$out += $i
}
}
Else
{
#If server is Windows 2008R2 or lower, do not pull Scheduled Task as this script wont work with those OS Versions. And fill in to Array the server and Error to say its Win 2008R2
$i = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{
'Region' = ($server).region ;
'EC2-ID' = ($server).InstanceID;
'Server' = $remote_hostname;
'LastRunTime' = "";
'State' = "";
'TaskName' = "";
'Description' = "";
'CountMissedRuns' = "";
'LastTaskResult' = "";
'RunAs' = "";
'OS_Version' = $FullVersion;
'Error' = "This is a Windows 2008R2 or lower server. Which is to old for this script to work. Needs 2012 or newer.";
})
$out += $i
}
}
catch {
#If any error happens when trying to run the script on server, Add server name to Array and put in the error message for later investigation.
$i = ""|Select-Object @{N ='Region'; E={($server).region}}, @{N ='EC2-ID';E = {($server).InstanceID}},@{N ='Server';E = {$remote_hostname}}, @{N ='LastRunTime';e={""}}, @{N ='State';e={""}}, @{N ='TaskName';e={""}},@{N ='Description';e={""}}, @{N ='CountMissedRuns';e={""}},@{N ='LastTaskResult';e={""}},@{N ='RunAs';E = {""}}, @{n='OS_Version';e={$fullversion}}, @{n = 'Error'; e = { $error[0] } }
$out += $i
}
}
#If you want to view this on the screen in a GridView, make sure this is un-commented.
$out | Out-GridView
#If you want to export the information to a csv file, make sure this is un-commented.
$out | Export-Csv -NoTypeInformation -Append -Path c:\temp\Scheduled_Task_inventory-for-AWS-$(get-date -f yyyyMMdd-hhmm).csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment