Monday, January 2, 2017

Conway's Game of Life in PowerShell using WPF.  Slow but seemingly functional (let me know if there are any bugs).  Game pauses when mouse is hovering over board.  Beware of line breaks caused by "blogger."

#SCRIPT
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore

$Window = [System.Windows.Window]@{Width=450;Height=450}

$Global:GX_Size = 25
$Global:GY_Size = 25


$Life_Canvas = [System.Windows.Controls.Canvas]@{Background="#CCEEFF"};$Window.AddChild($Life_Canvas)


Function Compare-Boards ($Board1, $Board2){
    $Keys = $Board1.Keys

    foreach($Key in $Keys){
        $B1_Val = $Board1.$Key
        $B2_Val = $Board2.$Key
        if($B1_Val -ne $B2_Val){
            Write-Host -ForegroundColor Green -Object "$B1_Val != $B2_Val"
        }
        else{
            Write-Host -ForegroundColor Red -Object "$B1_Val = $B2_Val"
        }
    }
}

Function Draw-Cell($Parent,$CellWidth=20,$CellHeight=20,$State=1,$X, $Y, $Board){
    <#States
    1: Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    2: Any live cell with more than three live neighbours dies, as if by overcrowding.
    3: Any live cell with two or three live neighbours lives on to the next generation.
    4: Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    #>
    $CellPXWidth = $Parent.ActualWidth / ($CellWidth+1)
    $CellPXHeight = $Parent.ActualHeight / ($CellHeight+1)

    if([bool]$State){[System.Windows.Media.Brush]$BG = "Green"}else{[System.Windows.Media.Brush]$BG="Black"}

    $NewCell = [System.Windows.Shapes.Rectangle]@{Width=$CellPXWidth;Height=$CellPXHeight;Name="X$X`_Y$Y";Margin="$($X*$CellPXWidth),$($Y*$CellPXHeight)";HorizontalAlignment="Left";VerticalAlignment="Top";Fill=$BG;Stroke="#FF333333"}
   
    $Click_Change = {
       
        if([System.Windows.Input.Mouse]::LeftButton -ne "Pressed"){}
        else{
            $Board = $Global:Current_State
            $Name = $This.Name
   
            if($Board.$Name -eq 1){$Board.$Name = 0; $This.Fill = "Black"}
            else{$Board.$Name = 1;$This.Fill = "Green"}
        }
    }

    $NewCell.add_MouseEnter($Click_Change)  
    $NewCell.add_MouseDown($Click_Change)  

    $Parent.AddChild($NewCell)

}

Function Get-Tile($x, $y, $Board){
    if($x -lt 0){$x = $Board.Width}
    if($y -lt 0){$y = $Board.Height}
    if($x -gt $Board.Width){$x = 0}
    if($y -gt $Board.Height){$y = 0}
    $CoordString = "X$x`_Y$y"
   
    return @{Coord=$CoordString;Value=$Board.$CoordString}
}

Function Get-Neighbors ($xPos = 10, $yPos = 10, $Board){
    $LiveCount = 0
   
    $CheckedList = @()
   
    for($x = -1; $x -le 1; $x++){
        for($y = -1; $y -le 1; $y++){
            #Skip if self
            if($x -eq 0 -and $y -eq 0){continue}
            $TileData = Get-Tile -x ($xPos + $x) -y ($yPos + $y) -Board $Board
           
            #Skip if already checked; clipping purposes
            if($CheckedList -contains $TileData.Coord){continue}
            $CheckedList += $TileData.Coord
            $LiveCount += $TileData.Value
        }
    }


    return $LiveCount
}

Function Draw-Board ($X_Size=20, $Y_Size=20, $Current_State){
     
    $Next_State = @{Width=$X_Size;Height=$Y_Size;GUID=[guid]::NewGuid().Guid}

    for($x = 0; $x -le $X_Size; $x++){
        for($y = 0; $y -le $Y_Size; $y++){  
            $Coord = "X$x`_Y$y"
            $Count = Get-Neighbors -xPos $x -yPos $y -Board $Current_State
            #Write-Host $Count -ForegroundColor Cyan
            if($Current_State.$Coord -eq 1){
                #Write-Host "Die" -ForegroundColor Red
                if($Count -lt 2){$Next_State.$Coord = 0}
                if($Count -eq 2 -or $Count -eq 3){$Next_State.$Coord = 1}
                if($Count -gt 3){$Next_State.$Coord = 0}
            }
            elseif($Current_State.$Coord -eq 0){
           
                if($Count -eq 3){
                #Write-Host "Born" -ForegroundColor Cyan
                    $Next_State.$Coord = 1
                }
                else{$Next_State.$Coord = 0}
            }
        }
    }
    return $Next_State
}

#Initialize board
$Current_State = @{Width=$Global:GX_Size;Height=$Global:GY_Size;GUID=[guid]::NewGuid().Guid}
for($x = 0; $x -le $Global:GX_Size; $x++){
    for($y = 0; $y -le $Global:GY_Size; $y++){  
        #$Current_State."X$x`_Y$y" = (Get-Random @(1,0))
        $Current_State."X$x`_Y$y" = 0
    }
}
#Write-Host $Current_State.GUID -ForegroundColor Cyan

#Write-Host -ForegroundColor Cyan "Initializing board"
for($ix = 0; $ix -le $Global:GX_Size;$ix++){
    for($iy = 0; $iy -le $Global:GY_Size; $iy++){
        $Coord = "X$ix`_Y$iy"  
        Draw-Cell -Parent $Life_Canvas -CellWidth $Global:GX_Size -CellHeight $Global:GY_Size -State $Current_State.$Coord -X $ix -Y $iy -Board $Current_State
    }
}

$Global:Generation = 0
$GameLoop = {
    Write-Host ($Global:Generation++) -ForegroundColor Cyan
#    Write-Host -ForegroundColor Cyan "Running Game Loop"
    $Next_State = Draw-Board -X_Size $Global:GX_Size -Y_Size $Global:GY_Size -Current_State $global:Current_State
    $Life_Canvas.Children.Clear()
   
    #Write-Host -ForegroundColor Cyan "$($Next_State.X2_Y4)"
    for($ix = 0; $ix -le $Global:GX_Size;$ix++){
        for($iy = 0; $iy -le $Global:GY_Size; $iy++){
            $Coord = "X$ix`_Y$iy"  
            Draw-Cell -Parent $Life_Canvas -CellWidth $Global:GX_Size -CellHeight $Global:GY_Size -State $Next_State.$Coord -X $ix -Y $iy -Board $Next_State
        }
    }
    $global:Current_State = $Next_State
   
    #Write-Host $Current_State.GUID -ForegroundColor Cyan
}

$Refresher ={
    $global:timer = new-object System.Windows.Threading.DispatcherTimer
    $global:timer.Interval = [TimeSpan]"0:0:1"
    $global:timer.Add_Tick($GameLoop)
    $global:timer.Start()
}

$Window.add_Loaded($Refresher)
$Window.add_MouseEnter({$global:Timer.Stop()})
$Window.add_MouseLeave({$global:Timer.Start()})

$Window.ShowDialog()

Sunday, April 5, 2015

The futility of the lottery demonstrated with PowerShell

