Created
May 30, 2020 16:02
-
-
Save raspi/d532abce5b124770a7f8a19e36b019f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fetch system specific information | |
# -- Get Operating system name such as "Win 10 Pro" | |
$replaceTable = @{ | |
"Microsoft" = "" | |
"Windows" = "Win" | |
"Professional" = "Pro" | |
"Ultimate" = "Ult" | |
"Enterprise" = "Ent" | |
"Edition" = "Ed" | |
"Service Pack" = "SP" | |
} | |
$operatingsystem = (Get-WmiObject Caption -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption) | |
$replaceTable.GetEnumerator() | ForEach-Object { | |
$operatingsystem = $operatingsystem -replace $_.Key, $_.Value | |
} | |
$operatingsystem = $operatingsystem.Trim() | |
# -- Get serial number | |
$serialnumber = (Get-WmiObject SerialNumber -Class Win32_bios | Select-Object -ExpandProperty SerialNumber) | |
# -- Get processor ID | |
$processorid = (Get-WmiObject ProcessorId -Class Win32_Processor | Select-Object -ExpandProperty ProcessorId) | |
# -- Get logged in user list (not recommended for LLDP, see SNMP) | |
$explorerprocesses = @(Get-WmiObject -Query "SELECT * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue) | |
$logged_in_users_list = @{} | |
if ($explorerprocesses.Count -ne 0) | |
{ | |
foreach ($i in $explorerprocesses) | |
{ | |
$username = $i.GetOwner().User | |
$domain = $i.GetOwner().Domain.ToLower() | |
if(!$logged_in_users_list.ContainsKey($domain)) { | |
$logged_in_users_list.Add($domain, $username) | |
} | |
} | |
} | |
# change to <domain>: user1, user2, .. | |
$logged_in_users_list = $logged_in_users_list | Sort-Object | |
$logged_in_users = "" | |
foreach($dom in $logged_in_users_list.KEYS.GetEnumerator()) { | |
$logged_in_users += $dom + ": " | |
foreach($user in $logged_in_users_list[$dom]) { | |
$logged_in_users += $user + ", " | |
} | |
} | |
$logged_in_users = $logged_in_users.Trim().TrimEnd(',') | |
# -- Get uptime (not recommended for LLDP, see SNMP) | |
$up = (Get-Date) - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject LastBootUpTime -Class Win32_OperatingSystem).LastBootUpTime) | |
# change to XXXdXXhXXmXXs | |
$uptime = "{0:D3}d{1:D2}h{2:D2}m{3:D2}s" -f $up.Days, $up.Hours, $up.Minutes, $up.Seconds | |
# Initialize WinLLDPService.Configuration object | |
$config = New-Object WinLLDPService.Configuration | |
$config.PortDescription.Add("my new port descr..") | |
# Add operating system | |
$config.SystemDescription.Add("OS: " + $operatingsystem) | |
# Add serial number | |
$config.SystemDescription.Add("SN: " + $serialnumber) | |
# Add processor ID | |
$config.SystemDescription.Add("ID: " + $processorid) | |
# Add users logged in | |
$config.SystemDescription.Add("U: " + $logged_in_users_list) | |
# Add uptime | |
$config.SystemDescription.Add("Up: " + $uptime) | |
$config.SystemName.Add("my machine") | |
$config.Separator = "|" | |
# Must always return WinLLDPService.Configuration object | |
Return $config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment