Thursday, September 11, 2014

Pop up window with PowerShell

PowerShell popup with use of xaml for formatting the output. Enjoy! I know I did, I used this with a gpo login script to help remind users what to do...

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
## creates the runspace
$run = [RunspaceFactory]::CreateRunspace()
$run.ApartmentState = “STA”
$run.Open()
## opens PowerShell.exe
$ps = {Add-Type -AssemblyName PresentationCore}.GetPowerShell()
$ps.Runspace = $run
## specify xaml formatting
$ps.AddScript({
    [xml]$xaml = @"
    <Window
        xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        x:Name='Window' WindowStartupLocation = 'CenterScreen' Width = '1000' Height = '500' ShowInTaskbar = 'True' WindowStyle = 'None' AllowsTransparency = 'true'>
        <Window.Background>
            <SolidColorBrush Opacity= '0' ></SolidColorBrush>
        </Window.Background>
        <Grid x:Name = 'Grid' ShowGridLines='false' >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height = '*'/>
            </Grid.RowDefinitions>
            <Viewbox Stretch = 'Fill'>
                <Label x:Name='Content' FontWeight = 'Bold' Content = 'PowerShell Rules!' FontSize = '30' FontStyle = 'Normal' Foreground = 'Blue' />
            </Viewbox>
        </Grid>
    </Window>
"@

    ## read xaml code
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Global:popup=[Windows.Markup.XamlReader]::Load( $reader )
    ## sets right click to "Close Window"
    $popup.Add_MouseRightButtonUp({$this.close()})
    ## sets left click to "Drag\move Window"
    $popup.Add_MouseLeftButtonDown({$This.DragMove()})
    ## Always on top of all other windows
    $popup.Topmost = $True
    $popup.ShowDialog()
}).BeginInvoke() | Out-Null


Cleanup WinSxS

Cleanup WinSxS with PowerShell, remove features on demand in Windows Server 2012.

001
Get-WindowsFeature | where-object{$_.Installed -eq 0 -and $_.InstallState -eq 'Available'}


001
Get-WindowsFeature | where-object{$_.Installed -eq 0 -and $_.InstallState -eq 'Available'| uninstall-windowsfeature -remove

Sunday, August 17, 2014

Bored?

The below script with go through a list of PowerShell links and open them each up one at a time within a single tab. Every 2 seconds a new article will appear -- when you find something good just hit [CTRL]+C to stop the script form continuing.



001
002
003
004
005
006
007
008
009
010
011

$blog="http://www.matthewkerfoot.com/p/powershell-links.html"
$ie = new-object -com internetexplorer.application
$iwr = Invoke-WebRequest -Uri $blog
$links = $iwr.Links | Select-Object -skip 7 -ExpandProperty href
foreach($l in $links){
                        $ie.visible=$true
                        $ie.navigate("$l")
                                while ($ie.Busy -eq $true){ 
                                                            Wait-Event -Timeout 2
                                }
}