· 4 years ago · Apr 15, 2021, 01:54 PM
1###############################################################
2#############Add assembly types for the GUI####################
3###############################################################
4Add-Type -AssemblyName PresentationFramework
5Add-Type -AssemblyName System.Windows.Forms
6Add-Type -Name Window -Namespace Console -MemberDefinition '
7[DllImport("Kernel32.dll")]
8public static extern IntPtr GetConsoleWindow();
9
10[DllImport("user32.dll")]
11public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
12'
13
14###############################################################
15############Set global variables###############################
16###############################################################
17
18$ErrorActionPreference = "SilentlyContinue"
19
20$MACs = (Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'").macaddress
21
22$CiscoFormatedMacs = $Macs.ToLower()
23$CiscoFormatedMacs = $CiscoFormatedMacs.Replace(":","")
24$CiscoFormatedMacs = $CiscoFormatedMacs.Substring(0,4) + "." + $CiscoFormatedMacs.Substring(4,4) + "." + $CiscoFormatedMacs.Substring(8,4)
25
26#Get Ipaddress(es)
27$IPAddress = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null }).ipaddress | Where-Object {$_ -Like "10.*" -Or $_ -Like "192.*" -Or $_ -Like "172.*" -Or $_ -Like "169.*" -Or $_ -Like "169.*"}
28
29#Get Default Gateway
30$DefaultGateway = (Get-WmiObject -Class Win32_IP4RouteTable | Where-Object {$_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0'} | Sort-Object metric1 | Select-Object nexthop, metric1, interfaceindex).NextHop
31
32#Get Serial Number and test that it isn't blank
33$SN = (get-ciminstance win32_bios).SerialNumber
34If (!$SN) {$SN = "Unreadable."}
35Else {}
36
37#Get asset tag info if it exists
38$AssetTag = (Get-WmiObject Win32_SystemEnclosure).SMBiosAssetTag
39If (!$AssetTag) {$AssetTag = "Unavailable."}
40Else {}
41
42#Get Comptuer Model
43$ComputerType = (get-wmiobject -class win32_computersystem).Model
44
45###############################################################
46##################Function storage goes here##################
47###############################################################
48Function InfoClick {
49$syncHash.Host = $host
50$Runspace = [runspacefactory]::CreateRunspace()
51$Runspace.ApartmentState = "STA"
52$Runspace.ThreadOptions = "ReuseThread"
53$Runspace.Open()
54$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
55$Runspace.SessionStateProxy.SetVariable("SN",$SN)
56$Runspace.SessionStateProxy.SetVariable("CiscoFormatedMacs", $CiscoFormatedMacs)
57$Runspace.SessionStateProxy.SetVariable("IPAddress", $IPAddress)
58$Runspace.SessionStateProxy.SetVariable("DefaultGateway", $DefaultGateway)
59$Runspace.SessionStateProxy.SetVariable("MACs", $MACs)
60$Runspace.SessionStateProxy.SetVariable("AssetTag", $AssetTag)
61$Runspace.SessionStateProxy.SetVariable("ComputerType", $ComputerType)
62$code = {
63If (!($SyncHash.SecondClick)) {$SyncHash.SecondClick = (Get-Date).AddDays(-1)}
64If (!((Get-Date) -LT (($SyncHash.SecondClick).AddSeconds(1)))) {$SyncHash.SecondClick = Get-Date; Return}
65#Function Goes Here
66
67#Test if image information file exits, and collect it's information if it does.
68$DateFileExists = Test-Path C:\windows\system32\u46oem\date.txt
69
70If ($DateFileExists -eq $True) {$ImageVar = Get-Content C:\windows\system32\u46oem\date.txt}
71Else {$ImageVar = "This computer appears to have been imaged more than several years ago, and does not have a date file."}
72
73#Display information to user.
74[System.Windows.Forms.MessageBox]::Show(
75"1-The computer name is: $ENV:Computername
76
772- The computer serial number is: $SN
78
793- The Asset Tag of the computer is: $AssetTag
80
814- The IP Address of this computer is: $IPAddress
82
835- The MAC Address is: $MACs
84
856- The Default Gateway is: $DefaultGateway
86
877- The model of this computer is: $ComputerType
88
898- $ImageVar
90
919- The Cisco formated Mac Addreess is: $CiscoFormatedMacs
92
93" , "Computer Info" , 0)
94
95}#End of Code Block
96$PSinstance = [powershell]::Create().AddScript($Code)
97$PSinstance.Runspace = $Runspace
98$job = $PSinstance.BeginInvoke()
99
100} #End of InfoClick Function
101
102###############################################################
103##############Create inital session state and import functions#
104###############################################################
105
106#Create a blank Session state to import functions into
107$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
108
109#Create an object that contains a function
110$InfoClickFunction = Get-Content Function:\InfoClick -ErrorAction Stop
111$InfoClickFunctionObject = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'InfoClick', $InfoClickFunction
112
113#Add Function objects into inital session state for the main runspace
114$InitialSessionState.Commands.Add($InfoClickFunctionObject)
115
116###############################################################
117########Generate runspace for the GUI to run in################
118###############################################################
119
120$Runspace = [runspacefactory]::CreateRunspace($InitialSessionState)
121$Runspace.ApartmentState = "STA"
122$Runspace.ThreadOptions = "ReuseThread"
123$Runspace.Open()
124$Runspace.SessionStateProxy.SetVariable("SN",$SN)
125$Runspace.SessionStateProxy.SetVariable("CiscoFormatedMacs", $CiscoFormatedMacs)
126$Runspace.SessionStateProxy.SetVariable("IPAddress", $IPAddress)
127$Runspace.SessionStateProxy.SetVariable("DefaultGateway", $DefaultGateway)
128$Runspace.SessionStateProxy.SetVariable("MACs", $MACs)
129$Runspace.SessionStateProxy.SetVariable("AssetTag", $AssetTag)
130$Runspace.SessionStateProxy.SetVariable("ComputerType", $ComputerType)
131$code = {
132#Find the primary monitor
133$Monitor = [System.Windows.Forms.Screen]::AllScreens | Where-Object {$_.Primary -Like "True"}
134
135###############################################################
136############Determine location of theWindow####################
137###############################################################
138#$Global:Left = $Monitor.bounds.Width - 300
139#$Global:Top = $Monitor.bounds.Height #Curretnly not using this because it's just easier to set the absolute value in the XAML below.
140
141###############################################################
142#############XAML code thatcreate Creates the GUI##############
143###############################################################
144[xml]$xaml = @"
145<Window
146 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
147 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window_Main"
148
149Title="BGInfo" ShowInTaskbar="False" Focusable="False" ResizeMode="NoResize" WindowStyle="None" Left="50" Top="20" Height="50" Width="275" AutomationProperties.HelpText="Provides Information so Information Services can help you." AllowsTransparency="True">
150<Grid Background="#FF070707" AllowDrop="True">
151<TextBlock x:Name="TextBlock_ComputerName" Height="40" Grid.Column="0" Grid.Row="0" VerticalAlignment="Bottom" Margin="0,0,0,8" Foreground="White" HorizontalAlignment="Left" FontSize="14" Width="244">
152<Run Text="Computer Name: LAAA0000AAA0000" />
153<LineBreak />
154<Run Text="IP Address(es): 10.120.120.120. 10.120.120.120." />
155<LineBreak />
156<LineBreak />
157</TextBlock>
158<Button Content="Button" IsTabStop="False" x:Name="Button_1" Height="50" Width="275" Opacity="0" RenderTransformOrigin="0.5417,0.5" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right" VerticalAlignment="Bottom">
159<Button.ContextMenu>
160<ContextMenu>
161 <MenuItem x:Name="MenuItem_CopyComputerName" Header="Copy computer name to clipboard" />
162 <MenuItem x:Name="MenuItem_CopySerialNumber" Header="Copy serial number to clipboard" />
163 <MenuItem x:Name="MenuItem_CopyAssetTag" Header="Copy asset tag to clipboard" />
164 <MenuItem x:Name="MenuItem_CopyIPAddress" Header="Copy IP address to clipboard" />
165 <MenuItem x:Name="MenuItem_CopyMACAddress" Header="Copy MAC address to clipboard" />
166 <MenuItem x:Name="MenuItem_CopyDefaultGateway" Header="Copy default gateway to clipboard" />
167 <MenuItem x:Name="MenuItem_CopyComputerModel" Header="Copy computer model to clipboard" />
168 <Separator />
169 <MenuItem x:Name="MenuItem_CopyEverything" Header="Copy everything to clipboard" />
170</ContextMenu>
171</Button.ContextMenu>
172</Button>
173</Grid>
174</Window>
175"@
176
177###############################################################
178#########Create a hash table to store everyting in#############
179###############################################################
180$syncHash = [hashtable]::Synchronized(@{})
181$reader=(New-Object System.Xml.XmlNodeReader $xaml)
182$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
183
184###############################################################
185#Map all XAML objects to a variable of the same name in the hash table
186###############################################################
187$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object{Set-Variable -Name $_.Name -Value $SyncHash.Window.FindName($_.Name) -PassThru | ForEach-Object {$SyncHash.Add($_.Name,$_.Value)}}
188
189Import-Module Microsoft.PowerShell.Management
190
191###############################################################
192#########Button clicks go here#################################
193###############################################################
194
195$SyncHash.Button_1.Add_Click({InfoClick})
196
197$Synchash.MenuItem_CopyComputerName.Add_Click({Set-Clipboard -Value $ENV:COMPUTERNAME})
198$Synchash.MenuItem_CopySerialNumber.Add_Click({Set-Clipboard -Value $SN})
199$Synchash.MenuItem_CopyAssetTag.Add_Click({Set-Clipboard -Value $AssetTag})
200$Synchash.MenuItem_CopyIPAddress.Add_Click({Set-Clipboard -Value $IPAddress})
201$Synchash.MenuItem_CopyMACAddress.Add_Click({Set-Clipboard -Value $MACs})
202$Synchash.MenuItem_CopyDefaultGateway.Add_Click({Set-Clipboard -Value $DefaultGateway})
203$Synchash.MenuItem_CopyComputerModel.Add_Click({Set-Clipboard -Value $ComputerType})
204$syncHash.MenuItem_CopyEverything.Add_Click({Set-Clipboard -Value "$ENV:COMPUTERNAME `n$SN `n$AssetTag `n$IPAddress `n$MACs `n$DefaultGateway `n$ComputerType"})
205
206
207$syncHash.Host = $host
208$Runspace = [runspacefactory]::CreateRunspace()
209$Runspace.ApartmentState = "STA"
210$Runspace.ThreadOptions = "ReuseThread"
211$Runspace.Open()
212$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
213
214
215$code = {
216Add-Type -AssemblyName PresentationFramework
217Add-Type -AssemblyName System.Windows.Forms
218Add-Type -Name Window -Namespace Console -MemberDefinition '
219[DllImport("Kernel32.dll")]
220public static extern IntPtr GetConsoleWindow();
221
222[DllImport("user32.dll")]
223public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
224'
225
226Do {
227#Find the primary monitor
228$Monitor = ((([System.Windows.Forms.Screen]::AllScreens | Where-Object {$_.Primary -Like "True"}).Bounds).Width)
229
230Write-Output $SynchHash.Monitor | Out-File C:\Test.txt
231$SyncHash.Window.Dispatcher.invoke([action]{$SyncHash.Window.Left = ($Monitor - 300)})
232$SyncHash.Window.Dispatcher.invoke([action]{$SyncHash.Window.Top = (20)})
233
234$IPAddresses = (Get-NetIPAddress | Where-Object {$_.AddressFamily -Eq "IPv4"} | Where-Object {$_.InterFaceAlias -Eq "Wi-Fi" -Or $_.InterFaceAlias -Eq "Ethernet" -Or $_.InterFaceAlias -Like "*vEthernet*"} | Where-Object {$_.AddressState -Eq "Preferred"}).IPAddress
235#Check for VPN Connection
236$VPN = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.ServiceName -Eq "ft_vnic"}
237If ($VPN.IPAddress -Ne $Null) {$IPAddresses = $VPN.IPaddress}
238#Set the contents of the Text Block
239$SyncHash.TextBlock_ComputerName.Dispatcher.invoke([action]{
240$SyncHash.TextBlock_ComputerName.Text = "Computer Name: $Env:COMPUTERNAME
241Active IP Address: $IPAddresses"
242})
243Start-Sleep 10
244} Until (1 -eq 2) #End of Do loop
245}#End of Code Block
246
247$PSinstance = [powershell]::Create().AddScript($Code)
248$PSinstance.Runspace = $Runspace
249$job = $PSinstance.BeginInvoke()
250
251
252$SyncHash.Window.ShowDialog()
253
254
255 }#End of Code Block
256
257 $PSinstance = [powershell]::Create().AddScript($Code)
258 $PSinstance.Runspace = $Runspace
259 $job = $PSinstance.BeginInvoke()