For example: You have a list of 500 machines to update each day throughout the work week, and it's Monday. So if you throw the 500 machine list at the below script you can action on the first 100 of that list(5 days between mon-fri). Run it Tuesday and it will action on the first 125 items in the list (including the 100 from monday). This is important to be aware of - The code below ideally needs the input list to be validated at run time as it has no concept of state - it's not handling the input list possibly changing mid-week, previous runs of the code, future runs to occur, etc. So you'll want to add in code at "#Here is where you might do qualification..." to suit your needs to remove items that were already affected by previous runs, or are not valid when the script is run. Alternately if you prefer or can run against items again, I've included one commented way to do that below. Either way, be aware that the code below without modification if used for our example of 500 items, will result in selection like this:
Day | Items Selected |
---|---|
Mon | 100 |
Tue | 125 |
Wed | 166.6 |
Thur | 250 |
Fri(all) | 500 |
# Example powershell code snippet for taking an input via a file with a list and
# dynamically dividing that up based on the number of days left till friday.
# Can be used if you want to action on a pool of given items throughout the week.
# Virtual machines, indentities, etc.
# This example code does not do a number of things which you might think about if
# using it for production, here are a few:
# -Validation on the list inputted (are all the values formatted as desired, and valid)
# -In the same vein, without additional code, additional runs of the script will
# result in the actions being taken against the same items in the list
# -Sorting or processing of the list in any way other than how it is in the list ingested
# -Better logging beyond plain write-host output
# -Dependent modules (if you need to call them)
# -Time zones - this just uses the local result of get-date on the host executing the
# script, so if you're crossing datelines between the script host and affected items,
# or even timezones you might consider that
#
# Code is given as an example. Feel free to use if it's helpful to you, but please do
# do your own dilligence and validation of course.
Param (
#Script parameters go here, this in an input file
[Parameter(Mandatory = $true)][string]$file
)
#Set Error Action to Silently Continue
$ErrorActionPreference = 'SilentlyContinue'
#Testing variables
$testing = $false
#Read your list of whatever for this week
$list = get-content $file
$totalcount = $list.count
#Here is where you might do any qualification, or drop items from the list that
#don't require action, were already actioned on by a previous run, etc.
#Store variable for today and figure out number of days till friday
$today = get-date
$fridaydate = Get-Date
while ($fridaydate.DayOfWeek -ne "Friday") {$fridaydate = $fridaydate.AddDays(1)}
$daysleft = (New-TimeSpan -Start $today -End $fridaydate).days
#Divide list count by days left till Fri and compile a list for today
$numbertoactionon = $totalcount / $daysleft
$numbertoactiontoday = $list[0..$numbertoactionon]
#Alternately if you wanted to assume mon-fri run once daily, and idempotency of
#your actions below then you could use something like this to select an addition
#one fifth of the total items each run/day:
#$numbertoactionon = ($totalcount/5) * (6-$daysleft)
#$numbertoactiontoday = $list[0..$numbertoactionon]
#If it's the last day - in this example Fri, then we're grabbing all the items left
if ($daysleft -le 1){
$numbertoactiontoday = $list
}
#Loop through today's items in the list and output to screen while taking action
foreach ($i in $numbertoactiontoday){
if($testing){
#Insert your test case code here
Write-host "Testing $i"
#Do some testing
}else{
#Insert your prod (non-test) code here to do whatever
Write-host "Processing $i"
#Do something for real
#Sleep after each individual action, just to space things out over time if desired
write-host "sleeping 60 seconds"
start-sleep 60
write-host "onward!"
}
}