<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Lessons Learned</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/timid/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/timid/feed.xml</link>
    <description>My drop-site for interesting snippets and tips.  If it&amp;#039;s useful to you as well, great!</description>
    <lastBuildDate>Tue, 15 Mar 2016 02:58:30 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Active Directory - Creating Install From Media dump</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/active-directory-creating-install-from-media-dump</link>
      <pubDate>Tue, 01 Dec 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/12/01/active-directory-creating-install-from-media-dump/</guid>
      <description><![CDATA[function Backup-ActiveDirectoryIFM
{
    param (
    [string]$IFMPath,...]]></description>
      <content:encoded><![CDATA[<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<pre style="margin: 0; line-height: 125%;"><span style="color: #008800; font-weight: bold;">function</span> Backup-ActiveDirectoryIFM
{
    <span style="color: #008800; font-weight: bold;">param</span> (
    <span style="color: #003366; font-weight: bold;">[string]</span><span style="color: #996633;">$IFMPath</span>,
    <span style="color: #003366; font-weight: bold;">[switch]</span><span style="color: #996633;">$NoDefrag</span>
    )
    <span style="color: #996633;">$errorAction</span> = <span style="background-color: #fff0f0;">'stop'</span>
    <span style="color: #996633;">$minimumFreeSpace</span> = 0.5TB
    <span style="color: #996633;">$Mode</span> = <span style="background-color: #fff0f0;">'Full'</span>
    <span style="color: #008800; font-weight: bold;">if</span> (!<span style="color: #996633;">$IFMPath</span>)
    {
    <span style="color: #996633;">$driveObject</span> = <span style="color: #007020;">Get-PSDrive</span> |
        ? {
    <span style="color: #996633;">$_</span>.Free
        } |
    <span style="color: #007020;">Sort-Object</span> -Descending Free |
    <span style="color: #007020;">Select-Object</span> -First 1
    <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #996633;">$driveObject</span>.Free <span style="color: #333333;">-lt</span> <span style="color: #996633;">$minimumFreeSpace</span>)
        {
    <span style="color: #007020;">Write-Error</span> <span style="background-color: #fff0f0;">"Drive with largest free space </span><span style="background-color: #eeeeee;">$(</span><span style="color: #000000; background-color: #eeeeee;">$</span><span style="background-color: #eeeeee;">driveObject.Name)</span><span style="background-color: #fff0f0;">: has only </span><span style="background-color: #eeeeee;">$([int](</span><span style="color: #000000; background-color: #eeeeee;">$</span><span style="background-color: #eeeeee;">driveObject.Free/1GB)</span><span style="background-color: #fff0f0;">)GB free, which is less than the required minimum of </span><span style="background-color: #eeeeee;">$([int](</span><span style="color: #000000; background-color: #eeeeee;">$</span><span style="background-color: #eeeeee;">minimumFreeSpace/1GB)</span><span style="background-color: #fff0f0;">)GB."</span>
        }
    <span style="color: #996633;">$now</span> = <span style="color: #007020;">Get-Date</span>
    <span style="color: #996633;">$todayString</span> = <span style="color: #996633;">$now</span>.ToString(<span style="background-color: #fff0f0;">'yyyy-MM-dd'</span>)
    <span style="color: #996633;">$IFMPath</span> = <span style="background-color: #fff0f0;">"E:\NTDS_$todayString"</span>  <span style="color: #888888;">## This is where the IFM media set will be created</span>
    }
    <span style="color: #008800; font-weight: bold;">if</span> (<span style="color: #996633;">$noDefrag</span>)
    {
    <span style="color: #996633;">$NoDefragString</span> = <span style="background-color: #fff0f0;">'NoDefrag'</span>
    }
    <span style="color: #007020;">Write-Progress</span> -Activity <span style="background-color: #fff0f0;">"Create IFM $NoDefragString"</span> -Status <span style="color: #996633;">$IFMPath</span>
    ntdsutil.exe <span style="background-color: #fff0f0;">'activate instance ntds'</span> ifm <span style="background-color: #fff0f0;">"create sysvol $Mode $NoDefragString $IFMPath"</span> q q q | 
    <span style="color: #007020;">Tee-Object</span> -Variable IFMResult
    <span style="color: #996633;">$global:__IFMResult</span> = <span style="color: #996633;">$IFMResult</span>
}
</pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Converting Whitespace-Indented Output to XML</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/converting-whitespace-indented-output-to-xml</link>
      <pubDate>Fri, 20 Nov 2015 07:23:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/11/20/converting-whitespace-indented-output-to-xml/</guid>
      <description><![CDATA[Example of taking output delimited by indents and trying to beat it into XML.






 1
 2...]]></description>
      <content:encoded><![CDATA[<p>Example of taking output delimited by indents and trying to beat it into XML.</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;"> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"><span style="color: #0000ff;">function</span> Get-HPSmartArrayConfigAsXML
{
    <span style="color: #0000ff;">param</span> (

        [string]$HPSSACLIPath =  <span style="color: #a31515;">'C:\Program Files\HP\hpssacli\bin\hpssacli.exe'</span>,
        [string]$Path = <span style="color: #a31515;">"$home\Desktop\$env:ComputerName-hpssacli.xml"</span>
    )

    $ErrorActionPreference = <span style="color: #a31515;">'Stop'</span>

    <span style="color: #008000;"># test-path or die!!!</span>
    Get-Item $HPSSACLIPath | Out-Null

    $previousIndent = <span style="color: #a31515;">''</span>
    [System.Collections.ArrayList]$tagStack = <span>@</span>()
    [xml]$xml = <span style="color: #a31515;">'&lt;xml&gt;&lt;/xml&gt;'</span>
    $nodeStackPointer = $xml.FirstChild
    $lastNodePointer = $xml.FirstChild

    &amp; $HPSSACLIPath ctrl all show config |
    ? { 
        $_ -replace <span style="color: #a31515;">'^\s+'</span> -replace <span style="color: #a31515;">'\s+$'</span>
    } |
    % {
        <span style="color: #0000ff;">if</span> ($_ -match <span style="color: #a31515;">'^(?&lt;Indent&gt;\s*)(?&lt;Tag&gt;\S+)\s+(?&lt;Data&gt;.*)'</span>)
        {
            $thisLineIndent = $Matches.Indent
            $tagName        = $Matches.Tag
            $stringData     = $Matches.Data -replace <span style="color: #a31515;">' +'</span>, <span style="color: #a31515;">' '</span>

            $node = $xml.CreateElement($tagName)


            $data = $xml.CreateElement(<span style="color: #a31515;">'data'</span>)
            $node.AppendChild($data)

            $textNode = $xml.CreateTextNode($stringData)
            $data.AppendChild($textNode)

            <span style="color: #0000ff;">if</span> ($previousIndent.Length -eq $thisLineIndent.Length)
            {
                <span style="color: #008000;"># no op</span>
            }
            <span style="color: #0000ff;">elseif</span> ($previousIndent.Length -lt $thisLineIndent.Length)
            {
                Write-Verbose -Verbose <span style="color: #a31515;">"$(</span><span>$</span><span style="color: #a31515;">previousIndent.Length) -lt $(</span><span>$</span><span style="color: #a31515;">thisLineIndent.Length)"</span>
                Write-Verbose -Verbose <span style="color: #a31515;">"`$nodeStackPointer = '$(</span><span>$</span><span style="color: #a31515;">nodeStackPointer.LocalName)'"</span>
                Write-Verbose -Verbose <span style="color: #a31515;">"`$lastNodePointer = '$(</span><span>$</span><span style="color: #a31515;">lastNodePointer.LocalName)'"</span>
                $nodeStackPointer = $lastNodePointer
                $previousIndent = $thisLineIndent
            }
            <span style="color: #0000ff;">elseif</span> ($previousIndent.Length -gt $thisLineIndent.Length)
            {
                Write-Verbose -Verbose <span style="color: #a31515;">"$(</span><span>$</span><span style="color: #a31515;">previousIndent.Length) -GT $(</span><span>$</span><span style="color: #a31515;">thisLineIndent.Length)"</span>
                Write-Verbose -Verbose <span style="color: #a31515;">"`$nodeStackPointer = '$(</span><span>$</span><span style="color: #a31515;">nodeStackPointer.LocalName)'"</span>
                Write-Verbose -Verbose <span style="color: #a31515;">"`$lastNodePointer = '$(</span><span>$</span><span style="color: #a31515;">lastNodePointer.LocalName)'"</span>
                $nodeStackPointer = $nodeStackPointer.ParentNode
                $previousIndent = $thisLineIndent
            } 
        
            $nodeStackPointer.AppendChild($node)
            $lastNodePointer = $node
        }
        <span style="color: #0000ff;">else</span>
        {
            Write-Warning <span style="color: #a31515;">"Failed to parse '$_'."</span>
        }
    } | out-null

    $xml.Save($Path)

    Get-Item -Path $Path
}
</pre>
</td>
</tr>
</tbody>
</table>
</div>]]></content:encoded>
    </item>
    <item>
      <title>Moving Computer AD Objects</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/moving-computer-ad-objects</link>
      <pubDate>Thu, 19 Nov 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/11/19/moving-computer-ad-objects/</guid>
      <description><![CDATA[I'm just going to toss code up here from now own. Too busy to write nicely-formatted posts....]]></description>
      <content:encoded><![CDATA[<p>I'm just going to toss code up here from now own. Too busy to write nicely-formatted posts. Apologies.!</p>
<!-- HTML generated using hilite.me -->
<div style="background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;">
<table>
<tbody>
<tr>
<td>
<pre style="margin: 0; line-height: 125%;">  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141</pre>
</td>
<td>
<pre style="margin: 0; line-height: 125%;"><span style="color: #008000;">&lt;#</span>
<span style="color: #a31515;">.synopsis</span>
<span style="color: #008000;">Move AD Computer Object to specified OU</span>

<span style="color: #a31515;">.parameter</span><span style="color: #008000;"> ComputerName</span>
<span style="color: #008000;">Name of computer(s) to move</span>

<span style="color: #a31515;">.parameter</span><span style="color: #008000;"> Path</span>
<span style="color: #008000;">Text file(s) listing computers to move. Can contain blank lines and '# comments'</span>

<span style="color: #a31515;">.parameter</span><span style="color: #008000;"> OU</span>
<span style="color: #008000;">Target OU</span>

<span style="color: #a31515;">.example</span>
<span style="color: #008000;">Move-ADComputerToOU os2jp100med04</span>

<span style="color: #008000;">ComputerName OU Success</span>
<span style="color: #008000;">------------ -- -------</span>
<span style="color: #008000;">OS2JP100MED04 OU=DECOMM,OU=Non_Production True</span>

<span style="color: #a31515;">.example</span>
<span style="color: #008000;">Move-ADComputerToOU -Path .\jp1med.txt -OU Decomm </span>

<span style="color: #008000;">ComputerName OU Success</span>
<span style="color: #008000;">------------ -- -------</span>
<span style="color: #008000;">TY1JP100MED04 OU=DECOMM,OU=Non_Production True</span>
<span style="color: #008000;">TY1JP100MED05 OU=DECOMM,OU=Non_Production True</span>
<span style="color: #008000;">TY1JP100MED06 OU=DECOMM,OU=Non_Production True</span>

<span style="color: #008000;">#&gt;</span>

[<span style="color: #0000ff;">cmdletBinding</span>()]
<span style="color: #0000ff;">param</span> (
    [<span style="color: #0000ff;">parameter</span>(<span style="color: #0000ff;">ValueFromPipeLine</span>=$true,<span style="color: #0000ff;">Position</span>=0)]
    [string[]]$ComputerName,

    [string[]]$Path,

    [<span style="color: #0000ff;">parameter</span>(<span style="color: #0000ff;">Mandatory</span>=$true,<span style="color: #0000ff;">Position</span>=1)]
    [string]$OU
)

<span style="color: #0000ff;">function</span> Move-ADComputerToOU
{
    [<span style="color: #0000ff;">cmdletBinding</span>()]
    <span style="color: #0000ff;">param</span> (
        [<span style="color: #0000ff;">parameter</span>(<span style="color: #0000ff;">ValueFromPipeLine</span>=$true,<span style="color: #0000ff;">Position</span>=0)]
        [string[]]$ComputerName,

        [string[]]$Path,

        [<span style="color: #0000ff;">parameter</span>(<span style="color: #0000ff;">Mandatory</span>=$true,<span style="color: #0000ff;">Position</span>=1)]
        [string]$OU
    )

    <span style="color: #0000ff;">begin</span>
    {
        $ErrorActionPreference = <span style="color: #a31515;">'stop'</span>

        Import-Module ActiveDirectory

        [System.Collections.ArrayList]$toDo = @()    
        <span style="color: #0000ff;">try</span>
        {
            $ouObject = Get-ADOrganizationalUnit -LDAPFilter <span style="color: #a31515;">"(name=$ou)"</span>
            $ouDN = $ouObject.DistinguishedName
        }
        <span style="color: #0000ff;">catch</span>
        {
            Write-Warning $_.Exception.Message
            <span style="color: #0000ff;">break</span> __out
        }
    }

    <span style="color: #0000ff;">process</span>
    {
        $Path |
        ? { $_ } |
        % {
            <span style="color: #0000ff;">if</span> (Test-Path -Path $Path)
            {
                (Get-Content -Path $Path) -replace <span style="color: #a31515;">'\s'</span> -replace <span style="color: #a31515;">'#.*'</span> |
                ? { $_ } |
                % {
                    $ComputerName += $_
                }
            }
            <span style="color: #0000ff;">else</span>
            {
                Write-Warning <span style="color: #a31515;">"-Path '$Path' not found."</span>
            }
        }

        $ComputerName |
        ? { $_ } |
        % {
            <span style="color: #0000ff;">try</span>
            {
                $toDo += (
                    Get-AdComputer -Identity $_
                ).DistinguishedName
            }
            <span style="color: #0000ff;">catch</span>
            {
                Write-Warning <span style="color: #a31515;">"-ComputerName '$_' not found."</span>
            }
        }

    }

    <span style="color: #0000ff;">end</span>
    {
        $todo |
        Move-ADObject -TargetPath $ouDN

        $todo -replace <span style="color: #a31515;">',.*'</span> -replace <span style="color: #a31515;">'.*='</span> |
        Get-ADComputer |
        Select-Object @{
            n = <span style="color: #a31515;">'ComputerName'</span>
            e = {
                $_.Name
            }
        }, @{
            n = <span style="color: #a31515;">'OU'</span>
            e = {
                $_.DistinguishedName -replace <span style="color: #a31515;">',DC=.*'</span> -replace <span style="color: #a31515;">'^CN=[^,]+,'</span>
            }

        }, @{
            n = <span style="color: #a31515;">'Success'</span>
            e = {
                [bool]($_.DistinguishedName -match <span style="color: #a31515;">",OU=$ou"</span>)
            }
        }
    }
}

<span style="color: #0000ff;">if</span> ($ComputerName -or $Path)
{
    Move-ADComputerToOU @PSBoundParameters
}
</pre>
</td>
</tr>
</tbody>
</table>
</div>]]></content:encoded>
    </item>
    <item>
      <title>IE, ADFS, and Overkill</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/ie-adfs-and-overkill</link>
      <pubDate>Mon, 20 Jul 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/07/20/ie-adfs-and-overkill/</guid>
      <description><![CDATA[I do some testing that requires I not use Active Directory Federation Services (ADFS), which is an...]]></description>
      <content:encoded><![CDATA[<p>I do some testing that requires I not use Active Directory Federation Services (ADFS), which is an otherwise-convenient way to authenticate me against web sites via my Active Directory token.&#160; I used to start IE in Private Mode to do that, but it still sometimes-but-not-always redirects me to where I don’t want to go.&#160; Using IE in Private Mode made for difficult COM automation.</p>  <p>If I was sensible, I’d go to Internet Options | Security | Local intranet | Custom level | User Authentication (at the bottom of the scroll box) | Prompt for user name and password.</p>  <p>I’m not sensible.&#160; I have to use COM automation on IE, which already proves that.&#160; So, being that I’m not sensible, I went looking in the registry.</p>  <p>Disclaimer: the registry is not a public interface.&#160; What I’m posting here is for me to find in a year’s time, when I am scouring my brain for how I did something oh-so-long-ago.&#160; This is not a recommended or even a sane way to disable auto-log on.</p>  <p>   <div style="overflow: auto; border-top: gray 0.1em solid; border-right: gray 0.1em solid; width: auto; background: #ffffff; border-bottom: gray 0.1em solid; padding-bottom: 0.2em; padding-top: 0.2em; padding-left: 0.6em; border-left: gray 0.8em solid; padding-right: 0.6em">     <pre style="margin: 0px; line-height: 125%"><span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Enable and disable ADFS (Active Directory Federation Services in Internet Explorer</span>
<span style="color: #a31515">.link</span><span style="color: #008000"></span>
<span style="color: #008000">        https://support.software.dell.com/desktop-authority/kb/118560</span>
<span style="color: #008000">        Logon setting (1A00) may have any one of the following values (hexadecimal): </span>
<span style="color: #008000">        Int Value      Setting</span>
<span style="color: #008000">        --- -----      -------</span>
<span style="color: #008000">        0 0x00000 Automatically logon with current username and password</span>
<span style="color: #008000">        65536 0x10000 Prompt for user name and password</span>
<span style="color: #008000">        131072 0x20000 Automatic logon only in the Intranet zone</span>
<span style="color: #008000">        196608 0x30000 Anonymous logon</span>
<span style="color: #008000">        ===</span>
<span style="color: #008000">        Get-ItemProperty  'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\*' | </span>
<span style="color: #008000">        Select-Object -Property PSchildName, DisplayName, 1A00 | </span>
<span style="color: #008000">        Format-Table -AutoSize</span>
<span style="color: #008000">        PSChildName DisplayName        1A00</span>
<span style="color: #008000">        ----------- -----------        ----</span>
<span style="color: #008000">        0           Computer              0</span>
<span style="color: #008000">        1           Local intranet   131072</span>
<span style="color: #008000">        2           Trusted sites    131072</span>
<span style="color: #008000">        3           Internet         131072</span>
<span style="color: #008000">        4           Restricted sites  65536</span>
<span style="color: #008000">        ===</span>
<span style="color: #008000">        Get-ItemProperty  'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\*' | </span>
<span style="color: #008000">        Select-Object -Property PSchildName, DisplayName, 1A00 | </span>
<span style="color: #008000">        Format-Table -AutoSize</span>
<span style="color: #008000">        PSChildName DisplayName      1A00  </span>
<span style="color: #008000">        ----------- -----------      ----  </span>
<span style="color: #008000">        0           Computer               </span>
<span style="color: #008000">        1           Local intranet   65536 </span>
<span style="color: #008000">        2           Trusted sites    131072</span>
<span style="color: #008000">        3           Internet         131072</span>
<span style="color: #008000">        4           Restricted sites 65536 </span>
<span style="color: #008000">    </span>
<span style="color: #008000">#&gt;</span>
<span style="color: #008000">#region functions</span>
<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Disable ADFS (Active Directory Federation Services in Internet Explorer</span>
<span style="color: #a31515">.link</span><span style="color: #008000"></span>
<span style="color: #008000">https://support.software.dell.com/desktop-authority/kb/118560</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Get-InternetExplorerLogonSettings
{
                <span style="color: #0000ff">param</span> (
        [<span style="color: #0000ff">ValidateSet</span>(
                <span style="color: #a31515">'HKCU'</span>,
                <span style="color: #a31515">'HKLM'</span>
        )]
        [string[]]$Scope = @(
                <span style="color: #a31515">'HKCU'</span>,
                <span style="color: #a31515">'HKLM'</span>
        ),
        [<span style="color: #0000ff">ValidateSet</span>(
                <span style="color: #a31515">'Computer'</span>,
                <span style="color: #a31515">'Local_intranet'</span>,
                <span style="color: #a31515">'Trusted_sites'</span>, 
                <span style="color: #a31515">'Internet'</span>, 
                <span style="color: #a31515">'Restricted_sites'</span>
        )]
        [string[]]$Zone = @(
                <span style="color: #a31515">'Computer'</span>,
                <span style="color: #a31515">'Local_intranet'</span>,
                <span style="color: #a31515">'Trusted_sites'</span>, 
                <span style="color: #a31515">'Internet'</span>, 
                <span style="color: #a31515">'Restricted_sites'</span>
        ),
        [switch]$ShowPSPath
    )
    $myZone = $Zone -replace <span style="color: #a31515">'_'</span>, <span style="color: #a31515">' '</span>
                <span style="color: #0000ff">foreach</span> ($regKeyScope <span style="color: #0000ff">in</span> $Scope)
    {
        Get-ItemProperty <span style="color: #a31515">&quot;${regKeyScope}:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\*&quot;</span> | 
                <span style="color: #0000ff">ForEach</span>-Object {
            $outputObject = New-Object -TypeName psobject |
            Select-Object -Property Scope, Zone, Setting
            $outputObject.Scope   = $regKeyScope.ToUpper()
            $outputObject.Zone    = $_.DisplayName
            $outputObject.Setting = $Global:__IeLogonSettingsMap.([int]($_.<span style="color: #a31515">'1A00'</span>))
                <span style="color: #0000ff">if</span> ($ShowPSPath)
            {
                Add-Member -InputObject $outputObject -MemberType NoteProperty -Name PSPath -Value $_.PSPath
            }
            $outputObject
        
        } | 
        Where-Object { 
            $myZone -contains $_.Zone 
        }
    }
}
<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Set </span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Set-InternetExplorerLogonSettings
{
                <span style="color: #0000ff">param</span> (
        [<span style="color: #0000ff">ValidateSet</span>(
                <span style="color: #a31515">'HKCU'</span>,
                <span style="color: #a31515">'HKLM'</span>
        )]
        [string]$Scope = <span style="color: #a31515">'HKCU'</span>,
        [<span style="color: #0000ff">Parameter</span>(<span style="color: #0000ff">Mandatory</span>=$true)]
        [<span style="color: #0000ff">ValidateSet</span>(
                <span style="color: #a31515">'Computer'</span>,
                <span style="color: #a31515">'Local_intranet'</span>,
                <span style="color: #a31515">'Trusted_sites'</span>, 
                <span style="color: #a31515">'Internet'</span>, 
                <span style="color: #a31515">'Restricted_sites'</span>
        )]
        [string]$Zone,
        [<span style="color: #0000ff">Parameter</span>(<span style="color: #0000ff">Mandatory</span>=$true)]
        [<span style="color: #0000ff">ValidateSet</span>(
                <span style="color: #a31515">'Anonymous_logon'</span>,                                      <span style="color: #008000"># 196608</span>
                <span style="color: #a31515">'Automatic_logon_only_in_Intranet_zone'</span>,                <span style="color: #008000"># 131072</span>
                <span style="color: #a31515">'Automatic_logon_with_current_user_name_and_password'</span>,  <span style="color: #008000"># 0</span>
                <span style="color: #a31515">'Prompt_for_user_name_and_password'</span>                     <span style="color: #008000"># 65536</span>
        )]
        [string]$Setting
    )
    $mySetting = $Setting -replace <span style="color: #a31515">'_'</span>, <span style="color: #a31515">' '</span>
    $currentData = Get-InternetExplorerLogonSettings -Scope $Scope -Zone $Zone -ShowPSPath
                <span style="color: #0000ff">if</span> ($currentData.Setting -eq $mySetting)
    {
                <span style="color: #0000ff">return</span> (
            $currentData | 
            Select-Object -Property Scope, Zone, Setting
        )
    }
    $mySettingAsInt = $__IeLogonSettingsMap.$mySetting
    Set-ItemProperty -Path $currentData.PSPath -Name <span style="color: #a31515">'1A00'</span> -Value $mySettingAsInt
    ($currentData = Get-InternetExplorerLogonSettings -Scope $Scope -Zone $Zone)
                <span style="color: #0000ff">if</span> ($currentData.Setting -ne $mySetting)
    {
        Write-Warning <span style="color: #a31515">&quot;Unable to set ADFS setting in '$zone' zone for $scope scope to '$mySetting'.  It remains at '$(</span>$<span style="color: #a31515">currentData.Setting)'.&quot;</span>
    }
} <span style="color: #008000">#&gt;</span>
<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Enable ADFS (Active Directory Federation Services in Internet Explorer</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Enable-InternetExplorerAdfs
{
    Set-InternetExplorerLogonSettings -Scope HKCU -Zone Local_intranet -Setting Automatic_logon_only_in_Intranet_zone
} <span style="color: #008000">#&gt;</span>
<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Disable ADFS (Active Directory Federation Services in Internet Explorer</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Disable-InternetExplorerAdfs
{
    Set-InternetExplorerAdfsSetting -Scope HKCU -Zone Local_intranet -Setting Prompt_for_user_name_and_password
} <span style="color: #008000">#&gt;</span>
<span style="color: #008000">#endregion</span>
<span style="color: #008000">#region initialization</span>
<span style="color: #008000"># create mapping hashtable</span>
<span style="color: #0000ff">if</span> (!(Test-Path -Path Variable:__IeLogonSettingsMap))
{
    Set-Variable -Scope Global -Option ReadOnly -Name __IeLogonSettingsMap -Value @{
                <span style="color: #a31515">'Anonymous logon'</span>                                      = 196608
                <span style="color: #a31515">'Automatic logon only in Intranet zone'</span>                = 131072
                <span style="color: #a31515">'Automatic logon with current user name and password'</span>  = 0
                <span style="color: #a31515">'Prompt for user name and password'</span>                    = 65536
        196608 = <span style="color: #a31515">'Anonymous logon'</span>
        131072 = <span style="color: #a31515">'Automatic logon only in Intranet zone'</span>
        0 = <span style="color: #a31515">'Automatic logon with current user name and password'</span>
        65536 = <span style="color: #a31515">'Prompt for user name and password'</span>
    }
}
<span style="color: #008000"># warn if user and computer settings are different</span>
$currentSetting = Get-InternetExplorerLogonSettings
$hkcuData = $currentSetting |
Where-Object {
    $_.Scope -eq <span style="color: #a31515">'HKCU'</span>
} 
$hkcuData | 
<span style="color: #0000ff">ForEach</span>-Object {
    $zone    = $_.Zone
    $hklmData = $currentSetting |
    Where-Object {
        $_.Scope -eq <span style="color: #a31515">'HKLM'</span> -and
        $_.Zone  -eq $zone
    }
                <span style="color: #0000ff">if</span> ( $hklmData.Setting -ne $_.Setting )
    {
        Write-Warning -Message <span style="color: #a31515">&quot;ADFS setting in '$zone' zone for HKCU scope is '$(</span>$<span style="color: #a31515">_.Setting)', but for HKLM scope is '$(</span>$<span style="color: #a31515">hklmData.Setting)'.&quot;</span>
    }
}
Export-ModuleMember -Function * -Variable __IeLogonSettingsMap
<span style="color: #008000">#endregion</span></pre>
  </div></p>]]></content:encoded>
    </item>
    <item>
      <title>Backing Up BitLocker Keys to OneDrive as a Scheduled Task</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/backing-up-bitlocker-keys-to-onedrive-as-a-scheduled-task</link>
      <pubDate>Mon, 18 May 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/05/18/backing-up-bitlocker-keys-to-onedrive-as-a-scheduled-task/</guid>
      <description><![CDATA[While I can’t say I love Bitlocker, I do understand it as a requirement for any machine with...]]></description>
      <content:encoded><![CDATA[<p>While I can’t say I love Bitlocker, I do understand it as a requirement for any machine with corporate data.&#160; That said, it seems quite capricious when the BitLocker gremlin decides to require the 48-digit recovery key.&#160;&#160; I’ve taken to saving my recovery keys to OneDrive, so I can bring up the data on my phone when I need it.&#160; </p>  <h1>Easter Eggs</h1>  <p>These might not be worth a blog post by themselves, but are noteworthy enough for some text on their own.</p>  <h4>SkyDrive, Uh, OneDrive</h4>  <p>I dropped a second drive into my laptop (gotta love how the W530 can sport 3 hard disks!), so I pointed OneDrive to the existing files, now on drive D: instead of C:.&#160; My prior version of this hardcoded the path to $home\OneDrive\BitlockerRecovery, but that wasn’t the case anymore.&#160; And, I had to find out the hard way.&#160; So, here’s the first Easter egg: how to get your OneDrive path.</p>  <div style="list-style-type: disc; overflow: auto; border-top: black 1px solid; height: 173px; font-family: ; border-right: black 1px solid; width: 762px; border-bottom: black 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: black 1px solid; padding-right: 5px">   <table cellspacing="0" cellpadding="5" border="0"><tbody>       <tr>         <td valign="top">           <div style="font-family: ; background: #cecece; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">001                 <br />002                  <br />003                  <br />004                  <br />005                  <br />006                  <br />007                  <br />008                  <br />009                  <br />010</font></font>              <br /></div>         </td>          <td valign="top" nowrap="nowrap">           <div style="font-family: ; background: #fcfcfc; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><span style="color: "><font color="#ff4500"><font style="font-size: 10pt">$oneDriveRegKeyPath</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'HKCU:\Software\Microsoft\Windows\CurrentVersion\SkyDrive'</font></span><span style="color: "><font color="#000000">;</font></span>                  <br />                  <br /><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Test-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$oneDriveRegKeyPath</font></span></font></font><font style="font-size: 10pt"><font face="Consolas"><font color="#000000"><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Get-ItemProperty</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$oneDriveRegKeyPath</font></span><span style="color: "><font color="#000000">)</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">UserFolder</span><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">}</span></font>                  <br /><span style="color: "><font color="#00008b">else</font></span></font></font><font face="Consolas"><font style="font-size: 10pt"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;$home\OneDrive&quot;</font></span><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">}</span> </font></font></font></div>         </td>       </tr>     </tbody></table> </div>  <p>Yes, it’s still stored in the registry as SkyDrive, at least under Windows 8.1&#160; Please note that the Registry is what is termed a private interface – no one is making any guarantee that this regkey will be valid in the upcoming Windows 10.&#160;&#160; I’m just using what I found lying around.</p>  <h4>Run As Admin</h4>  <p>I could see requiring you to be admin to enable / disable / modify BitLocker settings, but even to view the recovery key requires you to be admin.&#160; Here’s a quick-and-dirty way to test if you’re running elevated or not:</p>  <div style="list-style-type: disc; overflow: auto; border-top: black 1px solid; font-family: ; border-right: black 1px solid; width: 750px; border-bottom: black 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: black 1px solid; padding-right: 5px">   <table cellspacing="0" cellpadding="5" border="0"><tbody>       <tr>         <td valign="top">           <div style="font-family: ; background: #cecece; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">001                 <br />002                  <br />003                  <br />004                  <br />005                  <br />006                  <br />007                  <br />008                  <br />009</font></font>              <br /></div>         </td>          <td valign="top" nowrap="nowrap">           <div style="font-family: ; background: #fcfcfc; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><span style="color: "><font color="#ff4500"><font style="font-size: 10pt">$user</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">New-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-TypeName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Security.Principal.WindowsPrincipal</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ArgumentList</font></span></font></font><font style="font-size: 10pt"><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#008080">[Security.Principal.WindowsIdentity]</font></span><span style="color: "><font color="#a9a9a9">::</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">GetCurrent</span><span style="color: ">(</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">)</span><span style="color: ">;</span></font>                  <br />                  <br /><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#a9a9a9">!</font></span><span style="color: "><font color="#ff4500">$user</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">IsInRole</span><span style="color: ">(</span></font><span style="color: "><font color="#008080">[Security.Principal.WindowsBuiltinRole]</font></span><span style="color: "><font color="#a9a9a9">::</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">Administrator</span><span style="color: ">)</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Write-Warning</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'Not running as Administrator. Exiting'</font></span></font></font><font face="Consolas"><font style="font-size: 10pt"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">return</font></span><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">}</span> </font></font></font></div>         </td>       </tr>     </tbody></table> </div>    <p>This Easter egg was used in some earlier scripts, too.</p>  <h4>Base64 Encoded Commands</h4>  <p>PowerShell.exe -Help details how to generate a Base64 encoded command from a string, and this shows how to auto-generate it (and how to test it) from a file.</p>  <div style="list-style-type: disc; overflow: auto; border-top: black 1px solid; font-family: ; border-right: black 1px solid; width: 750px; border-bottom: black 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: black 1px solid; padding-right: 5px">   <table cellspacing="0" cellpadding="5" border="0"><tbody>       <tr>         <td valign="top">           <div style="font-family: ; background: #cecece; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">001                 <br />002                  <br />003                  <br />004                  <br />005                  <br />006                  <br />007                  <br />008                  <br />009                  <br />010                  <br />011                  <br />012</font></font>              <br /></div>         </td>          <td valign="top" nowrap="nowrap">           <div style="font-family: ; background: #fcfcfc; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><span style="color: "><font color="#00008b"><font style="font-size: 10pt">function</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Convert-ThisFileToEncodedCommand                     <br /></font></span></font></font><font style="font-size: 10pt"><font face="Consolas"><font color="#000000"><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#ff4500">$scriptName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#a9a9a9">&amp;</font></span><span style="color: "><font color="#000000">{</font></span><span style="color: "><font color="#ff4500">$MyInvocation</font></span><font color="#000000"><span style="color: ">}</span><span style="color: ">)</span></font><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">ScriptName</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$command</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#008080">[string]</font></span><span style="color: "><font color="#000000">(</font></span><span style="color: "><font color="#0000ff">Get-Content</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$scriptName</font></span><font color="#000000"><span style="color: ">)</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">-replace</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'\s\s+'</font></span><span style="color: "><font color="#a9a9a9">,</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">' '</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#008080">[Convert]</font></span><span style="color: "><font color="#a9a9a9">::</font></span><font color="#000000"><span style="color: ">ToBase64String</span><span style="color: ">(</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#008080">[System.Text.Encoding]</font></span><span style="color: "><font color="#a9a9a9">::</font></span><span style="color: "><font color="#000000">Unicode</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">GetBytes</span><span style="color: ">(</span></font><span style="color: "><font color="#ff4500">$command</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br /><span style="color: ">}</span><span style="color: ">&#160;</span></font>                  <br />                  <br /><span style="color: "><font color="#00008b">function</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Test-ThisFileAsEncodedCommand</font></span></font></font><font face="Consolas"><font style="font-size: 10pt"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">powershell.exe</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-NoProfile</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-EncodedCommand</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Convert-ThisFileToEncodedCommand</font></span><font color="#000000"><span style="color: ">)</span>                    <br /><span style="color: ">}</span> </font></font></font></div>         </td>       </tr>     </tbody></table> </div>  <p>We’ll use the <font face="Consolas">Convert-ThisFileToEncodedCommand</font> by itself&#160; a little later.&#160; <font face="Consolas">Test-ThisFileAsEncodedCommand</font> makes sure our script will run when it’s encoded in base64.&#160; Here are some of the gotchas:</p>  <ul>   <li>No comments.&#160; <font face="Consolas">-EncodedCommand</font> expects a single command, so an unquoted ‘#’ in anything will cause the rest of the line to be ignored.&#160; Ungood.</li>    <li>No heretext.&#160; <font face="Consolas">@’. . .’@</font> and <font face="Consolas">@”. . .”@</font> won’t work because the terminating string is expected to start at column 0.</li>    <li>Semi; colons; everywhere;&#160; I; mean; <em><strong>EVERYWHERE</strong></em>;.&#160; Remember, <font face="Consolas">–EncodedCommand</font> expects it to be all in one line.&#160; This line can have multiple commands.&#160; It can even have functions, <font face="Consolas">trap{}</font>, etc.&#160; PowerShell, like Perl, is quite happy to be written in an obfuscated format, such as everything in a single line.&#160; We humans have a harder time.</li> </ul>  <h1>The Script</h1>  <p>Without further ado, here’s the script to back up your keys to OneDrive.</p>  <div style="list-style-type: disc; overflow: auto; border-top: black 1px solid; font-family: ; border-right: black 1px solid; width: 750px; border-bottom: black 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: black 1px solid; padding-right: 5px">   <table cellspacing="0" cellpadding="5" border="0"><tbody>       <tr>         <td valign="top">           <div style="font-family: ; background: #cecece; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">001                 <br />002                  <br />003                  <br />004                  <br />005                  <br />006                  <br />007                  <br />008                  <br />009                  <br />010                  <br />011                  <br />012                  <br />013                  <br />014                  <br />015                  <br />016                  <br />017                  <br />018                  <br />019                  <br />020                  <br />021                  <br />022                  <br />023                  <br />024                  <br />025                  <br />026                  <br />027                  <br />028                  <br />029                  <br />030                  <br />031                  <br />032                  <br />033                  <br />034                  <br />035                  <br />036                  <br />037                  <br />038                  <br />039                  <br />040                  <br />041                  <br />042                  <br />043                  <br />044                  <br />045                  <br />046                  <br />047                  <br />048                  <br />049                  <br />050                  <br />051                  <br />052                  <br />053                  <br />054                  <br />055                  <br />056                  <br />057                  <br />058                  <br />059                  <br />060                  <br />061                  <br />062                  <br />063                  <br />064                  <br />065                  <br />066                  <br />067                  <br />068                  <br />069                  <br />070                  <br />071                  <br />072                  <br />073                  <br />074                  <br />075                  <br />076                  <br />077                  <br />078                  <br />079                  <br />080                  <br />081                  <br />082                  <br />083                  <br />084                  <br />085                  <br />086                  <br />087                  <br />088                  <br />089                  <br />090</font></font>              <br /></div>         </td>          <td valign="top" nowrap="nowrap">           <div style="font-family: ; background: #fcfcfc; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><span style="color: "><font color="#00008b"><font style="font-size: 10pt">function</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Backup-BitLockerRecoveryKeys</font></span>                  <br /></font></font><font style="font-size: 10pt"><font face="Consolas"><font color="#000000"><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">trap</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#0000ff">Write-Warning</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$_</font></span><span style="color: "><font color="#a9a9a9">.</font></span><span style="color: "><font color="#000000">Exception</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">Message</span><span style="color: ">;</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#00008b">return</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span><span style="color: ">}</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$user</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">New-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-TypeName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Security.Principal.WindowsPrincipal</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ArgumentList</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#008080">[Security.Principal.WindowsIdentity]</font></span><span style="color: "><font color="#a9a9a9">::</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">GetCurrent</span><span style="color: ">(</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#a9a9a9">!</font></span><span style="color: "><font color="#ff4500">$user</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">IsInRole</span><span style="color: ">(</span></font><span style="color: "><font color="#008080">[Security.Principal.WindowsBuiltinRole]</font></span><span style="color: "><font color="#a9a9a9">::</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">Administrator</span><span style="color: ">)</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Write-Warning</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'Not running as Administrator. Exiting'</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">return</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$error</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">clear</span><span style="color: ">(</span><span style="color: ">)</span><span style="color: ">;</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$oneDriveRegKeyPath</font></span><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'HKCU:\Software\Microsoft\Windows\CurrentVersion\SkyDrive'</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Test-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$oneDriveRegKeyPath</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Get-ItemProperty</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$oneDriveRegKeyPath</font></span><span style="color: "><font color="#000000">)</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">UserFolder</span><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">else</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;$home\OneDrive&quot;</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$domainName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Get-WmiObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Class</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Win32_Computersystem</font></span><span style="color: "><font color="#000000">)</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">Domain</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$computerName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;$env:Computername.$domainName&quot;</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">ToLower</span><span style="color: ">(</span><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;$path\BitLockerRecovery\$computerName.txt&quot;</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Test-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">try</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#ff4500">$lastData</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">Import-Clixml</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">catch</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#0000ff">Remove-Item</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">else</font></span>                  <br /></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">New-Item</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ItemType</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Directory</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">|</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">Out-Null</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Remove-Item</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Export-Clixml</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-InputObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$null</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Get-BitLockerVolume</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">|</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Sort-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Property</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">MountPoint</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">|</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">%</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$bitlockerVolume</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$_</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$bitlockerVolume</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">KeyProtector</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">|</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">%</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$outputObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">New-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-TypeName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">PsObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">|</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Select-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Property</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Drive</font></span><span style="color: "><font color="#a9a9a9">,</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Guid</font></span><span style="color: "><font color="#a9a9a9">,</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Key</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$outputObject</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">Drive</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$bitlockerVolume</font></span><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">MountPoint</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$outputObject</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">GUID</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$_</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">KeyProtectorId</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">-replace</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'[{}]'</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#ff4500">$outputObject</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">Key</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$_</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">RecoveryPassword</span><span style="color: ">)</span><span style="color: ">&#160;</span><span style="color: ">{</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#ff4500">$outputObject</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">|</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#0000ff">Tee-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Variable</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">currentdata</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$lastData</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">-and</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Compare-Object</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ReferenceObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$currentData</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-DifferenceObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$lastData</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">)</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$lastWriteTime</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Get-Item</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">)</font></span><span style="color: "><font color="#a9a9a9">.</font></span><span style="color: "><font color="#000000">LastAccessTime</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">ToString</span><span style="color: ">(</span></font><span style="color: "><font color="#8b0000">'yyyy-MM-dd'</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$destination</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">-replace</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'\.txt'</font></span><span style="color: "><font color="#a9a9a9">,</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;($lastWriteTime).txt&quot;</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Move-Item</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Destination</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$destination</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Write-Host</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ForegroundColor</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Green</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;'$path' moved to '$destination'&quot;</font></span>                  <br /></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br />                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Export-Clixml</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-Path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$path</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-InputObject</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$currentData</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">Write-Host</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-ForegroundColor</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Green</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">&quot;Saved to '$path'&quot;</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span>                    <br /><span style="color: ">}</span></font>                  <br />                  <br /><span style="color: "><font color="#00008b">function</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Convert-ThisFileToEncodedCommand</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#00008b">if</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#ff4500">$scriptName</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#a9a9a9">&amp;</font></span><span style="color: "><font color="#000000">{</font></span><span style="color: "><font color="#ff4500">$MyInvocation</font></span><font color="#000000"><span style="color: ">}</span><span style="color: ">)</span></font><span style="color: "><font color="#a9a9a9">.</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">ScriptName</span><span style="color: ">)</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#ff4500">$command</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">=</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#008080">[string]</font></span><span style="color: "><font color="#000000">(</font></span><span style="color: "><font color="#0000ff">Get-Content</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#ff4500">$scriptName</font></span><font color="#000000"><span style="color: ">)</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#a9a9a9">-replace</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">'\s\s+'</font></span><span style="color: "><font color="#a9a9a9">,</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8b0000">' '</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">;</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font><span style="color: "><font color="#008080">[Convert]</font></span><span style="color: "><font color="#a9a9a9">::</font></span><font color="#000000"><span style="color: ">ToBase64String</span><span style="color: ">(</span><span style="color: ">&#160;</span></font><span style="color: "><font color="#008080">[System.Text.Encoding]</font></span><span style="color: "><font color="#a9a9a9">::</font></span><span style="color: "><font color="#000000">Unicode</font></span><span style="color: "><font color="#a9a9a9">.</font></span><font color="#000000"><span style="color: ">GetBytes</span><span style="color: ">(</span></font><span style="color: "><font color="#ff4500">$command</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">)</span><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span><span style="color: ">}</span>                    <br /><span style="color: ">}</span><span style="color: ">&#160;</span></font>                  <br />                  <br /><span style="color: "><font color="#00008b">function</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Test-ThisFileAsEncodedCommand</font></span></font><font face="Consolas"><font color="#000000"><span style="color: ">&#160;</span>                    <br /><span style="color: ">{</span><span style="color: ">&#160;</span>                    <br /><span style="color: ">&#160;&#160;&#160; </span></font><span style="color: "><font color="#0000ff">powershell.exe</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-NoProfile</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-EncodedCommand</font></span><font color="#000000"><span style="color: ">&#160;</span><span style="color: ">(</span></font><span style="color: "><font color="#0000ff">Convert-ThisFileToEncodedCommand</font></span></font></font><font face="Consolas"><font style="font-size: 10pt"><font color="#000000"><span style="color: ">)</span><span style="color: ">;</span>                    <br /><span style="color: ">}</span></font>                  <br />                  <br /><span style="color: "><font color="#0000ff">Backup-BitLockerRecoveryKeys</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#a9a9a9">|</font></span><span style="color: "><font color="#000000">&#160;</font></span>                  <br /><span style="color: "><font color="#0000ff">Format-List</font></span><span style="color: "><font color="#000000">;</font></span>                  <br /><span style="color: "><font color="#0000ff">Start-Sleep</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#800080">3</font></span></font><span style="color: "><font style="font-size: 10pt" color="#000000">;</font></span></font>              <br /></div>         </td>       </tr>     </tbody></table> </div>  <h1>   <br />The Scheduled Task</h1>  <p>The file below has three separate placeholders:</p>  <p><font face="Consolas">%USERDNSDOMAN%</font> is your AD domain, or your computer name if your computer is not AD-joined.&#160; </p>  <p><font face="Consolas">%USERNAME%</font> is your username.&#160; You need both to specify who you want your script to run as (you), which in turn is needed so you can run it with highest privileges (run as administrator, in other words.)</p>  <p><font face="Consolas">%ENCODEDCOMMAND%</font> is the output of <font face="Consolas">Convert-ThisFileToEncodedCommand</font>.&#160; This seems the most transparent way of sharing the string so you can be sure I’m not telling you to install a malicious scheduled task.</p>  <p>Replace the strings, save it as a XML file, open Task Scheduler and import the task.&#160; I have a new folder ‘User’ for my own scripts, but you can drop them into the main ‘Task Scheduler Library’ folder if you want.</p>  <div style="list-style-type: disc; overflow: auto; border-top: black 1px solid; font-family: ; border-right: black 1px solid; width: 750px; border-bottom: black 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: black 1px solid; padding-right: 5px">   <table cellspacing="0" cellpadding="5" border="0"><tbody>       <tr>         <td valign="top">           <div style="font-family: ; background: #cecece; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><font style="font-size: 10pt" color="#000000">001                  <br />002                   <br />003                   <br />004                   <br />005                   <br />006                   <br />007                   <br />008                   <br />009                   <br />010                   <br />011                   <br />012                   <br />013                   <br />014                   <br />015                   <br />016                   <br />017                   <br />018                   <br />019                   <br />020                   <br />021                   <br />022                   <br />023                   <br />024                   <br />025                   <br />026                   <br />027                   <br />028                   <br />029                   <br />030                   <br />031                   <br />032                   <br />033                   <br />034                   <br />035                   <br />036                   <br />037                   <br />038                   <br />039                   <br />040                   <br />041                   <br />042                   <br />043                   <br />044                   <br />045                   <br />046                   <br />047                   <br />048                   <br />049                   <br />050                   <br />051                   <br />052                   <br />053                   <br />054                   <br />055</font></font>               <br /></div>         </td>          <td valign="top" nowrap="nowrap">           <div style="font-family: ; background: #fcfcfc; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; padding-right: 5px"><font face="Consolas"><span style="color: "><font color="#a9a9a9"><font style="font-size: 10pt">&lt;</font></font></span><font style="font-size: 10pt"><span style="color: "><font color="#8a2be2">?xml</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">version=&quot;1.0&quot;</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">encoding=&quot;UTF-16&quot;?&gt;</font></span>                   <br /><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Task</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">version=&quot;1.2&quot;</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">xmlns=&quot;http://schemas.microsoft.com/windows/2004/02/mit/task&quot;&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">RegistrationInfo&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Date&gt;2014-08-11T12:14:26.4481786&lt;/Date&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Author&gt;%USERDNSDOMAIN%\%USERNAME%&lt;/Author&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/RegistrationInfo&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Triggers&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">CalendarTrigger&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">StartBoundary&gt;2014-08-11T10:00:00&lt;/StartBoundary&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Enabled&gt;true&lt;/Enabled&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">ScheduleByDay&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">DaysInterval&gt;1&lt;/DaysInterval&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/ScheduleByDay&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/CalendarTrigger&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">LogonTrigger&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Enabled&gt;true&lt;/Enabled&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">UserId&gt;%USERDNSDOMAIN%\%USERNAME%&lt;/UserId&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/LogonTrigger&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Triggers&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Principals&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Principal</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">id=&quot;Author&quot;&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">UserId&gt;%USERDNSDOMAIN%\%USERNAME%&lt;/UserId&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">LogonType&gt;InteractiveToken&lt;/LogonType&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">RunLevel&gt;HighestAvailable&lt;/RunLevel&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Principal&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Principals&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Settings&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">MultipleInstancesPolicy&gt;IgnoreNew&lt;/MultipleInstancesPolicy&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">DisallowStartIfOnBatteries&gt;true&lt;/DisallowStartIfOnBatteries&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">StopIfGoingOnBatteries&gt;true&lt;/StopIfGoingOnBatteries&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">AllowHardTerminate&gt;true&lt;/AllowHardTerminate&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">StartWhenAvailable&gt;false&lt;/StartWhenAvailable&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">RunOnlyIfNetworkAvailable&gt;false&lt;/RunOnlyIfNetworkAvailable&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">IdleSettings&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">StopOnIdleEnd&gt;true&lt;/StopOnIdleEnd&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">RestartOnIdle&gt;false&lt;/RestartOnIdle&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/IdleSettings&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">AllowStartOnDemand&gt;true&lt;/AllowStartOnDemand&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Enabled&gt;true&lt;/Enabled&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Hidden&gt;true&lt;/Hidden&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">RunOnlyIfIdle&gt;false&lt;/RunOnlyIfIdle&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">WakeToRun&gt;false&lt;/WakeToRun&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">ExecutionTimeLimit&gt;P3D&lt;/ExecutionTimeLimit&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Priority&gt;7&lt;/Priority&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Settings&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Actions</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">Context=&quot;Author&quot;&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Exec&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Command&gt;C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe&lt;/Command&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">Arguments&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#0000ff">-NoProfile</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#000080">-EncodedCommand</font></span><span style="color: "><font color="#000000">&#160;</font></span><span style="color: "><font color="#8a2be2">%ENCODEDCOMMAND%                     <br /></font></span></font></font><font face="Consolas"><font style="font-size: 10pt"><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Arguments&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">WorkingDirectory&gt;c:\temp&lt;/WorkingDirectory&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160;&#160;&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Exec&gt;</font></span>                   <br /><span style="color: "><font color="#000000">&#160; </font></span><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Actions&gt;</font></span>                   <br /><span style="color: "><font color="#a9a9a9">&lt;</font></span><span style="color: "><font color="#8a2be2">/Task&gt;</font></span><font color="#000000"> </font></font></font></div>         </td>       </tr>     </tbody></table> </div>        <p>An interesting gotcha comes from this as well: when you look at the task properties, you can see it’s a PowerShell task, but the encoded command string is so long that the arguments window is empty&#160; The encoded command string is visible only when you re-export the task.</p>]]></content:encoded>
    </item>
    <item>
      <title>Configuring the ISE</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/configuring-the-ise</link>
      <pubDate>Tue, 17 Feb 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/02/17/configuring-the-ise/</guid>
      <description><![CDATA[I tend to flatten my laptop about once a season. Among the other things I lose is my PowerShell_ISE...]]></description>
      <content:encoded><![CDATA[<p>I tend to flatten my laptop about once a season. Among the other things I lose is my PowerShell_ISE customizations. They’re not many, but I miss them when I have to live without them. Fortunately, they’re all captured in my Microsoft.PowerShellISE_profile.ps1:</p>  <div style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left">   <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span></span></span><span style="color: #006400"><span>#$PSModuleAutoLoadingPreference = 'None';</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>@(</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #8b0000"><span>&quot;C:\Users\timdunn\Config\PSHModules\ISESteroidsV1\ISESteroids.psd1&quot;</span></span><span style="color: #a9a9a9"><span>,</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #8b0000"><span>&quot;C:\Users\timdunn\Config\PSHModules\PSReadLine\PSReadline.psd1&quot;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>) </span></span><span style="color: #a9a9a9"><span>|</span></span><span><span> </span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span style="color: #0000ff"><span>%</span></span><span><span>{</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>if</span></span><span><span> (</span></span><span style="color: #0000ff"><span>Test-Path</span></span><span><span> </span></span><span style="color: #000080"><span>-Path</span></span><span><span> </span></span><span style="color: #ff4500"><span>$_</span></span><span><span>) { </span></span><span style="color: #0000ff"><span>Import-Module</span></span><span><span> </span></span><span style="color: #ff4500"><span>$_</span></span><span><span>; }</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>}</span></span></p>    <p></p>    <p></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span style="color: #00008b"><span>if</span></span><span><span> (</span></span><span style="color: #0000ff"><span>Test-Path</span></span><span><span> </span></span><span style="color: #000080"><span>-Path</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;C:\Users\timdunn\Config\PSHModules\Windows PowerShell ISE V3 Copy As HTML Add-On\Falchion.PowerShell.CopyAsHtmlAddOn.dll&quot;</span></span><span><span>)</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>{</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #0000ff"><span>Add-Type</span></span><span><span> </span></span><span style="color: #000080"><span>-Path</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;C:\Users\timdunn\Config\PSHModules\Windows PowerShell ISE V3 Copy As HTML Add-On\Falchion.PowerShell.CopyAsHtmlAddOn.dll&quot;</span></span><span><span>;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$copyAsHtmlAddOn</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>=</span></span><span><span> </span></span><span style="color: #0000ff"><span>New-Object</span></span><span><span> </span></span><span style="color: #8a2be2"><span>Falchion.PowerShell.CopyAsHtmlAddOn.CopyAsHtml</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$copyAsHtmlAddOn</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>HostObject </span></span><span style="color: #a9a9a9"><span>=</span></span><span><span> </span></span><span style="color: #ff4500"><span>$psISE</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>try</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; {</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$psISE</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>CurrentPowerShellTab</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>AddOnsMenu</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>Submenus</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>Add(</span></span><span style="color: #8b0000"><span>&quot;Copy as HTML&quot;</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> { </span></span><span style="color: #ff4500"><span>$copyAsHtmlAddOn</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>Copy() }</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;CTRL+SHIFT+C&quot;</span></span><span><span>) </span></span><span style="color: #a9a9a9"><span>|</span></span><span><span> </span></span><span style="color: #0000ff"><span>Out-Null</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; }</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>catch</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; {</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; }</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>}</span></span></p>    <p></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span style="color: #00008b"><span>function</span></span><span><span> </span></span><span style="color: #8a2be2"><span>prompt</span></span><span><span> </span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>{</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #8b0000"><span>&quot;`n# </span></span><span><span>$(</span></span><span style="color: #ff4500"><span>$env:USERDNSDOMAIN</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>ToLower())</span></span><span style="color: #8b0000"><span>\</span></span><span style="color: #ff4500"><span>$env:UserName</span></span><span style="color: #8b0000"><span>@</span></span><span style="color: #ff4500"><span>$env:computerName</span></span><span style="color: #8b0000"><span>.</span></span><span><span>$((</span></span><span style="color: #0000ff"><span>Get-WmiObject</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #8a2be2"><span>Win32_ComputerSystem</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>Domain)</span></span><span style="color: #8b0000"><span> &quot;</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>ToLower() </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> $(</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>if</span></span><span><span> (</span></span><span style="color: #ff4500"><span>$history</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>=</span></span><span><span> </span></span><span style="color: #0000ff"><span>Get-History</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>|</span></span><span><span> </span></span><span style="color: #0000ff"><span>Select-Object</span></span><span><span> </span></span><span style="color: #000080"><span>-last</span></span><span><span> </span></span><span style="color: #800080"><span>1</span></span><span><span>)</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (</span></span><span style="color: #ff4500"><span>$history</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>EndExecutionTime </span></span><span style="color: #a9a9a9"><span>-</span></span><span><span> </span></span><span style="color: #ff4500"><span>$history</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>StartExecutionTime)</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>ToString() </span></span><span style="color: #a9a9a9"><span>-replace</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;\..*&quot;</span></span><span><span>;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>else</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #8b0000"><span>&quot;00:00:00&quot;</span></span><span><span>;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </span></span></p>    <p></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; ) </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot; (</span></span><span><span>$(</span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>int</span></span><span style="color: #a9a9a9"><span>]</span></span><span><span>((</span></span><span style="color: #0000ff"><span>Get-History</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #a9a9a9"><span>|</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #0000ff"><span>Select-Object</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #000080"><span>-Last</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #800080"><span>1</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>ID)</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #a9a9a9"><span>+</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #800080"><span>1</span></span><span><span>)</span></span><span style="color: #8b0000"><span>)`n# </span></span><span><span>$(</span></span><span style="color: #0000ff"><span>Get-Location</span></span><span><span>)</span></span><span style="color: #8b0000"><span> </span></span><span><span>$(</span></span><span style="color: #0000ff"><span>Get-Date</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #000080"><span>-Format</span></span><span style="color: #8b0000"><span> 'yyyy-MM-dd_HH.mm.ss'</span></span><span><span>)</span></span><span style="color: #8b0000"><span> </span></span><span><span>$(</span></span><span style="color: #8b0000"><span>'&gt;' </span></span><span style="color: #a9a9a9"><span>*</span></span><span style="color: #8b0000"><span> </span></span><span style="color: #ff4500"><span>$nestedpromptlevel</span></span><span><span>)</span></span><span style="color: #8b0000"><span>&gt; `n&quot;</span></span><span><span>;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>}</span></span></p>    <p></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span style="color: #0000ff"><span>cd</span></span><span><span> </span></span><span style="color: #ff4500"><span>$home</span></span><span><span>;</span></span></p>    <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span style="color: #ff4500"><span>$Error</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>Clear();</span></span></p>  <p>As you can see, I use <a href="http://www.powertheshell.com/isesteroids/">ISESteroids</a>, <a href="https://github.com/lzybkr/PSReadLine/blob/master/README.md">PSReadline</a>, and <a href="http://blog.falchionconsulting.com/index.php/2012/10/windows-powershell-v3-ise-copy-as-html-add-on/">Copy As HTML</a>. The first one is payware, but I find it useful. PSReadline is actually more intended for console use; the command console under ISE is pretty good, but it lacks the concept of a <code>$Host.Ui.RawUI.WindowSize</code>, so stuff like pagination doesn’t work. Finally, Cop yAs HTML is the reason I’m writing this post. I’ve used probably three syntax formatters for PSH-to-HTML in the past, but seem to los them each time. Bing searches return ways to output HTML from PowerShell, or to use PSH to drive some other product to pretty-print HTML.</p>  <p>The past few articles saw me using <a href="http://hilite.me/">hilite.me</a>. It’s good, but insists on putting a red rectangle around my trailing semicolons, so I wanted to move off it and onto something better.</p>]]></content:encoded>
    </item>
    <item>
      <title>Left-Padding a String</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/left-padding-a-string</link>
      <pubDate>Mon, 16 Feb 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/02/16/left-padding-a-string/</guid>
      <description><![CDATA[I’ll be honest; .NET has more methods and classes and properties than I can shake an idiom at. In my...]]></description>
      <content:encoded><![CDATA[<p>I’ll be honest; .NET has more methods and classes and properties than I can shake an idiom at. In my previous blog post, I needed to left-pad a string with zeroes in order to treat it as a 32-bit binary value to <code>[Array]::Reverse()</code>. I also had to do the same for an 8-bit value, but the method is the same.</p>  <p><strong>Huge, </strong>huge gotcha with <code>[Array]::Reverse()</code>: it returns <code>void</code>, <em>not</em> the reversed array. It reverses the array in place.</p>  <p>So, I can convert an <code>[int]</code> to a string representing that value as binary, but if I’m reversing bits, then I need the binary-as-string to be 32 characters long. <code>[Convert]::ToString(8,2)</code> will return <code>100</code>, but I need it to be <code>00000000000000000000000000000100</code> so I can reverse it properly. Sure, I can play with <code>$string.Length</code> and all that, but here's a faster way.</p>  <p><div style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left">   <p style="font-family: lucida console; margin: 0px; background-color: #ffffff"><span><span>((</span></span><span style="color: #8b0000"><span>&quot;0&quot;</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>*</span></span><span><span> </span></span><span style="color: #800080"><span>31</span></span><span><span>) </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>convert</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>ToString(</span></span><span style="color: #ff4500"><span>$UInt32</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #800080"><span>2</span></span><span><span>)) </span></span><span style="color: #a9a9a9"><span>-replace</span></span><span><span> </span></span><span style="color: #8b0000"><span>'.*(.{32})$'</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;`$1&quot;</span></span></p>  <p>I know that the resulting string will be 32 characters long, and that the input string will be at least one character.  (I know this because I'm already bounds-checking before this.  You <em>do</em> bounds-check your input, don't you?)  I simply prepend a string of 31 zeroes to my input string, then RegEx out the rest. I don't even need to build out the RegEx as a string because I'm using the count-range-of-previous-(meta)character operator, the <code>{[int] [,[int]]}</code> specifier.  <code>\d{0,10}</code> means "between 0 and 10 digits" and <code>{[int]}</code> means <em>only</em> that many of the previous (meta)character.</p>  <p>Edit: Scratch out the part about <code>[Array]::Reverse()</code>  No, it still reverse the array in place and returns <code>void</code>, but I didn't need it in my current implementation.  I needed that static method in a prior one because <code>([IPAddress]$ipV4Address).Address</code> was a Big-Endian 32-bit number, and I needed convert it to Little-Endian so I could add an <code>[int]</code> to it (and then conver it back, etc.)  I'm just leaving in the part about <code>[Array]::Reverse()</code> because I had so much <em>fun</em> with it.</p>  <p>Yeah, let's just leave it at 'fun,' shall we?</p>]]></content:encoded>
    </item>
    <item>
      <title>Pass the CIDR, Will You?</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/pass-the-cidr-will-you</link>
      <pubDate>Fri, 13 Feb 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/02/13/pass-the-cidr-will-you/</guid>
      <description><![CDATA[Classless Inter-Domain Routing (e.g. 192.168.1.0/24) replaces dotted-notation netmasks (e.g....]]></description>
      <content:encoded><![CDATA[<p>Classless Inter-Domain Routing (e.g. 192.168.1.0/24) replaces dotted-notation netmasks (e.g. 192.168.1.0 with a subnet mask of 255.255.255.0) to do the same thing: what IP addresses are considered on-subnet (and therefore do not need to go through the default gateway) and which ones are not. In fact, both examples above are giving the same IPv4 address range, 192.168.1.0 – 192.168.1.255.</p>  <p>It gets more interesting when you get subnets like 192.168.1.96/30, or 192.168.1.96 with a subnet mask of 255.255.255.252. These both describe a 3-address range between 192.168.1.96 – 192.168.1.99. Or at least I think so; I’m not too sure about the netmask. These days, it appears CIDR is the common notation, so we’ll go with that.</p>  <p>Here are a few functions that will give you the IPv4 address range given a CIDR starting address (192.168.1.0) and prefix (24), which are separated by a forward slash. The starting address is straightforward. The prefix-to-IPv4-address-range conversion is pretty straightforward:</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/79/metablogapi/8546.Clipboard01_785285DA.gif" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-79-metablogapi/8546.Clipboard01_5F00_785285DA.gif"><img title="Clipboard01" style="border-top: 0px; border-right: 0px; border-bottom: 0px; float: none; margin-left: auto; border-left: 0px; display: block; margin-right: auto" border="0" alt="Clipboard01" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/79/metablogapi/2021.Clipboard01_thumb_18015FA3.gif" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-79-metablogapi/2021.Clipboard01_5F00_thumb_5F00_18015FA3.gif" width="244" height="61" /></a>( Please pardon the screen-shot. I haven’t been able to figure out how to insert something from MSWord’s Equation Editor into here.)</p>  <p>Notice that I said this is pretty straightforward, and it doesn’t exactly roll off the tongue. Adding an <code>[int]</code> worth of <code>[IPAddress]</code>es to an existing IPv4 address is not something the <code>[IPAddress]</code> class library seems to support. Nor comparators, so it’s difficult to determine if an IPv4 address is in an IPv4 address range. Now, <code>[IPAddress]</code> objects do have a .Address property, but as near as I can figure it, it’s a Big-Endian value. The class itself has static methods <code>[IPAddress]::NetworkToHostOrder()</code> and <code>[IPAddress]::HostToNetworkOrder()</code>, but those don’t seem to do what I need it to do to reverse the bit order.</p>  <p>That’s where the Perl-like write-only code in the <code>Convert-IPv4AddressToUInt32()</code> and <code>Convert-UInt32ToIPv4Address()</code> functions come in. By converting the IPv4 address to UInt32, we can then use the built-in comparators (–le and –ge) to determine if an IPv4 address is in a given IPv4 address range. Please note that I’m specifying –le and –ge. Remember that the upper and lowest addresses in the range are still part of the range.</p>  <p>This is also why the prefix-to-address-count conversion subtracts one at the end. 2 to the 0th power is 1. (Anything to the 0th power is 1, even 0. Try it.) The start of the IPv4 address range is already one address.</p>  <p>Oh, and remember that this <strong>only</strong> applies to IP<strong>v4</strong> addresses.  IP<strong>v6</strong> is a whole different ball game, and one I don't know how to play.</p>   <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span></span></span><span style="color: #006400"><span>#region CIDR</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>################################################################################</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>&lt;#######################################</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.synopsis</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>Convert an IPv4 address to a UInt32.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.description</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>Once an IPv4 Address is converted to a UInt32, an IP range can be added to it to determine the upper address of the range.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.parameter IpV4Address</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>Ipv$ address to convert to UIint 64.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.example</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>(Convert-IpV4AddressToUInt32 '102.81.52.0') + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt32ToIpV4Address</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>102.81.52.255</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #00008b"><span>function</span></span><span><span> </span></span><span style="color: #8a2be2"><span>Convert-IpV4AddressToUInt32</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>{</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>param</span></span><span><span> (</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #00bfff"><span>parameter</span></span><span><span>(mandatory</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>valuefrompipeline</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>helpmessage</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #8b0000"><span>'Example: 10.8.152.0'</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>]</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>ipAddress</span></span><span style="color: #a9a9a9"><span>]</span></span><span style="color: #ff4500"><span>$IpV4Address</span></span><span><span> </span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; );</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$bits</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>=</span></span><span><span> </span></span><span style="color: #8b0000"><span>''</span></span><span><span>;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$IpV4Address</span></span><span style="color: #a9a9a9"><span>.</span></span><span><span>ToString() </span></span><span style="color: #a9a9a9"><span>-split</span></span><span><span> </span></span><span style="color: #8b0000"><span>'\.'</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>|</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #0000ff"><span>%</span></span><span><span> { </span></span><span style="color: #ff4500"><span>$bits</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>+=</span></span><span><span> ((</span></span><span style="color: #8b0000"><span>&quot;0&quot;</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>*</span></span><span><span> </span></span><span style="color: #800080"><span>8</span></span><span><span>) </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>convert</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>ToString(</span></span><span style="color: #ff4500"><span>$_</span></span><span style="color: #a9a9a9"><span>,</span></span><span style="color: #800080"><span>2</span></span><span><span>)) </span></span><span style="color: #a9a9a9"><span>-replace</span></span><span><span> </span></span><span style="color: #8b0000"><span>'.*(.{8})$'</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;`$1&quot;</span></span><span><span>; }</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>convert</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>ToUInt32(</span></span><span style="color: #ff4500"><span>$bits</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #800080"><span>2</span></span><span><span>);</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>} </span></span><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>&lt;#######################################</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.synopsis</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>Convert a UInt32 back to an IPv4 address.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.description</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>Reverses Convert-IpV4AddresstoUInt32, after an IP range has been added.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.parameter UInt32</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>UInt32 to convert to IPv4 address.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.example</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>(Convert-IpV4AddressToUInt32 '102.81.52.0') + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt32ToIpV4Address</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>102.81.52.255</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #00008b"><span>function</span></span><span><span> </span></span><span style="color: #8a2be2"><span>Convert-UInt32ToIpV4Address</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>{</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>param</span></span><span><span> (</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #00bfff"><span>parameter</span></span><span><span>(mandatory</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>valuefrompipeline</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>helpmessage</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #8b0000"><span>'Example: 168335360'</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>]</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>UInt32</span></span><span style="color: #a9a9a9"><span>]</span></span><span style="color: #ff4500"><span>$UInt32</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; );</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>if</span></span><span><span> ((((</span></span><span style="color: #8b0000"><span>&quot;0&quot;</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>*</span></span><span><span> </span></span><span style="color: #800080"><span>31</span></span><span><span>) </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>convert</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>ToString(</span></span><span style="color: #ff4500"><span>$UInt32</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #800080"><span>2</span></span><span><span>)) </span></span><span style="color: #a9a9a9"><span>-replace</span></span><span><span> </span></span><span style="color: #8b0000"><span>'.*(.{32})$'</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> </span></span><span style="color: #8b0000"><span>&quot;`$1&quot;</span></span><span><span>) </span></span><span style="color: #a9a9a9"><span>-match</span></span><span><span> (</span></span><span style="color: #8b0000"><span>&quot;(.{8})&quot;</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>*</span></span><span><span> </span></span><span style="color: #800080"><span>4</span></span><span><span>))</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; {</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$ipV4Addr</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>=</span></span><span><span> </span></span><span style="color: #8b0000"><span>''</span></span><span><span>;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #800080"><span>1</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>..</span></span><span><span> </span></span><span style="color: #800080"><span>4</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>|</span></span><span><span> </span></span><span style="color: #0000ff"><span>%</span></span><span><span> { </span></span><span style="color: #ff4500"><span>$ipV4Addr</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>+=</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>string</span></span><span style="color: #a9a9a9"><span>]</span></span><span><span>(</span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>convert</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>ToInt16(</span></span><span style="color: #ff4500"><span>$matches</span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #ff4500"><span>$_</span></span><span style="color: #a9a9a9"><span>],</span></span><span><span> </span></span><span style="color: #800080"><span>2</span></span><span><span>)) </span></span><span style="color: #a9a9a9"><span>+</span></span><span><span> </span></span><span style="color: #8b0000"><span>'.'</span></span><span><span>; }</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #ff4500"><span>$ipV4Addr</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>-replace</span></span><span><span> </span></span><span style="color: #8b0000"><span>'\.$'</span></span><span><span>;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; }</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>} </span></span><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>&lt;#######################################</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.synopsis</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>For given CIDR prefix, return number of IP addresses in that range.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.description</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>For given CIDR prefix n (such as 24), number of addresses in the range is:</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>(2 ^ (32 - n)) - 1</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.parameter CidrPrefix</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>CIDR prefix. The 'n' in the notation &quot;192.168.1.0/24&quot;.</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>.example</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>(Convert-IpV4AddressToUInt32 '102.81.52.0') + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt32ToIpV4Address</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>102.81.52.255</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #00008b"><span>function</span></span><span><span> </span></span><span style="color: #8a2be2"><span>Get-IpV4AddressRangeFromCidrPrefix</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>{</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #00008b"><span>param</span></span><span><span> (</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #00bfff"><span>validateRange</span></span><span><span>(</span></span><span style="color: #800080"><span>0</span></span><span style="color: #a9a9a9"><span>,</span></span><span style="color: #800080"><span>32</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>]</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #00bfff"><span>parameter</span></span><span><span>(mandatory</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>valuefrompipeline</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #ff4500"><span>$true</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span>helpmessage</span></span><span style="color: #a9a9a9"><span>=</span></span><span style="color: #8b0000"><span>'Example: 10.8.152.0'</span></span><span><span>)</span></span><span style="color: #a9a9a9"><span>]</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>int</span></span><span style="color: #a9a9a9"><span>]</span></span><span style="color: #ff4500"><span>$CidrPrefix</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; );</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>&#160;&#160;&#160; </span></span><span style="color: #a9a9a9"><span>[</span></span><span style="color: #008080"><span>math</span></span><span style="color: #a9a9a9"><span>]::</span></span><span><span>Pow(</span></span><span style="color: #800080"><span>2</span></span><span style="color: #a9a9a9"><span>,</span></span><span><span> (</span></span><span style="color: #800080"><span>32</span></span><span><span> </span></span><span style="color: #a9a9a9"><span>-</span></span><span style="color: #ff4500"><span>$CidrPrefix</span></span><span><span>)) </span></span><span style="color: #a9a9a9"><span>-</span></span><span><span> </span></span><span style="color: #800080"><span>1</span></span><span><span>;</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span>} </span></span><span style="color: #006400"><span>#&gt;</span></span></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: segoe ui; font-weight: normal; color: #000000; font-style: normal; text-align: left"></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>################################################################################</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span style="color: #006400"><span>#endregion</span></span></p>  <p style="font-family: lucida console; font-weight: normal; color: #000000; font-style: normal; text-align: left; margin: 0px; background-color: #ffffff"><span><span></span></span></p>]]></content:encoded>
    </item>
    <item>
      <title>One-Liner: Left-Padding a String with Zeroes</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/one-liner-left-padding-a-string-with-zeroes</link>
      <pubDate>Thu, 12 Feb 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/02/12/one-liner-left-padding-a-string-with-zeroes/</guid>
      <description><![CDATA[Yesterday’s post had a little bit of sleight-of-hand involved.&#160; For the IPv4 address to UInt64...]]></description>
      <content:encoded><![CDATA[<p>Yesterday’s post had a little bit of sleight-of-hand involved.&#160; For the IPv4 address to UInt64 conversion to work, each octet had to be converted into an 8-character string of ones and zeroes.&#160; However, [convert]::ToString($int, 2) doesn’t zero pad, and this is now a string, not an Int, so I can’t use .Net’s ToString() formatting line noise.&#160; This means that the last octet of 192.168.1.0 is a single ‘0’, not an 8 character string.&#160; Instead, this is what I did:</p> <!-- HTML generated using hilite.me -->  <div style="overflow: auto; border-top: gray 0.1em solid; border-right: gray 0.1em solid; width: auto; background: #ffffff; border-bottom: gray 0.1em solid; padding-bottom: 0.2em; padding-top: 0.2em; padding-left: 0.6em; border-left: gray 0.8em solid; padding-right: 0.6em">   <pre style="margin: 0px; line-height: 125%">$bits += ((<span style="color: #a31515">&quot;0&quot;</span> * 7) + [convert]<span style="border-top: #ff0000 1px solid; border-right: #ff0000 1px solid; border-bottom: #ff0000 1px solid; border-left: #ff0000 1px solid">::</span>ToString($_,2)) -replace <span style="color: #a31515">'.*(.{8})$'</span>, <span style="color: #a31515">&quot;`$1&quot;</span><span style="border-top: #ff0000 1px solid; border-right: #ff0000 1px solid; border-bottom: #ff0000 1px solid; border-left: #ff0000 1px solid">;</span> </pre>
</div>

<p>I prepend 7 zeroes to by the string returned by ToString($_,2).&#160; This means at even a single digit is now 8 characters long.&#160; </p>

<p>That’s great, but what if the octet’s value is 255, which is 8 ‘1’ character?&#160; That would make it 15 characters long, right? </p>

<p>Well, we only need the 8 starting from the right side.&#160; Originally, I tried casting it to [char[]], then taking elements out of the array with negative indexes.&#160; (You <em>did</em> know that $array[-1] returns the last element in the array?&#160; That wasn’t as easy to read as I’d like.</p>

<p>Instead, RegEx to the rescue.&#160; The RegEx pattern, ‘.*(.{8}$’, replaces all but the last 8 characters with nothing (removes them.)&#160; “(.{8}$” specified those last 8 characters from end-of-line (“^”).&#160; Search-and-replace in .NET seems to be designed by someone who hated Perl.&#160; It’s quite painful.&#160; Here, I say this because “`$1” is the way to specify “the matched string in the parenthesis.”&#160; ‘$1’ doesn’t work.&#160; It has to be “`$1”.</p>]]></content:encoded>
    </item>
    <item>
      <title>IPv4 Addresses and CIDR Fun</title>
      <link>https://docs.microsoft.com/archive/blogs/timid/ipv4-addresses-and-cidr-fun</link>
      <pubDate>Wed, 11 Feb 2015 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[timdunn]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timid/2015/02/11/ipv4-addresses-and-cidr-fun/</guid>
      <description><![CDATA[Classless Inter-Domain Routing is the current way to define a subnet.&#160; An example entry would...]]></description>
      <content:encoded><![CDATA[<p>Classless Inter-Domain Routing is the current way to define a subnet.&#160; An example entry would be “192.168.1.0/24”.&#160; What this means is that the first 24 bits of the 32 bit IPv4 address are considered ‘off the subnet.’&#160; The last octet is considered part of the subnet.&#160; I.e. the computer will not route traffic through the default gateway if the destination address is on the same subnet.&#160; In this example, all machines on 192.168.1.xyz c an talk to one another.</p>  <p>/24 is simple.&#160; That’s just the last octet.&#160; What if people want to sub divide it further?&#160; 193.168.1.128.0/25 and 192.168.1.128/25 further divide that last octect into two separate subnets.</p>  <p>Also, what is considered on the subnet, IPv4 address wise?&#160; Assuming you’ve calculated the highest IP address in the range defined by the CIDR record, it’s relatively simple, at least for people.&#160; Look at each octet, side by side.&#160; Make sure the IPv4 address in question is at least the same, if not greater than, the start of the CIDR range.&#160; Then compare it to the upper bound.</p>  <p>For computers, that could be a lot more work:</p>  <p>- Split the IPv4 address into the four octets.</p>  <p>- Compare first octet, compare second only, etc.</p>  <p>- Two comparisons each: the octet being compared must be greater or equal to the first entry in the range, and less than the upper entry in the range.</p>  <!-- HTML generated using hilite.me --><div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Convert an IPv4 address to a UInt64.</span>

<span style="color: #a31515">.description</span><span style="color: #008000"></span>
<span style="color: #008000">Once an IPv4 Address is converted to a UInt64, an IP range can be added to it to determine the upper address of the range.</span>

<span style="color: #a31515">.parameter</span><span style="color: #008000"> IpV4Address</span>
<span style="color: #008000">Ipv$ address to convert to UIint 64.</span>

<span style="color: #a31515">.example</span><span style="color: #008000"></span>
<span style="color: #008000">(Convert-IpV4AddressToUInt64 &#39;10.8.152.0&#39;) + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt64ToIpV4Address</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Convert-IpV4AddressToUInt64
{
    <span style="color: #0000ff">param</span> (
        [<span style="color: #0000ff">parameter</span>(<span style="color: #0000ff">mandatory</span>=$true,<span style="color: #0000ff">valuefrompipeline</span>=$true,<span style="color: #0000ff">helpmessage</span>=<span style="color: #a31515">&#39;Example: 10.8.152.0&#39;</span>)]
        [ipAddress]$IpV4Address 
    )<span style="border: 1px solid #FF0000">;</span>
    
    $bits = <span style="color: #a31515">&#39;&#39;</span><span style="border: 1px solid #FF0000">;</span>
    $IpV4Address.ToString() -split <span style="color: #a31515">&#39;\.&#39;</span> |
    % { $bits += ((<span style="color: #a31515">&quot;0&quot;</span> * 8) + [convert]<span style="border: 1px solid #FF0000">::</span>ToString($_,2)) -replace <span style="color: #a31515">&#39;.*(.{8})$&#39;</span>, <span style="color: #a31515">&quot;`$1&quot;</span><span style="border: 1px solid #FF0000">;</span> }

    [convert]<span style="border: 1px solid #FF0000">::</span>ToUInt64($bits, 2)
} <span style="color: #008000">#&gt;</span>


<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">Convert a UInt64 back to an IPv4 address.</span>

<span style="color: #a31515">.description</span><span style="color: #008000"></span>
<span style="color: #008000">Reverses Convert-IpV4AddresstoUInt64, after an IP range has been added.</span>

<span style="color: #a31515">.parameter</span><span style="color: #008000"> UInt64</span>
<span style="color: #008000">UInt64 to convert to IPv4 address.</span>

<span style="color: #a31515">.example</span><span style="color: #008000"></span>
<span style="color: #008000">(Convert-IpV4AddressToUInt64 &#39;10.8.152.0&#39;) + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt64ToIpV4Address</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Convert-UInt64ToIpV4Address
{
    <span style="color: #0000ff">param</span> (
        [<span style="color: #0000ff">parameter</span>(<span style="color: #0000ff">mandatory</span>=$true,<span style="color: #0000ff">valuefrompipeline</span>=$true,<span style="color: #0000ff">helpmessage</span>=<span style="color: #a31515">&#39;Example: 168335360&#39;</span>)]
        [uint64]$UInt64
    )

    <span style="color: #0000ff">if</span> ((((<span style="color: #a31515">&quot;0&quot;</span> * 31) + [convert]<span style="border: 1px solid #FF0000">::</span>ToString($UInt64, 2)) -replace <span style="color: #a31515">&#39;.*(.{32})$&#39;</span>, <span style="color: #a31515">&quot;`$1&quot;</span>) -match (<span style="color: #a31515">&quot;(.{8})&quot;</span> * 4))
    {
        $ipV4Addr = <span style="color: #a31515">&#39;&#39;</span><span style="border: 1px solid #FF0000">;</span>
        1 .. 4 | % { $ipV4Addr += [string]([convert]<span style="border: 1px solid #FF0000">::</span>ToInt16($matches[$_], 2)) + <span style="color: #a31515">&#39;.&#39;</span><span style="border: 1px solid #FF0000">;</span> }
        $ipV4Addr -replace <span style="color: #a31515">&#39;\.$&#39;</span><span style="border: 1px solid #FF0000">;</span>
    }
} <span style="color: #008000">#&gt;</span>


<span style="color: #008000">&lt;#</span>
<span style="color: #a31515">.synopsis</span><span style="color: #008000"></span>
<span style="color: #008000">For given CIDR prefix, return number of IP addresses in that range.</span>

<span style="color: #a31515">.description</span><span style="color: #008000"></span>
<span style="color: #008000">For given CIDR prefix n (such as 24), number of addresses in the range is:</span>

<span style="color: #008000">(2 ^ (32 - n)) - 1</span>

<span style="color: #a31515">.parameter</span><span style="color: #008000"> CidrPrefix</span>
<span style="color: #008000">CIDR prefix. The &#39;n&#39; in the notation &quot;192.168.1.0/24&quot;.</span>

<span style="color: #a31515">.example</span><span style="color: #008000"></span>
<span style="color: #008000">(Convert-IpV4AddressToUInt64 &#39;10.8.152.0&#39;) + (Get-IpV4AddressRangeFromCidrPrefix 24) | Convert-UInt64ToIpV4Address</span>
<span style="color: #008000">#&gt;</span>
<span style="color: #0000ff">function</span> Get-IpV4AddressRangeFromCidrPrefix
{
    <span style="color: #0000ff">param</span> (
        [<span style="color: #0000ff">validateRange</span>(0,32)]
        [<span style="color: #0000ff">parameter</span>(<span style="color: #0000ff">mandatory</span>=$true,<span style="color: #0000ff">valuefrompipeline</span>=$true,<span style="color: #0000ff">helpmessage</span>=<span style="color: #a31515">&#39;Example: 10.8.152.0&#39;</span>)]
        [int]$CidrPrefix
    )<span style="border: 1px solid #FF0000">;</span>

    [math]<span style="border: 1px solid #FF0000">::</span>Pow(2, (32 -$CidrPrefix)) - 1<span style="border: 1px solid #FF0000">;</span>
} <span style="color: #008000">#&gt;</span>
</pre></div>]]></content:encoded>
    </item>
  </channel>
</rss>