MISC.‎ > ‎

Download Sysinternals Tools Help (Powershell)

A Powershell function that connects to Sysinternals Web site and downloads the Help files on all the tools, Creates a Powershell help file accessible from powershell Get-Help command.


Get-SysInternalsHelp.ps1

 


#=*=*=*=*=*=*=*=*=*=*=*=*=

# Created By : Assaf Miron

# Date : 22/09/2008

# Description : Download Sysinternals Tools Help

#=*=*=*=*=*=*=*=*=*=*=*=*=

Function Get-SysInternalsHelp {

param( $SysTool="",$Update=$false)


#Define where the Help Files are

$HelpFiles = (Get-ItemProperty HKLM:\software\microsoft\PowerShell\1\PowerShellEngine).ApplicationBase

If ($update) # Check for Updates

{

# Define a new Web Client Object

$wc = New-Object System.Net.WebClient


#region Collect Tools Name and Links to $sysTools

$sysintSite = "http://www.microsoft.com/technet/sysinternals/utilitiesindex.mspx"

$content = $wc.DownloadString($sysintSite)

$sysTools = @{}

$ToolPat = [regex] "<p><a*.*?>*.*?</a><br />*.*?</p>"

$ToolsMatch = $ToolPat.match($content)

While ($ToolsMatch.success){

$linkPat = [regex] "href=*.*?>"

$LinksMatch = $linkPat.match($ToolsMatch.Value)

$NamePat = [regex] '">.*</a>'

$NameMatch = $NamePat.match($ToolsMatch.Value)

$ShortDesc = $ToolsMatch.value.Substring($ToolsMatch.value.LastIndexOf("<br />")+6,$ToolsMatch.value.IndexOf("</p>") - $ToolsMatch.value.LastIndexOf("<br />")-6)

$sysTools.add($NameMatch.Value.Substring(2,$nameMatch.Length-6),@($LinksMatch.Value.Substring(6,$LinksMatch.Length-8),$ShortDesc))

$ToolsMatch = $ToolsMatch.NextMatch()

}

#endregion


#region Create About Help Files

ForEach ($tool in $sysTools.Keys){

$ToolSite = $sysTools.Get_Item($tool)[0]

$ToolDesc = $sysTools.Get_Item($tool)[1]

$ToolHelpPath = "$HelpFiles\about_$tool.help.txt"

$exist = test-path $ToolHelpPath

# Get all the Site String Content

$tmpContent = $wc.DownloadString($ToolSite)

# find the First index of the Header

$first = $tmpcontent.IndexOf("<h2>Introduction</h2>")

# find the last section of the Page

$last = $tmpContent.IndexOf("<strong>Download")

# temp Help File for the Compare-object Compare

$tmpHelpFilePath = "C:\tmpHelpFile.txt" 

#output the site Content (after filtering) to a Temp File

If($last -gt $first) { $intChar = $last - $first } Else { $intChar = 1 }

$FileContent = $tmpContent.Substring($first,$intChar) 

Convert-Html2Help $FileContent $ToolDesc | out-file $tmpHelpFilePath

# Check if the Help file Exists

if($exist){

If((Compare-Object (Get-Content $tmpHelpFilePath) $(Get-Content $ToolHelpPath)) -ne $Null -or $(Test-Path $ToolHelpPath) -eq $false)

# File Needs to be Updated

Write-host "Updating $tool Help File" -ForegroundColor Yellow 

Get-Content $tmpHelpFilePath | Out-File $ToolHelpPath

Remove-Item $tmpHelpFilePath

}

else

# If File Exists and Compare-Object is Null - No Change in File

{

Write-Host "No Change in $tool Help File" -ForegroundColor green

Remove-Item $tmpHelpFilePath

}

}

else

# No File - Create and Update

Write-host "Updating Help File" -ForegroundColor Yellow 

Get-Content $tmpHelpFilePath | Out-File $ToolHelpPath

Remove-Item $tmpHelpFilePath

}

}

#endRegion

}

elseif(! $sysTool -eq "") # No Updating - Just open the Help File

{

Get-Help about_$sysTool

}

}


function Convert-Html2Help {

param ($FileContent, $ShortDescription)

$text = "`nabout_$tool`n`rSHORT DESCRIPTION`n`t"

$text += Replace-HTMLSigns $ShortDescription 

$text += "`n`r`nLONG DESCRIPTION`n`t"

$text += Replace-HTMLSigns $FileContent 

return $text

}


Function Replace-HTMLSigns{

param ($text)

$cmap = @{

    "&lt;" = [char]"<"

    "&gt;" = [char]">"

"&nbsp;" = [char]" "

"&amp;" = [char]"&"

}

ForEach($chr in $cmap.Keys)

{

$text = $text.replace($chr,$cmap[$chr])

}

# for Description New Line

$text = $text.Replace([string]"<br />","`n")

$text = $text.Replace([string]"<p>","`n`n")

$sHTMLTagPat = [regex] "<\w *.*?>" #"<([a-zA-Z0-9\.\,\'_\/\-\+~=;:\(\)?&#%![\]@]+)>"

$eHTMLTagPat = [regex] "</\w *.*?>" #"</([a-zA-Z0-9\.\,\'_\/\-\+~=;:\(\)?&#%![\]@]+)>"

$text = $sHTMLTagPat.replace($text,"")

$text = $eHTMLTagPat.replace($text,"")

$text = HTML-WordWrap $text 75

return $text

}


Function HTML-WordWrap{

param ($InputText,$MaxLine)

Foreach($line in $InputText)

{

$arrWords = $line.split(" ")

$newText = ""

for ($i=0; $i -lt $arrWords.length; $i++) 

{

$lineLength = $newText.Length + $arrWords[$i].length

if ($lineLength -le $MaxLine) 

{

$newText+=$arrWords[$i]+" "

}

else 

{

$OutPutText += $newText + "`n`t"

$newText=$arrWords[$i]+" "

}

}

$OutPutText += $newText + "`n`t"

}

return $OutPutText

Comments