Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===========================================================================
- # Assemblies and XAML
- #===========================================================================
- Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
- [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
- [xml]$XAML = @'
- <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:WpfApp3"
- Name="window" WindowStyle="None" Width="350" Height="295" Opacity="0.85" AllowsTransparency="True">
- <Window.Resources>
- <Style TargetType="GridViewColumnHeader">
- <Setter Property="Background" Value="Transparent" />
- <Setter Property="Foreground" Value="Transparent"/>
- <Setter Property="BorderBrush" Value="Transparent"/>
- <Setter Property="FontWeight" Value="Bold"/>
- <Setter Property="Opacity" Value="0.5"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="GridViewColumnHeader">
- <Border Background="Transparent">
- <ContentPresenter></ContentPresenter>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <Grid Name="grid" Height="418" HorizontalAlignment="Left" VerticalAlignment="Top">
- <Label Name="Title" Content="System Information" HorizontalAlignment="Left" VerticalAlignment="Top" Width="513" Background="#313130" Foreground="White" FontWeight="Bold" FontSize="14" Height="27"/>
- <Label Content="User Name" HorizontalAlignment="Left" Margin="0,27,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Computer Name" HorizontalAlignment="Left" Margin="0,57,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Domain" HorizontalAlignment="Left" Margin="0,87,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="IP Address" HorizontalAlignment="Left" Margin="0,117,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Connection Type" HorizontalAlignment="Left" Margin="0,147,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Operating System Name" HorizontalAlignment="Left" Margin="0,177,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Operating System Version" HorizontalAlignment="Left" Margin="0,207,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
- <Label Content="Free Space" HorizontalAlignment="Left" Margin="0,237,0,0" Width="170" Background="#313130" Foreground="White" Height="30" VerticalAlignment="Top"/>
- <Label Content="Last Boot Time" HorizontalAlignment="Left" Margin="0,267,0,0" Width="170" Background="#313130" Foreground="White" Height="30" VerticalAlignment="Top"/>
- <TextBox Name="txtUserName" Height="30" Margin="170,27,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" IsEnabled="True" AllowDrop="True" BorderThickness="0.5" HorizontalAlignment="Left" Width="343"/>
- <TextBox Name="txtHostName" Height="30" Margin="170,57,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" IsEnabled="True" AllowDrop="True" BorderThickness="0.5" HorizontalAlignment="Left" Width="343"/>
- <TextBox Name="txtDomain" Height="30" Margin="170,87,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" IsEnabled="True" AllowDrop="True" BorderThickness="0.5" HorizontalAlignment="Left" Width="343"/>
- <TextBox Name="txtWindowsIP" HorizontalAlignment="Left" Height="30" Margin="170,117,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <TextBox Name="txtConnectionType" HorizontalAlignment="Left" Height="30" Margin="170,147,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <TextBox Name="txtOSName" HorizontalAlignment="Left" Height="30" Margin="170,177,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <TextBox Name="txtOSVersion" HorizontalAlignment="Left" Height="30" Margin="170,207,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <TextBox Name="txtSystemFree" HorizontalAlignment="Left" Height="30" Margin="170,237,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <TextBox Name="txtLastBoot" HorizontalAlignment="Left" Height="30" Margin="170,267,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
- <ListView Name="listview" SelectionMode="Single" Foreground="White" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False">
- <ListView.ItemContainerStyle>
- <Style>
- <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
- <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
- </Style>
- </ListView.ItemContainerStyle>
- </ListView>
- </Grid>
- </Window>
- '@
- #===========================================================================
- #Read XAML
- #===========================================================================
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
- catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}
- #===========================================================================
- # Store Form Objects In PowerShell
- #===========================================================================
- $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
- Function RefreshData{
- #===========================================================================
- # Stores WMI values in WMI Object from System Classes
- #===========================================================================
- $user = @()
- $oWMIOS = @()
- $oWMICS = @()
- $oWMIHD = @()
- $oWMINIC = @()
- $connectionType = @()
- $lastBoot = @()
- $user = ((Get-WmiObject -Class Win32_Process -Filter 'Name="explorer.exe"').GetOwner().User)
- $oWMIOS = Get-WmiObject win32_OperatingSystem
- $oWMICS = Get-WmiObject -Class Win32_ComputerSystem
- $oWMIHD = Get-WmiObject Win32_Volume -Filter "DriveType='3'"
- $oWMINIC = (@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -notlike "*:*")[0])
- $connectionType = (Get-WmiObject Win32_NetworkAdapter -filter ("NetConnectionStatus = 2") | Where-Object {$_.NetConnectionID -notLike 'VMware*'} | Select -ExpandProperty NetConnectionID)
- $lastBoot = (Get-CimInstance -ClassName Win32_OperatingSystem | Select -Exp LastBootUpTime)
- #===========================================================================
- # Calculate Disk Space
- #===========================================================================
- foreach ($disk in ($oWMIHD | Sort-Object -Property Name)) {
- $disks = @()
- if (!$disk.name.StartsWith("\\")) {
- $disks += [PSCustomObject]@{
- Free = "$([Math]::Round($disk.FreeSpace /1GB,1)) GB"
- }
- }
- }
- #===========================================================================
- # Links WMI Object Values to XAML Form Fields
- #===========================================================================
- $txtUserName.Text = $user
- $txtHostName.Text = $oWMIOS.PSComputerName
- #Displays Domain
- $domain = $oWMICS.Domain
- $txtDomain.Text = $domain
- #Displays IP Address
- $txtWindowsIP.Text = $oWMINIC
- #Displays connection type
- $txtConnectionType.Text = $connectionType
- #Formats and displays OS name
- $aOSName = $oWMIOS.name.Split("|")
- $txtOSName.Text = $aOSName[0]
- #Displays OS version
- $txtOSVersion.Text = $oWMIOS.Version
- #Displays System Drive
- $txtSystemFree.Text = $disks.Free
- #Displays Last Boot Up
- $txtLastBoot.Text = $lastBoot
- Return $user, $oWMIOS.PSComputerName, $domain, $oWMINIC, $connectionType, $aOSName[0], $oWMIOS.Version, $disks.Free, $lastBoot
- }
- #===========================================================================
- # Build Tray Icon UevAppMonitor.exe
- #===========================================================================
- $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\UevAppMonitor.exe")
- # Populate ListView with PS Object data and set width
- $listview.ItemsSource = $disks
- $listview.Width = $grid.width*.9
- # Create GridView object to add to ListView
- $gridview = New-Object System.Windows.Controls.GridView
- # Dynamically add columns to GridView, then bind data to columns
- foreach ($column in $columnorder) {
- $gridcolumn = New-Object System.Windows.Controls.GridViewColumn
- $gridcolumn.Header = $column
- $gridcolumn.Width = $grid.width*.20
- $gridbinding = New-Object System.Windows.Data.Binding $column
- $gridcolumn.DisplayMemberBinding = $gridbinding
- $gridview.AddChild($gridcolumn)
- }
- # Add GridView to ListView
- $listview.View = $gridview
- # Create notifyicon
- $notifyicon = New-Object System.Windows.Forms.NotifyIcon
- $notifyicon.Text = "System Information"
- $notifyicon.Icon = $icon
- $notifyicon.Visible = $true
- $returnData = RefreshData
- $Menu_User = New-Object System.Windows.Forms.MenuItem
- $Menu_User.Text = "Copy User Name"
- $Menu_Computer = New-Object System.Windows.Forms.MenuItem
- $Menu_Computer.Text = "Copy Computer Name"
- $Menu_Domain = New-Object System.Windows.Forms.MenuItem
- $Menu_Domain.Text = "Copy Domain"
- $Menu_Ip = New-Object System.Windows.Forms.MenuItem
- $Menu_Ip.Text = "Copy IP Address"
- $Menu_ConnectionType = New-Object System.Windows.Forms.MenuItem
- $Menu_ConnectionType.Text = "Copy Connection Type"
- $Menu_OperatingSystem = New-Object System.Windows.Forms.MenuItem
- $Menu_OperatingSystem.Text = "Copy Operating System"
- $Menu_OSVersion = New-Object System.Windows.Forms.MenuItem
- $Menu_OSVersion.Text = "Copy OS Version"
- $Menu_FreeDisk = New-Object System.Windows.Forms.MenuItem
- $Menu_FreeDisk.Text = "Copy Free Space"
- $Menu_LastBoot = New-Object System.Windows.Forms.MenuItem
- $Menu_LastBoot.Text = "Copy Last Boot Up Time"
- $Menu_CopyAll = New-Object System.Windows.Forms.MenuItem
- $Menu_CopyAll.Text = "Copy All Information"
- # ---------------------------------------------------------------------
- # Create context menu and add items to the menu
- # ---------------------------------------------------------------------
- $notifyicon.contextMenu = New-Object System.Windows.Forms.ContextMenu
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_User)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_Computer)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_Domain)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_Ip)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_ConnectionType)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_OperatingSystem)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_OSVersion)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_FreeDisk)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_LastBoot)
- $notifyicon.contextMenu.MenuItems.AddRange($Menu_CopyAll)
- # ---------------------------------------------------------------------
- # Actions after clicking menu items
- # ---------------------------------------------------------------------
- $Menu_User.Add_Click({
- ("User Name: " + ($returnData[0])) | Set-Clipboard
- })
- $Menu_Computer.Add_Click({
- ("Computer Name: " + ($returnData[1])) | Set-Clipboard
- })
- $Menu_Domain.Add_Click({
- ("Domain: " + ($returnData[2])) | Set-Clipboard
- })
- $Menu_Ip.Add_Click({
- ("IP Address: " + ($returnData[3])) | Set-Clipboard
- })
- $Menu_ConnectionType.Add_Click({
- ("Connection Type: " + ($returnData[4])) | Set-Clipboard
- })
- $Menu_OperatingSystem.Add_Click({
- ("Operating System: " + ($returnData[5])) | Set-Clipboard
- })
- $Menu_OSVersion.Add_Click({
- ("Operating System Version: " + ($returnData[6])) | Set-Clipboard
- })
- $Menu_FreeDisk.Add_Click({
- ("Free Space: " + ($returnData[7])) | Set-Clipboard
- })
- $Menu_LastBoot.Add_Click({
- ("Last Boot Time: " + ($returnData[8])) | Set-Clipboard
- })
- $sysInfo = @(
- ("User Name: " + ($returnData[0])),
- ("Computer Name: " + ($returnData[1])),
- ("Domain: " + ($returnData[2])),
- ("IP Address: " + ($returnData[3])),
- ("Connection Type: " + ($returnData[4])),
- ("Operating System: " + ($returnData[5])),
- ("Operating System Version: " + ($returnData[6])),
- ("Free Space: " + ($returnData[7])),
- ("Last Boot Time: " + ($returnData[8]))
- )
- $Menu_CopyAll.Add_Click({
- ($sysInfo) | Set-Clipboard
- })
- # Add a left click that makes the Window appear in the lower right part of the screen, above the notify icon.
- $notifyicon.add_Click({
- if ($_.Button -eq [Windows.Forms.MouseButtons]::Left) {
- # reposition each time, in case the resolution or monitor changes
- $window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width)
- $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height)
- $window.Show()
- $window.Activate()
- RefreshData
- }
- })
- # Close the window if it's double clicked
- $window.Add_MouseDoubleClick({
- RefreshData
- })
- #Close the window if it loses focus
- $window.Add_Deactivated({
- $window.Hide()
- })
- # Make PowerShell Disappear
- $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
- $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
- $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
- #Force garbage collection just to start slightly lower RAM usage.
- [System.GC]::Collect()
- # Create an application context for it to all run within.
- # This helps with responsiveness, especially when clicking Exit.
- $appContext = New-Object System.Windows.Forms.ApplicationContext
- [void][System.Windows.Forms.Application]::Run($appContext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement