Friday, March 4, 2016

Powershell v2 Learn from the Pros Just look in your Windows 7 folders

If you really want to see how to use Powershell like a pro, look at what Microsoft puts on Windows 7 machines.  If you didnt realize it there are a lot of maintenance tasks that have been handed over to those little .ps1 files.  To get a glimpse of what alls down there run this command:
dir C:Windowsdiagnostics -Recurse *.ps1 | select name, directory
My figures show there are 210 scripts down there:
(dir C:Windowsdiagnostics -Recurse *.ps1 | select name, directory).count
210
To get an idea of what all these scripts contain (in terms of functionality) run this to list all the functions contained in those 210 files:
dir C:Windowsdiagnostics -Recurse *.ps1 | Get-Content | ? {$_ -match ^function } | % {$_ -replace function ,} | % {$_ -replace (.+,}
On my machine (Windows 7 Enterprise) I see 318 functions:
(dir C:Windowsdiagnostics -Recurse *.ps1 | Get-Content | ? {$_ -match ^function } | % {$_ -repace function ,} | % {$_ -replace (.+,}).count
318
The fact that the function count and the script count are close just tells me functions are used sparingly.  But, when you look at a few of the scripts you will find that many of them are stored in a single .ps1 file and dot sourced by other scripts.  For example, the script  contains the following code (its short):
# Copyright © 2008, Microsoft Corporation. All rights reserved.
PARAM($RepairName, $RepairText, $HelpTopicLink, $HelpTopicLinkText, $FailResolution)

#Non NDF Help Topic Resolution (defined non-manual so we dont need to prompt the user to see the repair)
#include utility functions
. .UtilityFunctions.ps1

Import-LocalizedData -BindingVariable localizationString -FileName LocalizationData
#the strings come in as raw resource strings, load the actual strings
$repairNameStr = LoadResourceString $RepairName;
$repairTextStr = LoadResourceString $RepairText;
$helpTopicLinkTextStr = LoadResourceString $HelpTopicLinkText

#display the help topic interaction
Get-DiagInput -ID "IT_HelpTopicRepair" -Parameter @{"IT_P_Name"=$repairNameStr; "IT_P_Description"=$repairTextStr; "IT_P_HelpTopicText" = $helpTopicLinkTextStr; "IT_P_HelpTopicLink" = $HelpTopicLink;}
if($FailResolution -eq "TRUE")
{
    throw "Issue not resolved."
}
If you notice, line 7 contains this line
. .UtilityFunctions.ps1
This references a script (UtilityFunctions.ps1) in the same directory so its functions can be used as if they were in the same file.  Popping over to this script, I ran a quick check and found that there are 51 functions in this script:
(dir C:Windowsdiagnosticssystem etworkingutilityfunctions.ps1 | Get-Content | ? {$_ -match ^function } | % {$_ -replace function ,} | % {$_ -replace (.+,}).count
51
So, this gives us a little best practice. Store functions to be used across multiple files in a single .ps1 script and dot source it. This allows easier maintenance, smaller scripts, and, simplified version control.

Looking more closely at specific .ps1 files in these directories it becomes pretty clear that there are some tricks that can be learned from studying these scripts.  For example, you can look in the same directory to check out the file C:WindowsdiagnosticssystemNetworkingUtilityFirewall.ps1.  Within this file, you can see that there is a great example of C# porting Win32 unmanaged code to Powershell with the Add-Type cmdlet.  So, if you want real world examples of how to do that take a glance at this file.  This is just one example of a practical piece of default code anyone with a Windows 7 install can learn from.  There are plenty of others, but, I think now that you might be aware of whats out there, you can go check it out.

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger