check to see if a script has been run this month

This is part of a script that I wrote to check to see if a script has been run this month.

It reads in a string from a file, then converts it to datetime format. If the months are different it runs the monthly checks. then updates the string in the file.

Source Code

This script has not been checked by Spiceworks. Please understand the risks before using it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

$Today = Get-Date
$Cred = Get-Credential
# The rest of the script is omitted for brevity
#get the last time the monthly checks were run and convert to datetime
[string]$MonthlyString = Get-Content monthlyrun.txt
[datetime]$MonthlyDate = $MonthlyString
#if monthly checks have not been run this month, run them.
if ($Today.Month -ne $MonthlyDate.Month)
{
write-host "running monthly checks"
.\"monthly checks.ps1" -credential $Cred
#update file to show most recent time the monthly checks were run.
$today | Out-File monthlyrun.txt
}