<#

The command to update help (yes, help files should be updated) is Update-Help. Pretty simple right? The problem comes in remembering to do this. I had it in my $Profile script but that really slowed down starting Powershell. And some days I may open/close/open Powershell numerous times. Running Help update each time was way too crazy. But without something automatic, I tend to forget about doing this task until it comes and bites me in the …

So last night I decided to write a quick script that would
1. Get the date of the last time help was updated from a file.
2. If the current date was 7 days or more since that date, run Update-Help.
3. Overwrite the date in the file with the this new update date (for next time).

Sounds like an easy function correct? Well, as they say, “the devil is in the details.” Or in this case, the devil is in casting data types!

Data types is a way of flagging to the computer what kind of information it is looking at. The most common are [string] – meaning characters, words, and such, and [int] which is short for integer – meaning numbers. Others include [bool] (Boolean – yes/no type results), [decimal] (fractional numbers), and [datetime]. For a fuller list, see TechNet – Powershell Types.

However, the usual lists given for Powershell types does not include Object. And that is a real problem. In this case, storing a date in a file was fine until I tried to retrieve it. Get-Content returns an OBJECT. That is by design as the program does not know what it is ever going to return so it encapsulates the data in an array and wraps that in an object container (as best as I can tell).

Problem here is while Powershell has a variety of ways to re-cast data types, [datetime] would not work for an object. Even extracting the array data (Foreach) as type [string] still failed. Most frustrating was using the built-in method for [string].ToDatetime() did not work (nor did temp casting with the  -as keyword). What finally worked was to re-cast using multiple variables, ending up with this:

Function Update-HelpWeekly {
[cmdletbinding()]
Param()
Begin   {
$LastChecked = Get-Content -Path C:\UpdateDateHelp.txt
}#begin

Process {
$LastCheck = [datetime]$LastChecked
$evenDays = ((Get-Date).AddDays(-7)).ToShortDateString()

If($LastCheck -le $evenDays) {
Update-Help -Force
}
}#process

END {
Get-Date -Format d | Out-File C:\UpdateDateHelp.txt
}#end

}#function

Yep, this script should not work but it has been tested on a few machines successfully. A variable holds the Get-Content object and then gets assigned to another variable as a different data type. There is no breaking out the array or splitting of data. Specifically $LastCheck is assigned $LastChecked as a datetime data type even though $LastChecked cannot be directly re-cast as a datetime object! Bizarre. ($LastChecked.ToDateTime(), [datetime]$LastChecked, and $LastChecked -as [datetime] all failed in the script, even when packed inside parenthesizes or evaluated $() ).

As hard as this ending up being (2 hours of *.GetType()) it was worth every drop of sweat because of all the learning and affirming with data types. This ended up being worth so much more than just creating a convenience function.

tags: Data type, Data Types, gettype(), datetime, casting, recasting, re-casting, Powershell

#>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s