Logo_PowerShell

Last time we played with having a script become a module and passing through variables from the command line. 

Which is nice.   VERY nice.  “Life is good” Nice.

And a lot of the time, the system will just “let it run” if you forget to type in variables.  It might not do anything actually USEFUL but it will run.

But we’re going to take take last module we made and give it some “fallbacks”.   Plug in information INCASE you forget.  Or the next admin. 

Having Defaults is a good thing you know. 🙂

So here’s our last module.

———————————————

New-Module -scriptblock {

function ListUsers ($startdate, $enddate, $ou)

{

connect-qadservice -service ‘contoso.local’

get-qaduser –searchroot “contoso.local/$ou” |  where { ($_.Creationdate -ge $startdate) -and ($_.Creationdate -le $enddate) } |  select-Object Name

}

Export-ModuleMember -function ListUsers

}

——————————————–

Now “What if” I forgot to type in either date?  The end result is a big load of nothing, unless you had a user created on the EXACT DATE you ran this on.   So pretty useless.  

Now there are very fancy ways to do this, but the point of any of my posts is for new Admins to get a FEEL for Powershell and learn from there.  And honestly? I’m not that good.  So I go the lazy way.

So for my first check and balance, I want to make sure the End date, if it’s not supplied, is assumed to be at LEAST today.

And with a little “IF statement” that’s fixed.

IF ( $enddate –eq $NULL) { $enddate=get-date; write-host ‘Ending Date not supplied, Today Assumed’ )

And of course, it would make sense to have the Startdate in this particular module to be something BEFORE the End Date, again otherwise, useless results.   But I want to make sure that information is supplied.  If it isn’t, I’m going to tell the silly Administrator and stop the module there and then.

IF ( $startdate –gt $enddate) { write-host ‘The Starting Date MUST be BEFORE the Ending Date’}

In fact with this in mind, I’m going to make sure the module never runs if this information is not supplied by dropping in an “ELSE” that incorporates the rest of the script.

Now there is one REALLY important details I almost forgot.   You see, unless you TELL powershell that the information you’re passing in is a DATE? It’s going to take that nice little ‘4/20/2009’ and figure it out like math!  *oops*.  So you’re “date” will end up being I think some stupid amount like ‘9.95520159283226E-05’

So to fix that, we’re going to tell the module that certain variables are going to be ASSUMED as a date/time.   And that is done by prefixing the variable with [datetime]

Yes, so if you just “skipped off” after Post # 2?  Your ummmm module?  Not working no good 🙂

So there’s a lot more we can do here but we now have a script that looks like this.

———————————————

New-Module -scriptblock {

function ListUsers ([datetime]$startdate, [datetime]$enddate, $ou)

{

IF ( $enddate –eq $NULL) { $enddate=get-date; write-host ‘Ending Date not supplied, Today Assumed’ }
IF ( $ou –eq $NULL) { $ou=’Users’; write-host ‘Organization Unit not supplied, Assuming default Users container’ }
IF ( $startdate –eq $NULL) { $startdate=$enddate; write-host ‘Starting Date not supplied, Today Assumed’ }

IF ( $startdate –gt $enddate) { write-host ‘The Starting Date MUST be BEFORE the Ending Date’}
ELSE
{

connect-qadservice -service ‘contoso.local’

get-qaduser –searchroot “contoso.local/$ou” |  where { ($_.Creationdate -ge $startdate) -and ($_.Creationdate -le $enddate) } |  select-Object Name

}
}

Export-ModuleMember -function ListUsers

}

——————————————–

Next time I’ll touch on something the example in the Powershell help showed us, how we could add in a pseudo “help line” to our module.  There’s a few ways of doing that.

But until next time, Keep on Scriptin’!

Sean
The Energized Tech

Advertisements