I wrote this a long time ago.  While we often hear how the odds are stacked against those who play lottery, this script will make you get a real feel for it.  When you run this you'll select your Powerball numbers and watch as your (not real) money flies out the window with live winnings/losses/#picks/etc.  I've let this run for hundreds of thousands of simulated picks and have never gotten more than the $100 prize.


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
$PlayPowerBall = {

Function Get-Five(){ $Result = @()
 While($Result.count -ne 5){
    for($i=0;$i -le 5$i++){
        $Result += (Get-Random -Minimum 1 -Maximum 59)
    }
    $Result = $Result | select -Unique -First 5 | sort}
$Result}
$Winnings = 0
cls
$WinningsTable = @()

$Numbers = @()

for($i=0;$i -le 4;$i++){$NumTest = 0
    while([int]$NumTest -gt [int]59 -or [int]$NumTest -lt [int]1 -or $Numbers -contains $NumTest){
    $NumTest = Read-Host "Please enter lottery number $($i+1) from 1 to 59"
    if([int]$NumTest -gt [int]59 -or [int]$NumTest -lt [int]0){Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. Enter a number between 1 and 59!"}
    if($Numbers -contains $NumTest){Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. No duplicate values allowed!"}
    }
$Numbers += $NumTest
}
$Numbers = $Numbers | sort{[int]$_}
$PB = 0
While([int]$PB -lt [int]1 -or [int]$PB -gt [int]35){
$PB = Read-Host "Please enter the powerball number from 1 to 35"
if([int]$PB -lt [int]1 -or [int]$PB -gt [int]35){
Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. Enter a number between 1 and 35!"}
}

$timer = [System.Diagnostics.Stopwatch]::StartNew()
$tries  = 0
$loop = $false
$ConsoleCounter = 0
while($loop -eq $false){
$Result = Get-Five
    $PBResult = Get-Random -Minimum 1 -Maximum 35

[psobject]$row = "" | select WinValue, PickCount, Matches, TimeElapsed

    switch("$((Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result).count),$($PB -eq $PBResult)"){
      
        "10,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 4;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize;$Winnings = $Winnings + 4}
        "8,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 4;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 4}
        "6,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 7;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 7}
        "4,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 7;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 7}
        "4,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 100;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 100}
        "2,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 100;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 100}
        "0,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 1000000;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 1000000}
        "0,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 160000000;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 160000000}
        }
      
    $tries++
 Write-Progress -Activity "Running picks against your numbers..." -CurrentOperation "Picks-> $tries Your Numbers: $($Numbers[0])-$($Numbers[1])-$($Numbers[2])-$($Numbers[3])-$($Numbers[4])---$PB) Winnings=`$$Winnings Losses=`$$($tries*2))"
    }
$timer.stop()

}

.$PlayPowerBall

Tuesday, March 3, 2015

Get-Uninstall Function

When using WMI (Win32_Product class) to collect and manipulate software on machines I found that I was not happy with the lack of information that the query would return.  This is because Win32_Product only seems to collect information for software that was installed using an MSI and is populated in the CLASSES part of the registry.  I wrote this function to collect information from the uninstall keys and build objects based on the information in those keys.  The function by default collects a small set of properties to make the collection quick and practical.  You can set the "full" switch on the function to get every more properties, which usually aren't necessarily useful.  In the future I would like to implement remote capability, add a script method to make uninstalling software easy, and detect registry/system comflicts (keys for software that were already uninstalled but not cleaned up properly; I'm looking at you Java and Adobe).

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
Function Get-Uninstall([switch]$Full){
    $GUID_Regex =  "\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b"

    $Table= @()
    $All_Properties = @()

    Switch(Get-WmiObject -Class Win32_OperatingSystem | select -ExpandProperty OSArchitecture){
        "32-Bit" {$Uninstall_Locations = @{32="HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"}}
        "64-Bit" {$Uninstall_Locations = @(@{32="HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"};@{64="HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"})}
    }

    $Full_Key_Paths = Foreach($Location in $Uninstall_Locations){
        $Uninstall_Root = Get-Item $Location.Values
        $Uninstall_Root.GetSubKeyNames() | %{$Uninstall_Root.OpenSubKey($_).Name -replace "HKEY_LOCAL_MACHINE", "HKLM:"}
    }

    if($Full){
        $Properties_Greedy += foreach($Key in $Full_Key_Paths){
            (Get-Item $Key).GetValueNames()
        }

        $All_Properties = $Properties_Greedy | select -Unique
        $All_Properties = $All_Properties | ?{$_ -ne ""}
    }
    else{$All_Properties = @("DisplayName","InstallDate","WindowsInstaller","DisplayVersion","UninstallString","Publisher","InstallSource","RegOwner","EstimatedSize")}
  


    $i = 0
    foreach($Key in $Full_Key_Paths){
    $i++
    Write-Progress -Activity "Processing Software" -Status "$i/$($Full_Key_Paths.Count) complete." -PercentComplete (($i/$Full_Key_Paths.Count)*100)
        $Hash = [ordered]@{}
        foreach($Prop in $All_Properties){
            $Hash.$Prop = (Get-Item $Key).GetValue($Prop)
        }
        if($Key -match "Wow6432Node"){$Hash.Architecture = "32-Bit"}
        elseif($Key -notmatch "Wow6432Node" -and $Uninstall_Locations.Count -eq 2){$Hash.Architecture = "64-Bit"}
        else{$Hash.Architecture = "32-Bit"}
        $Hash.GUID = [regex]::Match($Key,$GUID_Regex, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) | select -ExpandProperty Value
        $Table += (New-Object -TypeName psobject -Property $Hash)
    }
    $Table = $Table | ?{$_.DisplayName -ne "" -and $_.DisplayName -ne $null}
    $Table
}

Thursday, February 19, 2015

Roku Controls With PowerShell

This is a GUI I created that will interact with any Roku boxes that may be on your network.  Not only that, I was able to figure out how to convert some c# to PowerShell to send out SSDP broadcasts for network device discovery.  This enables the GUI to discover any Roku boxes without having to know the IP address off hand.  It is not complete, but works well.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
Add-Type -TypeDefinition @"
    public enum RokuCommands
    {
        Home,
        Rev,
        Fwd,
        Play,
        Select,
        Left,
        Right,
        Down,
        Up,
        Back,
        InstantReplay,
        Info,
        Backspace,
        Search,
        Enter,
    }
"@


#GetAppIDS http://192.168.1.134:8060/query/apps
$Global:WebClient = new-object System.Net.WebClient

Function Discover-RokuDevices($Port){
    #Use unused port or it will fail
    $LocalEndPoint = New-Object System.Net.IPEndPoint([ipaddress]::Any,$Port)

    $MulticastEndPoint = New-Object System.Net.IPEndPoint([ipaddress]::Parse("239.255.255.250"),1900)

    $UDPSocket = New-Object System.Net.Sockets.Socket([System.Net.Sockets.AddressFamily]::InterNetwork,[System.Net.Sockets.SocketType]::Dgram,[System.Net.Sockets.ProtocolType]::Udp)

    $UDPSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, [System.Net.Sockets.SocketOptionName]::ReuseAddress,$true)

    $UDPSocket.Bind($LocalEndPoint)

    $UDPSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP,[System.Net.Sockets.SocketOptionName]::AddMembership, (New-Object System.Net.Sockets.MulticastOption($MulticastEndPoint.Address, [ipaddress]::Any)))

    $UDPSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::MulticastTimeToLive, 2)

    $UDPSocket.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::IP, [System.Net.Sockets.SocketOptionName]::MulticastLoopback, $true)

    #Write-Host "UDP-Socket setup done...`r`n"
    #All SSDP Search
    $SearchString = "M-SEARCH * HTTP/1.1`r`nHOST:239.255.255.250:1900`r`nMAN:`"ssdp:discover`"`r`nST:ssdp:all`r`nMX:3`r`n`r`n"

    $UDPSocket.SendTo([System.Text.Encoding]::UTF8.GetBytes($SearchString), [System.Net.Sockets.SocketFlags]::None, $MulticastEndPoint| Out-Null

    #Write-Host "M-Search sent...`r`n"

    [byte[]]$RecieveBuffer = New-Object byte[] 64000

    [int]$RecieveBytes = 0

    $Response_RAW = ""
    $Timer = [System.Diagnostics.Stopwatch]::StartNew()
    $Delay = $True
    while($Delay){
        #15 Second delay so it does not run forever
        if($Timer.Elapsed.TotalSeconds -ge 5){Remove-Variable Timer$Delay = $false}
        if($UDPSocket.Available -gt 0){
            $RecieveBytes = $UDPSocket.Receive($RecieveBuffer, [System.Net.Sockets.SocketFlags]::None)

            if($RecieveBytes -gt 0){
                $Text = "$([System.Text.Encoding]::UTF8.GetString($RecieveBuffer, 0, $RecieveBytes))"
                $Response_RAW += $Text
            }
        }
    }

    $Rokus = [regex]::Matches($Response_RAW,"(http://)(\d{1,3}\.?){4}(:8060/)"| select -ExpandProperty value | select -Unique
    [regex]::Matches($Rokus, "(\d{1,3}\.)(\d{1,3}\.?){3}"| select -ExpandProperty value
}

Function Connect-Roku($IPAddress){
    $Global:Roku = "http://$($IPAddress):8060"
}

Function Send-RokuCommand([RokuCommands]$Command){
    $Global:WebClient.UploadString("$Global:Roku/keypress/$Command", "POST","")
}

Function Send-RokuText($Text,$RokuIPAddress){
    $Text.Replace(" ","%20")
    $Text.ToCharArray() | %{"$Roku/keypress/Lit_$_"#foreach{$Global:WebClient.UploadString("$Global:Roku/keypress/Lit_$_", "POST","")}
}

Function Launch-RokuApp($AppID){
    $WebClient.UploadString("$Global:Roku/launch/$AppID", "POST","")
}

Function Get-RokuApps(){
    ([xml]$WebClient.DownloadString("$Global:Roku/query/apps")).apps.app | select "#Text", ID
}

$RokuList = Discover-RokuDevices -Port 65433 
Connect-Roku -IPAddress $RokuList.Item(0)

####Build Form####
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form_Main = New-Object System.Windows.Forms.Form
$Form_Main.Size = [System.Drawing.Point]"640,480"

$Roku_Listbox = New-Object System.Windows.Forms.ListBox
$Roku_Listbox.Items.AddRange($RokuList)
$Roku_Listbox.SelectedIndex = 0
$Roku_Listbox.Add_SelectedIndexChanged({$Global:Roku = "http://$($Roku_Listbox.SelectedItem):8060"})
$Roku_Listbox.Size = [System.Drawing.Point]"100,50"
$Roku_Listbox.Location = [System.Drawing.Point]"10,10"
$Form_Main.Controls.Add($Roku_Listbox)


$Group_Arrows = New-Object System.Windows.Forms.GroupBox
$Group_Arrows.Size = [System.Drawing.Point]"150,100"
$Group_Arrows.Location = [System.Drawing.Point]"100,50"

$Arrow_up = New-Object System.Windows.Forms.Button
$Arrow_up.Text = [char]0x2191
$Arrow_up.Size = [System.Drawing.Point]"30,30"
$Arrow_up.Location = [System.Drawing.Point]"60,20"
$Arrow_up.Add_Click({Send-RokuCommand -Command Up})
$Group_Arrows.Controls.Add($Arrow_up)

$Arrow_Down = New-Object System.Windows.Forms.Button
$Arrow_Down.Text = [char]0x2193
$Arrow_Down.Size = [System.Drawing.Point]"30,30"
$Arrow_Down.Location = [System.Drawing.Point]"60,60"
$Arrow_Down.Add_Click({Send-RokuCommand -Command Down})
$Group_Arrows.Controls.Add($Arrow_Down)

$Arrow_Left = New-Object System.Windows.Forms.Button
$Arrow_Left.Text = [char]0x2190
$Arrow_Left.Size = [System.Drawing.Point]"30,30"
$Arrow_Left.Location = [System.Drawing.Point]"20,60"
$Arrow_Left.Add_Click({Send-RokuCommand -Command Left})
$Group_Arrows.Controls.Add($Arrow_Left)

$Arrow_Right = New-Object System.Windows.Forms.Button
$Arrow_Right.Text = [char]0x2192
$Arrow_Right.Size = [System.Drawing.Point]"30,30"
$Arrow_Right.Location = [System.Drawing.Point]"100,60"
$Arrow_Right.Add_Click({Send-RokuCommand -Command Right})
$Group_Arrows.Controls.Add($Arrow_Right)

$Apps = Get-RokuApps
$App_Box = New-Object System.Windows.Forms.ListBox
$App_Box.Items.AddRange(($Apps | select -ExpandProperty "#Text"))
$App_Box.Add_SelectedIndexChanged({$Global:AppSelection = $Apps | where{$_."#Text" -eq $App_Box.SelectedItem} | select -ExpandProperty id})
$App_Box.Size = [System.Drawing.Point]"150,200"
$App_Box.Location = [System.Drawing.Point]"400,10"

$App_Box_Fire = New-Object System.Windows.Forms.Button
$App_Box_Fire.Size = [System.Drawing.Point]"150,50"
$App_Box_Fire.Location = [System.Drawing.Point]"400,220"
$App_Box_Fire.Text = "Go to App!"
$App_Box_Fire.Add_Click({Launch-RokuApp -AppID $Global:AppSelection})

$Group_PlayControls = New-Object System.Windows.Forms.GroupBox
$Group_PlayControls.Size = [System.Drawing.Point]"200,200"
$Group_PlayControls.Location = [System.Drawing.Point]"100,150"

$Play_Pause = New-Object System.Windows.Forms.Button
$Play_Pause.Size = [System.Drawing.Point]"50,50"
$Play_Pause.Location = [System.Drawing.Point]"75,130"
$Play_Pause.Text = "$([char]0x34)`n$([char]0x3B)"
$Play_Pause.Font = New-Object System.Drawing.Font("Webdings",12,[System.Drawing.FontStyle]::Bold)
$Play_Pause.Add_Click({Send-RokuCommand -Command Play})
$Group_PlayControls.Controls.Add($Play_Pause)

$Reverse = New-Object System.Windows.Forms.Button
$Reverse.Size = [System.Drawing.Point]"50,50"
$Reverse.Location = [System.Drawing.Point]"25,130"
$Reverse.Text = "$([char]0x37)"
$Reverse.Font = New-Object System.Drawing.Font("Webdings",12,[System.Drawing.FontStyle]::Bold)
$Reverse.Add_Click({Send-RokuCommand -Command Rev})
$Group_PlayControls.Controls.Add($Reverse)

$Fast_Forward = New-Object System.Windows.Forms.Button
$Fast_Forward.Size = [System.Drawing.Point]"50,50"
$Fast_Forward.Location = [System.Drawing.Point]"125,130"
$Fast_Forward.Text = "$([char]0x38)"
$Fast_Forward.Font = New-Object System.Drawing.Font("Webdings",12,[System.Drawing.FontStyle]::Bold)
$Fast_Forward.Add_Click({Send-RokuCommand -Command Fwd})
$Group_PlayControls.Controls.Add($Fast_Forward)

$Replay = New-Object System.Windows.Forms.Button
$Replay.Size = [System.Drawing.Point]"50,50"
$Replay.Location = [System.Drawing.Point]"25,25"
$Replay.Text = "$([char]0x71)"
$Replay.Font = New-Object System.Drawing.Font("Webdings",12,[System.Drawing.FontStyle]::Bold)
$Replay.Add_Click({Send-RokuCommand -Command InstantReplay})
$Group_PlayControls.Controls.Add($Replay)

$Info = New-Object System.Windows.Forms.Button
$Info.Size = [System.Drawing.Point]"50,50"
$Info.Location = [System.Drawing.Point]"125,25"
$Info.Text = "*"
$Info.Font = New-Object System.Drawing.Font("Calibri",36,[System.Drawing.FontStyle]::Bold)
$Info.Add_Click({Send-RokuCommand -Command Info})
$Group_PlayControls.Controls.Add($Info)

$Enter = New-Object System.Windows.Forms.Button
$Enter.Size = [System.Drawing.Point]"150,50"
$Enter.Location = [System.Drawing.Point]"25,77"
$Enter.Text = "OK"
$Enter.Font = New-Object System.Drawing.Font("Calibri",12,[System.Drawing.FontStyle]::Bold)
$Enter.Add_Click({Send-RokuCommand -Command Enter})
$Group_PlayControls.Controls.Add($Enter)

#Add TextBox for input
#Home buton
#Back button
#Enter button does not work??

$Form_Main.Controls.Add($Group_PlayControls)
$Form_Main.Controls.Add($Group_Arrows)
$Form_Main.Controls.Add($App_Box)
$Form_Main.Controls.Add($App_Box_Fire)
$Form_Main.ShowDialog()

Thursday, February 12, 2015

Powershell WSUS Client Front End

This is a script that I wrote which uses the WSUS APIs in windows (COM) and presents the user with a GUI to select and install updates.  I was very happy to learn how to use a listview .NET control in this exercise.


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#####################
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'

function Show-Console {
   $consolePtr = [Console.Window]::GetConsoleWindow()
  #5 show
 [Console.Window]::ShowWindow($consolePtr, 5)
}

function Hide-Console {
    $consolePtr = [Console.Window]::GetConsoleWindow()
  #0 hide
 [Console.Window]::ShowWindow($consolePtr, 0)
}
#####################
#uncomment next line to hide console window to make things prettier if run from SCCM application/package for instance
#Hide-Console
Start-Process -FilePath "wuauclt.exe" -ArgumentList "/a /resetauthorization /detectnow"

#Alternate detection than wuauclt, not sure if it does the resetauthorization switch
#$AutoUpdate = New-Object -ComObject Microsoft.Update.AutoUpdate
#$AutoUpdate.DetectNow()

$UpdateCollection = New-Object -ComObject "Microsoft.Update.UpdateColl"
$Searcher = New-Object -ComObject "Microsoft.Update.Searcher"
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Downloader = $Session.CreateUpdateDownloader()
$Installer = New-Object -ComObject "Microsoft.Update.Installer"

#May be necessary to specify other parameters to use a WSUS server
#$Searcher.ServerSelection = 0 #Default
#$Searcher.Online = $False

$LoadingForm = New-Object System.Windows.Forms.Form
$LoadingForm.Size = [System.Drawing.Point]"300,300"
$LoadingForm.FormBorderStyle = "None"
$LoadingForm.TopMost = $true
$LoadingForm.BackColor = [System.Drawing.Color]::PaleTurquoise
$LoadingForm.StartPosition = "CenterScreen"
$LoadingLabel = New-Object System.Windows.Forms.Label
$LoadingLabel.Text = "Loading updates, please wait..."
$LoadingLabel.Location = [System.Drawing.Point]"25,100"
$LoadingLabel.Font = "Calibri, 25"
$LoadingLabel.Size = [System.Drawing.Point]"300,200"
$LoadingForm.Controls.Add($LoadingLabel)
$LoadingForm.Show()

#Allow update scan time to work
Start-Sleep -Seconds 30

$NotInstalled = $Searcher.Search("IsInstalled=0")

$LoadingForm.Close()
#>
########Begin Form
Function New-Button($Text,$X,$Y,$ScriptBlock){
    $Button = New-Object System.Windows.Forms.Button
    $Button.Size = [System.Drawing.Point]"200,50"
    $Button.Location = [System.Drawing.Point]"$x,$y"
    $Button.Text = $Text
    $Button.Add_Click($ScriptBlock)
    $Button
}

########Begin Form
   
$Form = New-Object System.Windows.Forms.Form
$Form.Size = [System.Drawing.Point]"800,600"
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = "FixedSingle"
$Form.TopMost = $true
$Form.MinimizeBox = $false
$Form.MaximizeBox = $false

$ListView = New-Object System.Windows.Forms.ListView
$ListView.Size = [System.Drawing.Point]"400,400"
$ListView.Location = [System.Drawing.Point]"50,50"
$ListView.CheckBoxes = $true
$ListView.View = [System.Windows.Forms.View]::Details
$ListView.Width = $Form.ClientRectangle.Width - 100
$ListView.Height = $Form.ClientRectangle.Height - 150
$ListView.Anchor = "Top, Left, Right, Bottom"

$ListView.Columns.Add("Title",-2) | Out-Null
$ListView.Columns.Add("GUID",0) | Out-Null
$ListView.Columns.Add("Needs Reboot",-2) | Out-Null
$ListView.add_ItemChecked({if(.$EvalChecks){$OKButton.Enabled = $true}else{$OKButton.Enabled = $false}})
   
Foreach($Update in $NotInstalled.Updates){
        if($Update.InstallationBehavior.CanRequestUserInput -eq $false){
        $ListViewItem = New-Object System.Windows.Forms.ListViewItem($Update.Title)
        #$ListViewItem.SubItems.Add($Update.KBArticleIDs.Item(0)) | Out-Null
        #$ListViewItem.SubItems.Add([int]($Update.MaxDownloadSize / 1MB)) | Out-Null
        #$ListViewItem.SubItems.Add($Update.LastDeploymentChangeTime.ToString()) | Out-Null
        $ListViewItem.SubItems.Add($Update.Identity.UpdateID) | Out-Null
        $ListViewItem.SubItems.Add($Update.RebootRequired.ToString()) | Out-Null
        $ListView.Items.Add($ListViewItem) | Out-Null
    }
}

$EvalChecks = {
    ($ListView.Items | select -ExpandProperty checked | ?{$_ -eq $true} | Measure-Object | select -ExpandProperty count) -gt 0
}

$InstallUpdates = {
    $Form.Hide()
    $CheckedUpdates = $ListView.Items | where{$_.Checked} | select -ExpandProperty SubItems | Where{$_.Text -match "\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b"} | select -ExpandProperty Text
    $Installer = New-Object -ComObject "Microsoft.Update.Installer"
    $UpdateCollection = New-Object -ComObject "Microsoft.Update.UpdateColl"
    $DownloadCollection =  New-Object -ComObject "Microsoft.Update.UpdateColl"

    Foreach($Selected_Update in $CheckedUpdates){
        $UpdateToInstall = $NotInstalled.Updates | Where{$_.Identity.UpdateID -in $Selected_Update}
        #Check if already downloaded, no need to duplicate effort
        if(!$UpdateToInstall.IsDownloaded){
            $DownloadCollection.Add($UpdateToInstall)
        }

        $UpdateCollection.Add($UpdateToInstall)
        if($Installer.RebootRequiredBeforeInstallation){[System.Windows.Forms.MessageBox]::Show("A reboot is required before the remaining updates can be installed.");$Form.Close();break}
    }
       
        $Downloader.Updates = $DownloadCollection
        $Downloader.Download()

        $Installer.Updates = $UpdateCollection
        $Installer.Install()
        $Form.Close()
}

$CancelButton = New-Button -Text "Cancel" -X 425 -Y 485 -ScriptBlock {$ListView.Items | %{$_.Checked = $false};$Form.Close()}
$OKButton = New-Button -Text "Install Selection" -X 175 -Y 485 -ScriptBlock $InstallUpdates

$CheckBox_Select_All = New-Object System.Windows.Forms.CheckBox
$CheckBox_Select_All.Size = [System.Drawing.Point]"100,20"
$CheckBox_Select_All.Location = [System.Drawing.Point]"55,25"
$CheckBox_Select_All.Text = "Select All"
$CheckBox_Select_All.Add_CheckedChanged({
    If($CheckBox_Select_All.Checked){
        $ListView.Items | %{$_.Checked = $true}
    }
    elseif(! $CheckBox_Select_All.Checked){
        $ListView.Items | %{$_.Checked = $false}
    }
})
$Form.Controls.Add($CheckBox_Select_All)

$Title = New-Object System.Windows.Forms.Label
$Title.Text = "The Chicoine Patching Tool"
$Title.Font = "Calibri,20"
$Title.Location = [System.Drawing.Point]"250,10"
$Title.Size = [System.Drawing.Point]"500,40"
   
$Form.Controls.Add($Title)
$Form.Controls.Add($ListView) | Out-Null
$Form.Controls.Add($OKButton)
$Form.Controls.Add($CancelButton)
$Form.Add_Shown({if(.$EvalChecks){$OKButton.Enabled = $true}else{$OKButton.Enabled = $false}})
#[System.Windows.Forms.Application]::Run($Form)
$Form.ShowDialog()