Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################################################
- #############Add assembly types for the GUI####################
- ###############################################################
- Add-Type -AssemblyName PresentationFramework
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- ###############################################################
- ############Set global variables###############################
- ###############################################################
- $ErrorActionPreference = "SilentlyContinue"
- $MACs = (Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'").macaddress
- $CiscoFormatedMacs = $Macs.ToLower()
- $CiscoFormatedMacs = $CiscoFormatedMacs.Replace(":","")
- $CiscoFormatedMacs = $CiscoFormatedMacs.Substring(0,4) + "." + $CiscoFormatedMacs.Substring(4,4) + "." + $CiscoFormatedMacs.Substring(8,4)
- #Get Ipaddress(es)
- $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.*"}
- #Get Default Gateway
- $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
- #Get Serial Number and test that it isn't blank
- $SN = (get-ciminstance win32_bios).SerialNumber
- If (!$SN) {$SN = "Unreadable."}
- Else {}
- #Get asset tag info if it exists
- $AssetTag = (Get-WmiObject Win32_SystemEnclosure).SMBiosAssetTag
- If (!$AssetTag) {$AssetTag = "Unavailable."}
- Else {}
- #Get Comptuer Model
- $ComputerType = (get-wmiobject -class win32_computersystem).Model
- ###############################################################
- ##################Function storage goes here##################
- ###############################################################
- Function InfoClick {
- $syncHash.Host = $host
- $Runspace = [runspacefactory]::CreateRunspace()
- $Runspace.ApartmentState = "STA"
- $Runspace.ThreadOptions = "ReuseThread"
- $Runspace.Open()
- $Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
- $Runspace.SessionStateProxy.SetVariable("SN",$SN)
- $Runspace.SessionStateProxy.SetVariable("CiscoFormatedMacs", $CiscoFormatedMacs)
- $Runspace.SessionStateProxy.SetVariable("IPAddress", $IPAddress)
- $Runspace.SessionStateProxy.SetVariable("DefaultGateway", $DefaultGateway)
- $Runspace.SessionStateProxy.SetVariable("MACs", $MACs)
- $Runspace.SessionStateProxy.SetVariable("AssetTag", $AssetTag)
- $Runspace.SessionStateProxy.SetVariable("ComputerType", $ComputerType)
- $code = {
- If (!($SyncHash.SecondClick)) {$SyncHash.SecondClick = (Get-Date).AddDays(-1)}
- If (!((Get-Date) -LT (($SyncHash.SecondClick).AddSeconds(1)))) {$SyncHash.SecondClick = Get-Date; Return}
- #Function Goes Here
- #Test if image information file exits, and collect it's information if it does.
- $DateFileExists = Test-Path C:\windows\system32\u46oem\date.txt
- If ($DateFileExists -eq $True) {$ImageVar = Get-Content C:\windows\system32\u46oem\date.txt}
- Else {$ImageVar = "This computer appears to have been imaged more than several years ago, and does not have a date file."}
- #Display information to user.
- [System.Windows.Forms.MessageBox]::Show(
- "1-The computer name is: $ENV:Computername
- 2- The computer serial number is: $SN
- 3- The Asset Tag of the computer is: $AssetTag
- 4- The IP Address of this computer is: $IPAddress
- 5- The MAC Address is: $MACs
- 6- The Default Gateway is: $DefaultGateway
- 7- The model of this computer is: $ComputerType
- 8- $ImageVar
- 9- The Cisco formated Mac Addreess is: $CiscoFormatedMacs
- " , "Computer Info" , 0)
- }#End of Code Block
- $PSinstance = [powershell]::Create().AddScript($Code)
- $PSinstance.Runspace = $Runspace
- $job = $PSinstance.BeginInvoke()
- } #End of InfoClick Function
- ###############################################################
- ##############Create inital session state and import functions#
- ###############################################################
- #Create a blank Session state to import functions into
- $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
- #Create an object that contains a function
- $InfoClickFunction = Get-Content Function:\InfoClick -ErrorAction Stop
- $InfoClickFunctionObject = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'InfoClick', $InfoClickFunction
- #Add Function objects into inital session state for the main runspace
- $InitialSessionState.Commands.Add($InfoClickFunctionObject)
- ###############################################################
- ########Generate runspace for the GUI to run in################
- ###############################################################
- $Runspace = [runspacefactory]::CreateRunspace($InitialSessionState)
- $Runspace.ApartmentState = "STA"
- $Runspace.ThreadOptions = "ReuseThread"
- $Runspace.Open()
- $Runspace.SessionStateProxy.SetVariable("SN",$SN)
- $Runspace.SessionStateProxy.SetVariable("CiscoFormatedMacs", $CiscoFormatedMacs)
- $Runspace.SessionStateProxy.SetVariable("IPAddress", $IPAddress)
- $Runspace.SessionStateProxy.SetVariable("DefaultGateway", $DefaultGateway)
- $Runspace.SessionStateProxy.SetVariable("MACs", $MACs)
- $Runspace.SessionStateProxy.SetVariable("AssetTag", $AssetTag)
- $Runspace.SessionStateProxy.SetVariable("ComputerType", $ComputerType)
- $code = {
- #Find the primary monitor
- $Monitor = [System.Windows.Forms.Screen]::AllScreens | Where-Object {$_.Primary -Like "True"}
- ###############################################################
- ############Determine location of theWindow####################
- ###############################################################
- #$Global:Left = $Monitor.bounds.Width - 300
- #$Global:Top = $Monitor.bounds.Height #Curretnly not using this because it's just easier to set the absolute value in the XAML below.
- ###############################################################
- #############XAML code thatcreate Creates the GUI##############
- ###############################################################
- [xml]$xaml = @"
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Window_Main"
- Title="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">
- <Grid Background="#FF070707" AllowDrop="True">
- <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">
- <Run Text="Computer Name: LAAA0000AAA0000" />
- <LineBreak />
- <Run Text="IP Address(es): 10.120.120.120. 10.120.120.120." />
- <LineBreak />
- <LineBreak />
- </TextBlock>
- <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">
- <Button.ContextMenu>
- <ContextMenu>
- <MenuItem x:Name="MenuItem_CopyComputerName" Header="Copy computer name to clipboard" />
- <MenuItem x:Name="MenuItem_CopySerialNumber" Header="Copy serial number to clipboard" />
- <MenuItem x:Name="MenuItem_CopyAssetTag" Header="Copy asset tag to clipboard" />
- <MenuItem x:Name="MenuItem_CopyIPAddress" Header="Copy IP address to clipboard" />
- <MenuItem x:Name="MenuItem_CopyMACAddress" Header="Copy MAC address to clipboard" />
- <MenuItem x:Name="MenuItem_CopyDefaultGateway" Header="Copy default gateway to clipboard" />
- <MenuItem x:Name="MenuItem_CopyComputerModel" Header="Copy computer model to clipboard" />
- <Separator />
- <MenuItem x:Name="MenuItem_CopyEverything" Header="Copy everything to clipboard" />
- </ContextMenu>
- </Button.ContextMenu>
- </Button>
- </Grid>
- </Window>
- "@
- ###############################################################
- #########Create a hash table to store everyting in#############
- ###############################################################
- $syncHash = [hashtable]::Synchronized(@{})
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- $syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
- ###############################################################
- #Map all XAML objects to a variable of the same name in the hash table
- ###############################################################
- $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)}}
- Import-Module Microsoft.PowerShell.Management
- ###############################################################
- #########Button clicks go here#################################
- ###############################################################
- $SyncHash.Button_1.Add_Click({InfoClick})
- $Synchash.MenuItem_CopyComputerName.Add_Click({Set-Clipboard -Value $ENV:COMPUTERNAME})
- $Synchash.MenuItem_CopySerialNumber.Add_Click({Set-Clipboard -Value $SN})
- $Synchash.MenuItem_CopyAssetTag.Add_Click({Set-Clipboard -Value $AssetTag})
- $Synchash.MenuItem_CopyIPAddress.Add_Click({Set-Clipboard -Value $IPAddress})
- $Synchash.MenuItem_CopyMACAddress.Add_Click({Set-Clipboard -Value $MACs})
- $Synchash.MenuItem_CopyDefaultGateway.Add_Click({Set-Clipboard -Value $DefaultGateway})
- $Synchash.MenuItem_CopyComputerModel.Add_Click({Set-Clipboard -Value $ComputerType})
- $syncHash.MenuItem_CopyEverything.Add_Click({Set-Clipboard -Value "$ENV:COMPUTERNAME `n$SN `n$AssetTag `n$IPAddress `n$MACs `n$DefaultGateway `n$ComputerType"})
- $syncHash.Host = $host
- $Runspace = [runspacefactory]::CreateRunspace()
- $Runspace.ApartmentState = "STA"
- $Runspace.ThreadOptions = "ReuseThread"
- $Runspace.Open()
- $Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
- $code = {
- Add-Type -AssemblyName PresentationFramework
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -Name Window -Namespace Console -MemberDefinition '
- [DllImport("Kernel32.dll")]
- public static extern IntPtr GetConsoleWindow();
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
- '
- Do {
- #Find the primary monitor
- $Monitor = ((([System.Windows.Forms.Screen]::AllScreens | Where-Object {$_.Primary -Like "True"}).Bounds).Width)
- Write-Output $SynchHash.Monitor | Out-File C:\Test.txt
- $SyncHash.Window.Dispatcher.invoke([action]{$SyncHash.Window.Left = ($Monitor - 300)})
- $SyncHash.Window.Dispatcher.invoke([action]{$SyncHash.Window.Top = (20)})
- $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
- #Check for VPN Connection
- $VPN = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.ServiceName -Eq "ft_vnic"}
- If ($VPN.IPAddress -Ne $Null) {$IPAddresses = $VPN.IPaddress}
- #Set the contents of the Text Block
- $SyncHash.TextBlock_ComputerName.Dispatcher.invoke([action]{
- $SyncHash.TextBlock_ComputerName.Text = "Computer Name: $Env:COMPUTERNAME
- Active IP Address: $IPAddresses"
- })
- Start-Sleep 10
- } Until (1 -eq 2) #End of Do loop
- }#End of Code Block
- $PSinstance = [powershell]::Create().AddScript($Code)
- $PSinstance.Runspace = $Runspace
- $job = $PSinstance.BeginInvoke()
- $SyncHash.Window.ShowDialog()
- }#End of Code Block
- $PSinstance = [powershell]::Create().AddScript($Code)
- $PSinstance.Runspace = $Runspace
- $job = $PSinstance.BeginInvoke()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement