Skip to content

Instantly share code, notes, and snippets.

@MarcoGriep88
Created January 27, 2022 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoGriep88/33fd6c9e82c0548c11fa1e51637f9dd4 to your computer and use it in GitHub Desktop.
Save MarcoGriep88/33fd6c9e82c0548c11fa1e51637f9dd4 to your computer and use it in GitHub Desktop.
Active Directory, WSUS, Lansweeper Computer Object Sync
#Get-WindowsCapability -Online | ? {$_.Name -like "*RSAT*" -and $_.State -eq "NotPresent"} | Add-WindowsCapability -Online
# Oder:
#DISM /Online /Add-Capability /CapabilityName:Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
#Install-Module ImportExcel
class HostInfo {
[string]$Hostname
[string]$LastSync
}
## Get AD Computers
Import-Module ActiveDirectory
$computers = Get-ADComputer -Filter * -Properties * | select DNSHostName, Created, LastLogonDate, OperatingSystem, IPv4Address, Description
## Get Lansweeper Computers
$response = Invoke-WebRequest -Uri "http://localhost:85/api/Values" # Enter URL for API Lansweeper here
$data = ConvertFrom-Json $([String]::new($response.Content))
$lansweeper_machines = @()
foreach($d in $data) {
$obj = @([HostInfo]@{Hostname=$d.AssetName})
$found = $False
foreach($entry in $lansweeper_machines) {
if ($entry.Hostname -eq $obj.Hostname) {
$found = $True
break;
}
}
if ($found -ne $True) { $lansweeper_machines += $obj }
}
## Get WSUS Computers
$srv = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer("MyWSUS.Domain.Int",$True,8531) # WSUS server. Check SSL and port!
$wsusdata = (Get-WsusComputer -UpdateServer $srv -IncludeDownstreamComputerTargets)
$wsuscomputers = @()
foreach($d in $wsusdata) {
$comp = $d.FullDomainName.Split('.')[0]
$obj = @([HostInfo]@{Hostname=$comp;LastSync=$d.LastReportedStatusTime})
$found = $False
foreach($entry in $wsuscomputers) {
if ($entry.Hostname -eq $obj.Hostname) {
$found = $True
break;
}
}
if ($found -ne $True) { $wsuscomputers += $obj }
}
class OutputData {
[string]$Hostname = ""
[string]$Description = ""
[string]$OS = ""
[string]$LastLogon = ""
[boolean]$isInLansweeper = $False
[boolean]$isInWSUS = $False
[string]$LastWsusSync = ""
}
$reportData = @()
foreach($comp in $computers) {
$isInLansweeper = $False
$isInWSUS = $False
$splittedName = $comp.DNSHostName.Split('.')[0].ToLower()
foreach($lscomp in $lansweeper_machines) {
if ($splittedName -like $lscomp.Hostname.ToLower()) {
$isInLansweeper= $True
break;
}
}
foreach($wsuscomp in $wsuscomputers) {
if ($splittedName -like $wsuscomp.Hostname.ToLower()) {
$isInWSUS = $True
$LastWsusSync = $wsuscomp.LastSync
break;
}
}
$repobj = @([OutputData]@{Hostname=$splittedName;Description=$comp.Description;OS=$comp.OperatingSystem;LastLogon=$comp.LastLogonDate;isInLansweeper=$isInLansweeper;isInWSUS=$isInWSUS;LastWsusSync=$LastWsusSync})
$reportData += $repobj
}
$reportData | Out-GridView
#$reportData | Export-Excel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment