Advertisement
Guest User

Untitled

a guest
Feb 1st, 2021
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. #===========================================================================
  2. Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
  3. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  4. [xml]$XAML = @'
  5. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. xmlns:local="clr-namespace:WpfApp3"
  10. Name="window" WindowStyle="None" Height="375" Width="420" Opacity="0.90" AllowsTransparency="False">
  11. <Window.Resources>
  12. <Style TargetType="GridViewColumnHeader">
  13. <Setter Property="Background" Value="Transparent" />
  14. <Setter Property="Foreground" Value="Transparent"/>
  15. <Setter Property="BorderBrush" Value="Transparent"/>
  16. <Setter Property="FontWeight" Value="Bold"/>
  17. <Setter Property="Opacity" Value="0.80"/>
  18. <Setter Property="Template">
  19. <Setter.Value>
  20. <ControlTemplate TargetType="GridViewColumnHeader">
  21. <Border Background="Transparent">
  22. <ContentPresenter></ContentPresenter>
  23. </Border>
  24. </ControlTemplate>
  25. </Setter.Value>
  26. </Setter>
  27. </Style>
  28. </Window.Resources>
  29. <Grid Name="grid" Height="197" HorizontalAlignment="Left" VerticalAlignment="Top">
  30. <Label Name="Title" Content="Pc Info" HorizontalAlignment="Left" VerticalAlignment="Top" Width="420" Height="40" Background="#313130" Foreground="#6996e3" FontWeight="Bold" FontSize="17"/>
  31.  
  32. <Label Content="Hostname" HorizontalAlignment="Left" Margin="0,37,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  33. <TextBox Name="txtHostName" Height="20" Margin="130,43,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" IsEnabled="True" AllowDrop="True" BorderThickness="0" HorizontalAlignment="Left" Width="290" FontSize="14"/>
  34. <Label Content="IP Address" HorizontalAlignment="Left" Margin="0,64.5,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  35. <TextBox Name="txtWindowsIP" HorizontalAlignment="Left" Height="20" Margin="130,70,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290" IsEnabled="True" BorderThickness="0" FontSize="14"/>
  36. <Label Content="Operating System" HorizontalAlignment="Left" Margin="0,92,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  37. <TextBox Name="txtOSName" HorizontalAlignment="Left" Height="20" Margin="130,97.5,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290" IsEnabled="True" BorderThickness="0" FontSize="14"/>
  38. <Label Content="Last Boot" HorizontalAlignment="Left" Margin="0,119.5,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  39. <TextBox Name="txtLastboot" HorizontalAlignment="Left" Height="20" Margin="130,125,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290" IsEnabled="True" BorderThickness="0" FontSize="14"/>
  40. <Label Content="Current User" HorizontalAlignment="Left" Margin="0,146,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  41. <TextBox Name="txtcurrentuser" HorizontalAlignment="Left" Height="20" Margin="130,151.5,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290" IsEnabled="True" BorderThickness="0" FontSize="14"/>
  42. <Label Content="Hardware" HorizontalAlignment="Left" Margin="0,172.5,0,0" VerticalAlignment="Top" Width="125" Height="30" Background="#313130" Foreground="White" FontSize="14"/>
  43. <TextBox Name="txthardware" HorizontalAlignment="Left" Height="20" Margin="130,179,5,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="290" IsEnabled="True" BorderThickness="0" FontSize="14"/>
  44.  
  45.  
  46.  
  47. <ListView Name="listview" SelectionMode="Single" Foreground="White" Background="Transparent" BorderBrush="Transparent" IsHitTestVisible="False">
  48. <ListView.ItemContainerStyle>
  49. <Style>
  50. <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
  51. <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
  52. </Style>
  53. </ListView.ItemContainerStyle>
  54. </ListView>
  55. </Grid>
  56. </Window>
  57. '@
  58.  
  59. #Read XAML
  60. $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  61. try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
  62. 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}
  63.  
  64. #===========================================================================
  65. # Store Form Objects In PowerShell
  66. #===========================================================================
  67. $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
  68.  
  69. Function RefreshData{
  70. #===========================================================================
  71. # Stores values in Object from System Classes
  72. #===========================================================================
  73. $oCIMOS = @()
  74. $oCIMNIC = @()
  75. $oCIMOS = Get-CimInstance win32_OperatingSystem
  76. $Win10Ver = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
  77. $oCIMNIC = Get-CimInstance Win32_NetworkAdapterConfiguration | Where { $_.IPAddress } | Select -Expand IPAddress | Where { $_ -like '1*' }
  78. $lastboot = Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty LastBootupTime
  79. $currentuser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
  80. $hardware = (Get-WmiObject -Class:Win32_ComputerSystem).Model
  81.  
  82.  
  83. #===========================================================================
  84. # Links Object Values to XAML Form Fields
  85. #===========================================================================
  86. $txtHostName.Text = Hostname
  87.  
  88. #Formats and displays OS name
  89. $aOSName = $oCIMOS.name.Split("|")
  90. $txtOSName.Text = ($aOSName[0] + " " + "(" + $Win10Ver + ")")
  91.  
  92. #Displays IP Address
  93. $txtWindowsIP.Text = $oCIMNIC
  94.  
  95. #Displays Last Boot time
  96. $txtlastboot.Text = $lastboot
  97.  
  98. #Displays Current User
  99. $txtcurrentuser.Text = $currentuser
  100.  
  101.  
  102. #Displays Current User
  103. $txthardware.Text = $hardware
  104.  
  105.  
  106.  
  107. }
  108.  
  109. #===========================================================================
  110. # Build Tray Icon
  111. #===========================================================================
  112. $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\HelpPane.exe")
  113.  
  114. # Populate ListView with PS Object data and set width
  115. $listview.ItemsSource = $disks
  116. $listview.Width = $grid.width*.9
  117.  
  118. # Create GridView object to add to ListView
  119. $gridview = New-Object System.Windows.Controls.GridView
  120.  
  121. # Dynamically add columns to GridView, then bind data to columns
  122. foreach ($column in $columnorder) {
  123. $gridcolumn = New-Object System.Windows.Controls.GridViewColumn
  124. $gridcolumn.Header = $column
  125. $gridcolumn.Width = $grid.width*.20
  126. $gridbinding = New-Object System.Windows.Data.Binding $column
  127. $gridcolumn.DisplayMemberBinding = $gridbinding
  128. $gridview.AddChild($gridcolumn)
  129. }
  130.  
  131. # Add GridView to ListView
  132. $listview.View = $gridview
  133.  
  134. # Create notifyicon, and right-click -> Exit menu
  135. $notifyicon = New-Object System.Windows.Forms.NotifyIcon
  136. $notifyicon.Text = "System Resources"
  137. $notifyicon.Icon = $icon
  138. $notifyicon.Visible = $true
  139.  
  140. $menuitem = New-Object System.Windows.Forms.MenuItem
  141. $menuitem.Text = "Exit"
  142.  
  143. $contextmenu = New-Object System.Windows.Forms.ContextMenu
  144. $notifyicon.ContextMenu = $contextmenu
  145. $notifyicon.contextMenu.MenuItems.AddRange($menuitem)
  146.  
  147. # Add a left click that makes the Window appear in the lower right part of the screen, above the notify icon.
  148. $notifyicon.add_Click({
  149. if ($_.Button -eq [Windows.Forms.MouseButtons]::Left) {
  150. # reposition each time, in case the resolution or monitor changes
  151. $window.Left = $([System.Windows.SystemParameters]::WorkArea.Width-$window.Width)
  152. $window.Top = $([System.Windows.SystemParameters]::WorkArea.Height-$window.Height)
  153. $window.Show()
  154. $window.Activate()
  155. RefreshData
  156. }
  157. })
  158.  
  159. # Close the window if it's double clicked
  160. $window.Add_MouseDoubleClick({
  161. RefreshData
  162. })
  163.  
  164. #Close the window if it loses focus
  165. $window.Add_Deactivated({
  166. $window.Hide()
  167. })
  168.  
  169. # When Exit is clicked, close everything and kill the PowerShell process
  170. $menuitem.add_Click({
  171. $notifyicon.Visible = $false
  172. $window.close()
  173. Stop-Process $pid
  174. })
  175.  
  176. # Make PowerShell Disappear
  177. $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
  178. $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
  179. $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
  180.  
  181. Force garbage collection just to start slightly lower RAM usage.
  182. [System.GC]::Collect()
  183.  
  184. Create an application context for it to all run within.
  185. This helps with responsiveness, especially when clicking Exit.
  186. $appContext = New-Object System.Windows.Forms.ApplicationContext
  187. [void][System.Windows.Forms.Application]::Run($appContext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement