Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===========================================================================
  2. # Assemblies and XAML
  3. #===========================================================================
  4. Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
  5. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  6. [xml]$XAML = @'
  7. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  8.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  9.        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  10.        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  11.        xmlns:local="clr-namespace:WpfApp3"
  12.        Name="window" WindowStyle="None" Width="350" Height="295" Opacity="0.85" AllowsTransparency="True">
  13.        <Window.Resources>
  14.        <Style TargetType="GridViewColumnHeader">
  15.            <Setter Property="Background" Value="Transparent" />
  16.            <Setter Property="Foreground" Value="Transparent"/>
  17.            <Setter Property="BorderBrush" Value="Transparent"/>
  18.            <Setter Property="FontWeight" Value="Bold"/>
  19.            <Setter Property="Opacity" Value="0.5"/>
  20.            <Setter Property="Template">
  21.                <Setter.Value>
  22.                    <ControlTemplate TargetType="GridViewColumnHeader">
  23.                    <Border Background="Transparent">
  24.                    <ContentPresenter></ContentPresenter>
  25.                    </Border>
  26.                    </ControlTemplate>
  27.                </Setter.Value>
  28.            </Setter>
  29.        </Style>
  30.        </Window.Resources>
  31.    <Grid Name="grid" Height="418" HorizontalAlignment="Left" VerticalAlignment="Top">
  32.        <Label Name="Title" Content="System Information" HorizontalAlignment="Left" VerticalAlignment="Top" Width="513" Background="#313130" Foreground="White" FontWeight="Bold" FontSize="14" Height="27"/>
  33.       <Label Content="User Name" HorizontalAlignment="Left" Margin="0,27,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  34.        <Label Content="Computer Name" HorizontalAlignment="Left" Margin="0,57,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  35.        <Label Content="Domain" HorizontalAlignment="Left" Margin="0,87,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  36.        <Label Content="IP Address" HorizontalAlignment="Left" Margin="0,117,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  37.        <Label Content="Connection Type" HorizontalAlignment="Left" Margin="0,147,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  38.        <Label Content="Operating System Name" HorizontalAlignment="Left" Margin="0,177,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  39.        <Label Content="Operating System Version" HorizontalAlignment="Left" Margin="0,207,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#313130" Foreground="White"/>
  40.        <Label Content="Free Space" HorizontalAlignment="Left" Margin="0,237,0,0" Width="170" Background="#313130" Foreground="White" Height="30" VerticalAlignment="Top"/>
  41.        <Label Content="Last Boot Time" HorizontalAlignment="Left" Margin="0,267,0,0" Width="170" Background="#313130" Foreground="White" Height="30" VerticalAlignment="Top"/>
  42.        <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"/>
  43.        <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"/>
  44.        <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"/>
  45.        <TextBox Name="txtWindowsIP" HorizontalAlignment="Left" Height="30" Margin="170,117,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
  46.        <TextBox Name="txtConnectionType" HorizontalAlignment="Left" Height="30" Margin="170,147,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>        
  47.        <TextBox Name="txtOSName" HorizontalAlignment="Left" Height="30" Margin="170,177,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
  48.        <TextBox Name="txtOSVersion" HorizontalAlignment="Left" Height="30" Margin="170,207,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
  49.        <TextBox Name="txtSystemFree" HorizontalAlignment="Left" Height="30" Margin="170,237,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
  50.        <TextBox Name="txtLastBoot" HorizontalAlignment="Left" Height="30" Margin="170,267,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="343" IsEnabled="True" BorderThickness="0.5"/>
  51.            <ListView Name="listview" SelectionMode="Single" Foreground="White" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False">
  52.                <ListView.ItemContainerStyle>
  53.                    <Style>
  54.                        <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
  55.                        <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
  56.                    </Style>
  57.                </ListView.ItemContainerStyle>
  58.            </ListView>
  59.    </Grid>
  60. </Window>
  61. '@
  62.  
  63. #===========================================================================
  64. #Read XAML
  65. #===========================================================================
  66. $reader=(New-Object System.Xml.XmlNodeReader $xaml)  
  67. try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  68. 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}
  69.  
  70. #===========================================================================
  71. # Store Form Objects In PowerShell
  72. #===========================================================================
  73. $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
  74.  
  75. Function RefreshData{
  76. #===========================================================================
  77. # Stores WMI values in WMI Object from System Classes
  78. #===========================================================================
  79. $user = @()
  80. $oWMIOS = @()
  81. $oWMICS = @()
  82. $oWMIHD = @()
  83. $oWMINIC = @()
  84. $connectionType = @()
  85. $lastBoot = @()
  86. $user = ((Get-WmiObject -Class Win32_Process -Filter 'Name="explorer.exe"').GetOwner().User)
  87. $oWMIOS = Get-WmiObject win32_OperatingSystem
  88. $oWMICS = Get-WmiObject -Class Win32_ComputerSystem
  89. $oWMIHD = Get-WmiObject Win32_Volume -Filter "DriveType='3'"
  90. $oWMINIC = (@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -notlike "*:*")[0])
  91. $connectionType = (Get-WmiObject Win32_NetworkAdapter -filter ("NetConnectionStatus = 2") | Where-Object {$_.NetConnectionID -notLike 'VMware*'} | Select -ExpandProperty NetConnectionID)
  92. $lastBoot = (Get-CimInstance -ClassName Win32_OperatingSystem | Select -Exp LastBootUpTime)
  93.  
  94. #===========================================================================
  95. # Calculate Disk Space
  96. #===========================================================================
  97. foreach ($disk in ($oWMIHD | Sort-Object -Property Name)) {
  98. $disks = @()
  99.     if (!$disk.name.StartsWith("\\")) {
  100.               $disks += [PSCustomObject]@{
  101.                      Free = "$([Math]::Round($disk.FreeSpace /1GB,1)) GB"
  102.               }
  103.        }
  104. }
  105.  
  106. #===========================================================================
  107. # Links WMI Object Values to XAML Form Fields
  108. #===========================================================================
  109. $txtUserName.Text = $user
  110.  
  111. $txtHostName.Text = $oWMIOS.PSComputerName
  112.  
  113. #Displays Domain
  114. $domain = $oWMICS.Domain
  115. $txtDomain.Text = $domain
  116.  
  117. #Displays IP Address
  118. $txtWindowsIP.Text = $oWMINIC
  119.  
  120. #Displays connection type
  121. $txtConnectionType.Text = $connectionType
  122.  
  123. #Formats and displays OS name
  124. $aOSName = $oWMIOS.name.Split("|")
  125. $txtOSName.Text = $aOSName[0]
  126.  
  127. #Displays OS version
  128. $txtOSVersion.Text = $oWMIOS.Version
  129.  
  130. #Displays System Drive
  131. $txtSystemFree.Text = $disks.Free
  132.  
  133. #Displays Last Boot Up
  134. $txtLastBoot.Text = $lastBoot
  135.  
  136. Return $user, $oWMIOS.PSComputerName, $domain, $oWMINIC, $connectionType, $aOSName[0], $oWMIOS.Version, $disks.Free, $lastBoot
  137. }
  138.  
  139. #===========================================================================
  140. # Build Tray Icon UevAppMonitor.exe
  141. #===========================================================================
  142. $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\UevAppMonitor.exe")
  143.  
  144. # Populate ListView with PS Object data and set width
  145. $listview.ItemsSource = $disks
  146. $listview.Width = $grid.width*.9
  147.  
  148. # Create GridView object to add to ListView
  149. $gridview = New-Object System.Windows.Controls.GridView
  150.  
  151. # Dynamically add columns to GridView, then bind data to columns
  152. foreach ($column in $columnorder) {
  153.     $gridcolumn = New-Object System.Windows.Controls.GridViewColumn
  154.     $gridcolumn.Header = $column
  155.     $gridcolumn.Width = $grid.width*.20
  156.     $gridbinding = New-Object System.Windows.Data.Binding $column
  157.     $gridcolumn.DisplayMemberBinding = $gridbinding
  158.     $gridview.AddChild($gridcolumn)
  159. }
  160.  
  161. # Add GridView to ListView
  162. $listview.View = $gridview
  163.  
  164. # Create notifyicon
  165. $notifyicon = New-Object System.Windows.Forms.NotifyIcon
  166. $notifyicon.Text = "System Information"
  167. $notifyicon.Icon = $icon
  168. $notifyicon.Visible = $true
  169.  
  170. $returnData = RefreshData
  171.  
  172. $Menu_User = New-Object System.Windows.Forms.MenuItem
  173. $Menu_User.Text = "Copy User Name"
  174.  
  175. $Menu_Computer = New-Object System.Windows.Forms.MenuItem
  176. $Menu_Computer.Text = "Copy Computer Name"
  177.  
  178. $Menu_Domain = New-Object System.Windows.Forms.MenuItem
  179. $Menu_Domain.Text = "Copy Domain"
  180.  
  181. $Menu_Ip = New-Object System.Windows.Forms.MenuItem
  182. $Menu_Ip.Text = "Copy IP Address"
  183.  
  184. $Menu_ConnectionType = New-Object System.Windows.Forms.MenuItem
  185. $Menu_ConnectionType.Text = "Copy Connection Type"
  186.  
  187. $Menu_OperatingSystem = New-Object System.Windows.Forms.MenuItem
  188. $Menu_OperatingSystem.Text = "Copy Operating System"
  189.  
  190. $Menu_OSVersion = New-Object System.Windows.Forms.MenuItem
  191. $Menu_OSVersion.Text = "Copy OS Version"
  192.  
  193. $Menu_FreeDisk = New-Object System.Windows.Forms.MenuItem
  194. $Menu_FreeDisk.Text = "Copy Free Space"
  195.  
  196. $Menu_LastBoot = New-Object System.Windows.Forms.MenuItem
  197. $Menu_LastBoot.Text = "Copy Last Boot Up Time"
  198.  
  199. $Menu_CopyAll = New-Object System.Windows.Forms.MenuItem
  200. $Menu_CopyAll.Text = "Copy All Information"
  201.  
  202. # ---------------------------------------------------------------------
  203. # Create context menu and add items to the menu
  204. # ---------------------------------------------------------------------
  205. $notifyicon.contextMenu = New-Object System.Windows.Forms.ContextMenu
  206. $notifyicon.contextMenu.MenuItems.AddRange($Menu_User)
  207. $notifyicon.contextMenu.MenuItems.AddRange($Menu_Computer)
  208. $notifyicon.contextMenu.MenuItems.AddRange($Menu_Domain)
  209. $notifyicon.contextMenu.MenuItems.AddRange($Menu_Ip)
  210. $notifyicon.contextMenu.MenuItems.AddRange($Menu_ConnectionType)
  211. $notifyicon.contextMenu.MenuItems.AddRange($Menu_OperatingSystem)
  212. $notifyicon.contextMenu.MenuItems.AddRange($Menu_OSVersion)
  213. $notifyicon.contextMenu.MenuItems.AddRange($Menu_FreeDisk)
  214. $notifyicon.contextMenu.MenuItems.AddRange($Menu_LastBoot)
  215. $notifyicon.contextMenu.MenuItems.AddRange($Menu_CopyAll)
  216.  
  217. # ---------------------------------------------------------------------
  218. # Actions after clicking menu items
  219. # ---------------------------------------------------------------------
  220. $Menu_User.Add_Click({
  221.     ("User Name: " + ($returnData[0])) | Set-Clipboard
  222. })
  223.  
  224. $Menu_Computer.Add_Click({
  225.     ("Computer Name: " + ($returnData[1])) | Set-Clipboard
  226. })
  227.  
  228. $Menu_Domain.Add_Click({
  229.     ("Domain: " + ($returnData[2])) | Set-Clipboard
  230. })
  231.  
  232. $Menu_Ip.Add_Click({
  233.     ("IP Address: " + ($returnData[3])) | Set-Clipboard
  234. })
  235.  
  236. $Menu_ConnectionType.Add_Click({
  237.     ("Connection Type: " + ($returnData[4])) | Set-Clipboard
  238. })
  239.  
  240. $Menu_OperatingSystem.Add_Click({
  241.     ("Operating System: " + ($returnData[5])) | Set-Clipboard
  242. })
  243.  
  244. $Menu_OSVersion.Add_Click({
  245.     ("Operating System Version: " + ($returnData[6])) | Set-Clipboard
  246. })
  247.  
  248. $Menu_FreeDisk.Add_Click({
  249.     ("Free Space: " + ($returnData[7])) | Set-Clipboard
  250. })
  251.  
  252. $Menu_LastBoot.Add_Click({
  253.     ("Last Boot Time: " + ($returnData[8])) | Set-Clipboard
  254. })
  255.  
  256. $sysInfo = @(
  257.     ("User Name: " + ($returnData[0])),
  258.     ("Computer Name: " + ($returnData[1])),
  259.     ("Domain: " + ($returnData[2])),
  260.     ("IP Address: " + ($returnData[3])),
  261.     ("Connection Type: " + ($returnData[4])),
  262.     ("Operating System: " + ($returnData[5])),
  263.     ("Operating System Version: " + ($returnData[6])),
  264.     ("Free Space: " + ($returnData[7])),
  265.     ("Last Boot Time: " + ($returnData[8]))
  266.  
  267. )
  268.  
  269. $Menu_CopyAll.Add_Click({
  270.     ($sysInfo) | Set-Clipboard
  271. })
  272.  
  273.  
  274.  
  275. # Add a left click that makes the Window appear in the lower right part of the screen, above the notify icon.
  276. $notifyicon.add_Click({
  277.     if ($_.Button -eq [Windows.Forms.MouseButtons]::Left) {
  278.             # reposition each time, in case the resolution or monitor changes
  279.         $window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width)
  280.             $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height)
  281.             $window.Show()
  282.             $window.Activate()
  283.             RefreshData
  284.     }
  285. })
  286.  
  287. # Close the window if it's double clicked
  288. $window.Add_MouseDoubleClick({
  289.     RefreshData
  290. })
  291.  
  292. #Close the window if it loses focus
  293. $window.Add_Deactivated({
  294.     $window.Hide()
  295. })
  296.  
  297. # Make PowerShell Disappear
  298. $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
  299. $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
  300. $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
  301.  
  302. #Force garbage collection just to start slightly lower RAM usage.
  303. [System.GC]::Collect()
  304.  
  305. # Create an application context for it to all run within.
  306. # This helps with responsiveness, especially when clicking Exit.
  307. $appContext = New-Object System.Windows.Forms.ApplicationContext
  308. [void][System.Windows.Forms.Application]::Run($appContext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement