‎04-27-2022 11:50 PM
‎04-28-2022 01:29 AM
# Variables you must set
$DHCPServer = "TYPEYOURDHCPSERVERNAMEHERE"
$LANSW = "TYPEYOURLANSWEEPERSQLSERVERINFOHERE"
# Get every lease in DHCP (Takes about 5 minutes to run for me, everything else is pretty quick)
$DHCPLeases = @()
$DHCPScopes = Get-DhcpServerv4Scope -ComputerName $DHCPSERVER
foreach ($Scope in $DHCPScopes.ScopeId){
$DHCPLeases += Get-DhcpServerv4Lease -ComputerName $DHCPSERVER -AllLeases -ScopeId $Scope
}
# Gets the Ethernet MAC address of every ChromeOS Device pulled from the admin console by the nightly midnight scan
$query = "select chrome.EthernetMacAddress from tblassets ass left join tblChromeOs chrome on ass.assetid = chrome.assetid where assettype = 84"
$lanswmacs = invoke-sqlcmd -serverinstance $LANSW -database lansweeperdb -ConnectionTimeout 30 -QueryTimeout 300 -query $query
# Convert MACS to match DHCP
$MACS = $lanswmacs.EthernetMacAddress -replace ':', '-'
# Get IP Address for each ChromeOS MAC found in DHCP
$MACStoImport = @()
foreach ($MAC in $MACS){
if ($MAC -in $DHCPLeases.ClientID){
$obj = new-object psobject
$obj | add-member noteproperty MAC ($MAC -replace '-', ':')
$obj | add-member noteproperty IP ($DHCPLeases | ? {$_.clientid -match $MAC}).IPAddress.IPAddressToString
$MACStoImport += $obj
}
}
# Import the Data into Lansweeper
foreach ($item in $MACStoImport){
# Update the IPAddress field
$query = "
UPDATE tblassets
SET IPAddress = '$($item.IP)'
FROM tblassets
LEFT JOIN tblChromeOs on tblassets.assetid = tblChromeOs.assetid
WHERE tblChromeOs.EthernetMacAddress like '$($item.Mac)'
"
invoke-sqlcmd -serverinstance $LANSW -database lansweeperdb -ConnectionTimeout 30 -QueryTimeout 300 -query $query
# Update the IPNumeric field
[long]$IPNum = ($item.IP.Split('.') | ForEach-Object {$_.PadLeft(3,'0')}) -join ''
$query = "
UPDATE tblassets
SET IPNumeric = $IPNum
FROM tblassets
LEFT JOIN tblChromeOs on tblassets.assetid = tblChromeOs.assetid
WHERE tblChromeOs.EthernetMacAddress like '$($item.Mac)'
"
invoke-sqlcmd -serverinstance $LANSW -database lansweeperdb -ConnectionTimeout 30 -QueryTimeout 300 -query $query
# Update the LocationID field
$ipsearch = $item.IP.Substring(0, $item.IP.lastIndexOf('.')) + '.%'
$query = "select LocationID from tsysIPLocations where realstart like '$ipsearch'"
$LocID = (invoke-sqlcmd -serverinstance $LANSW -database lansweeperdb -ConnectionTimeout 30 -QueryTimeout 300 -query $query).LocationID
$query = "
UPDATE tblassets
SET LocationID = $LocID
FROM tblassets
LEFT JOIN tblChromeOs on tblassets.assetid = tblChromeOs.assetid
WHERE tblChromeOs.EthernetMacAddress like '$($item.Mac)'
"
invoke-sqlcmd -serverinstance $LANSW -database lansweeperdb -ConnectionTimeout 30 -QueryTimeout 300 -query $query
}
Experience Lansweeper with your own data. Sign up now for a 14-day free trial.
Try Now