PowerShell WPF for Service Status

I don’t what to say but I was really busy to do my stuffs. I just came to post some interesting part of PowerShell that I did for my own happiness (hahahaha, i don’t know what to say but powershell is really an interesting tool than can rule everything ).

My PowerShell WPF Project

I was trying a new perspective to use WPF on PowerShell rather than I used XML, it’s more fun. well, it’s not a quite good stuff but not bad either (I think).

So here’s the source code. If you really want to ask me, feel free to do that.

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.ComponentModel

#Create Main Windows
$Windows = New-Object System.Windows.Window
$Windows.Title = "PowerShell WPF Windows"
$Windows.Width = 400
$Windows.Height = 200
$Windows.AllowsTransparency = $true
$Windows.Background = [System.Windows.Media.Brushes]::Black
$Windows.WindowStyle = [System.Windows.WindowStyle]::None
$Windows.Opacity = 0.8
#Create Grid
$WindowsGrid = New-Object System.Windows.Controls.Grid
#Create ComboBox
$ComboBox = New-Object System.Windows.Controls.ComboBox
$ComboBox.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$ComboBox.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$ComboBox.Margin = [System.Windows.Thickness]"23,22,0,0"
$ComboBox.Width = 212
#Create Close Button
$CloseRec = New-Object System.Windows.Shapes.Rectangle
$CloseRec.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$CloseRec.VerticalAlignment  = [System.Windows.VerticalAlignment]::Top
$CloseRec.Margin = [System.Windows.Thickness]"390,0,0,0"
$CloseRec.Fill = [System.Windows.Media.Brushes]::Red
$CloseRec.Width = 10
$CloseRec.Height = 10
#Label
$Label1 = New-Object System.Windows.Controls.Label
$Label1.Content = "Service Status`t: "
$Label1.Foreground = [System.Windows.Media.Brushes]::White
$Label1.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$Label1.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$Label1.Margin = [System.Windows.Thickness]"23,58,0,0"

$Label2 = New-Object System.Windows.Controls.Label
$Label2.Content = "Startup Type`t: "
$Label2.Foreground = [System.Windows.Media.Brushes]::White
$Label2.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$Label2.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$Label2.Margin = [System.Windows.Thickness]"23,84,0,0"

$Label3 = New-Object System.Windows.Controls.Label
$Label3.Foreground = [System.Windows.Media.Brushes]::White
$Label3.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$Label3.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$Label3.Margin = [System.Windows.Thickness]"23,115,0,0"

$LabelService = New-Object System.Windows.Controls.Label
$LabelService.Content = "None"
$LabelService.Foreground = [System.Windows.Media.Brushes]::White
$LabelService.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$LabelService.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$LabelService.Margin = [System.Windows.Thickness]"127,58,0,0"

$LabelType = New-Object System.Windows.Controls.Label
$LabelType.Content = "None"
$LabelType.Foreground = [System.Windows.Media.Brushes]::White
$LabelType.HorizontalAlignment = [System.Windows.HorizontalAlignment]::Left
$LabelType.VerticalAlignment = [System.Windows.VerticalAlignment]::Top
$LabelType.Margin = [System.Windows.Thickness]"127,84,0,0"

#Main Action
$Services = @()
foreach($Service in (Get-Service))
{
    $Services += $Service.Name
}
$ComboBox.ItemsSource = $Services

#Event Handler
$CloseRec.Add_MouseLeftButtonDown({
    $Windows.Close()
})
$Windows.Add_MouseLeftButtonDown({
    $Windows.DragMove()
})
$ComboBox.Add_SelectionChanged({
    $SelectedService = $ComboBox.SelectedItem.ToString();
    $Label3.Content = (Get-Service | ? {$_.Name -eq $SelectedService}).DisplayName
    $LabelType.Content = (Get-CimInstance win32_service | ? {$_.Name -eq $SelectedService}).StartMode
    $LabelService.Content = (Get-CimInstance win32_service | ? {$_.Name -eq $SelectedService}).State

    if($LabelType.Content -eq "Auto")
    {
        $LabelType.Foreground = [System.Windows.Media.Brushes]::Green
    }
    elseif($LabelType.Content -eq "Manual")
    {
        $LabelType.Foreground = [System.Windows.Media.Brushes]::Orange
    }
    elseif($LabelType.Content -eq "Disabled")
    {
        $LabelType.Foreground = [System.Windows.Media.Brushes]::Red
    }

    if($LabelService.Content -eq "Running")
    {
        $LabelService.Foreground = [System.Windows.Media.Brushes]::Green
    }
    elseif($LabelService.Content -eq "Stopped")
    {
        $LabelService.Foreground = [System.Windows.Media.Brushes]::Red
    }
})

#Load Windows
$WindowsGrid.Children.Add($ComboBox) | Out-Null
$WindowsGrid.Children.Add($CloseRec) | Out-Null
$WindowsGrid.Children.Add($Label1) | Out-Null
$WindowsGrid.Children.Add($Label2) | Out-Null
$WindowsGrid.Children.Add($LabelService) | Out-Null
$WindowsGrid.Children.Add($LabelType) | Out-Null
$WindowsGrid.Children.Add($Label3) | Out-Null
$Windows.Content = $WindowsGrid
$Windows.ShowDialog() | Out-Null

Have Fun!!

Raka Satria

Playing Lumia 800 (My Friend’s Smartphone) vs HTC 8S (My Smartphone) in Traditional Way

In Indonesia, there were many traditional games but as time goes by they are replaced by digital devices. So, me and my friend decided to use our own smartphone in a traditional game. It’s called “TEPOKAN”. The rule is so simple, just put the smartphone on your palm then do the high five. Check it out in our video !!

Do you wanna play with us ??

Have Fun!!

Raka Satria

Playing Movie Using PowerShell (PowerShell Video Player)

Hufh….!!
I just bored with my daily activities and I decided to make some fun with powershell.
Now, We will try to playing Movie using PowerShell.
I used WPF library + XAML for that. so, let’s see it 😀

#WPF Library for Playing Movie and some components
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.ComponentModel
#XAML File of WPF as windows for playing movie
[xml]$XAML = @"

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PowerShell Video Player" Height="355" Width="553" ResizeMode="NoResize">
    <Grid Margin="0,0,2,3">
        <MediaElement Height="250" Width="525" Name="VideoPlayer" LoadedBehavior="Manual" UnloadedBehavior="Stop" Margin="8,10,10,61" />
        <Button Content="Pause" Name="PauseButton" HorizontalAlignment="Left" Margin="236,283,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Play" Name="PlayButton" HorizontalAlignment="Left" Margin="236,283,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>
"@

#Movie Path
[uri]$VideoSource = "C:UsersRakaDownloadsHiVi!  - Orang Ke-3 Official MV HD.mp4"

#Devide All Objects on XAML
$XAMLReader=(New-Object System.Xml.XmlNodeReader $XAML)
$Window=[Windows.Markup.XamlReader]::Load( $XAMLReader )
$VideoPlayer = $Window.FindName("VideoPlayer")
$PauseButton = $Window.FindName("PauseButton")
$PlayButton = $Window.FindName("PlayButton")

#Video Default Setting
$VideoPlayer.Volume = 100;
$VideoPlayer.Source = $VideoSource;
$VideoPlayer.Play()
$PauseButton.Visibility = [System.Windows.Visibility]::Visible
$PlayButton.Visibility = [System.Windows.Visibility]::Hidden

#Button click event 
$PlayButton.Add_Click({
    $VideoPlayer.Play()
    $PauseButton.Visibility = [System.Windows.Visibility]::Visible
    $PlayButton.Visibility = [System.Windows.Visibility]::Hidden
})
$PauseButton.Add_Click({
    $VideoPlayer.Pause()
    $PauseButton.Visibility = [System.Windows.Visibility]::Hidden
    $PlayButton.Visibility = [System.Windows.Visibility]::Visible
})

#Show Up the Window 
$Window.ShowDialog() | out-null

Here’s a Screenshot of PowerShell Video Player
PowerShell Movie Player

I knew that it’s not good enough, but not too bad 😛
I will create new version of it (if i need to…. hahaha)
LOL!! PowerShell is so much fun!!

Have fun with it!!

Raka Satria