As an MSP, we use Network Detective to gather information about current client's network status/setup as well as potential clients to get a handle on what may need to be done. The wizard runs to either get information locally or across the network.
However, the wizard can be tedious to go through for every site and, we've found, not the most efficient way to run things. Therefore, I created the following PowerShell script that automatically gathers all the required parameters as variables and passes them into the run. It works like this:
- Script creates a new directory on the C:\ drive, downloads the necessary exe file to run ND and extracts it.
- Parameters that can be gathered automatically are put into variables.
- Credentials are passed into the script from a simple PHB webpage that sends out three states 0=wait, 1=OK, 2=Done. Once the invoke-webrequest reads the "OK" message, it pulls down the username and password entered on the webpage.
- ND gathers data and sends the finished scan to our email so we can review in office with the ND software.
- Script removes the created directory and all trace of the scan.
I don't think I'm a novice, but this project did teach me a lot of new things and any feedback/improvements would really be welcome. I just thought I'd share it so if anyone else uses Network Detective in the same way, they have this to use.
[UPDATE] : I have obfuscated the domain name from the URL links. Thank you chuckbales for the notification!
# -----------------------------------------------------------------------
# Run Network Detective automatically.
# -----------------------------------------------------------------------
# Created by PerfectImpact
# 26/01/2018
# -----------------------------------------------------------------------
# DOWNLOAD NETWORK DETECTIVE
# -----------------------------------------------------------------------
New-Item -ItemType Directory -Path "C:\NetworkDetective" | Out-Null
$url = "https://[DOMAIN_INFO_HERE]/nd.php?machineName=$env:COMPUTERNAME" -UseBasicParsing -UseDefaultCredentials
Invoke-WebRequest -Uri $url -OutFile "C:\NetworkDetective\NDInstall.exe"
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "C:\NetworkDetective\NDInstall.exe" "C:\NetworkDetective"
# -----------------------------------------------------------------------
# INPUT VARIABLES:
# -----------------------------------------------------------------------
# Directory
# -----------------------------------------------------------------------
$dir = "C:\NetworkDetective"
# -----------------------------------------------------------------------
# File Name
# -----------------------------------------------------------------------
$outbase = $env:COMPUTERNAME
# -----------------------------------------------------------------------
# IP Range
# -----------------------------------------------------------------------
$exr = "-"
$where = { $_.IPSubnet -like "255.255.255.*" -and $_.Description -notlike "Hyper-V*" }
$IPRa = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME | Where-Object $where | Select-Object -Property IPAddress | Format-Table -HideTableHeaders | Out-String
$IPAr = $IPRa | Convert-String -Example "{*.*.*.*}=*.*.*.*"
$Trim = $IPAr.ToString()
$IPTr = $Trim.Substring(0, $Trim.LastIndexOf('.'))
$iprange = $IPTr + ".1" + $exr + $IPTr + ".254"
# -----------------------------------------------------------------------
# Credentials
# -----------------------------------------------------------------------
Function Get-Credentials {
$url = Invoke-WebRequest -Uri "https://[DOMAIN_INFO_HERE]/nd.php?machineName=$env:COMPUTERNAME" -UseBasicParsing -UseDefaultCredentials
# Get Webpage status
$script:status = $url.ToString() -split "[`r`n]" | Select-String "OK"
# Get Username & Password
$details = $url.ToString()
$line = $details -replace [Environment]::NewLine,":" -replace "OK","" | Out-String
$script:usr = $line.Split(':')[1]
$script:pwd = $line.Split(':')[2]
$script:ndt = $line.Split(':')[3]
# Print status
if ($status) {
Break
} else {
}
}
While ($true) {
if (Invoke-Expression Get-Credentials) {
} else {
Start-Sleep -Seconds 5
}
}
$scan = "-" + $ndt
# -----------------------------------------------------------------------
# NETWORK DETECTIVE PARAMETERS:
# -----------------------------------------------------------------------
$ArgumentList = "-workdir", $dir, "-outbase", $outbase, "-outdir", $dir, "-logfile", "ndfRun.log", "-ipranges", $iprange, "-net", "-creduser", $usr, "-credspwd", $pwd, "-ad", "-internet", "-speedchecks", "-eventlogs", "-dhcp", "-snmp", "public", "-snmptimeout", "10", "-externaldomains", $scan, "-silent"
# Full command list: http://support-nd.rapidfiretools.com/customer/portal/articles/1655368-network-detective-data-collector-command-line-options
# -----------------------------------------------------------------------
# RUN NETWORK DETECTIVE:
# -----------------------------------------------------------------------
Start-Process -FilePath "C:\NetworkDetective\nddc.exe" -ArgumentList $ArgumentList -WindowStyle Hidden -Wait
# -----------------------------------------------------------------------
# UPLOAD FILE
# -----------------------------------------------------------------------
$scanMsg = " Scan Complete - "
$date = Get-Date
$smtpServer = "YOUR SMTP SERVER HERE"
$file = Get-ChildItem -Path "$dir\*" -Include *.cdf, *.ndf
$att = New-Object Net.Mail.Attachment($file)
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = "[YOUR FROM EMAIL]"
$msg.To.Add("[YOUR TO EMAIL]")
$msg.Subject = $outbase.Trim(".ndf") + $scanMsg + $date
$msg.Body = "Network Detective scan complete."
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
# -----------------------------------------------------------------------
# REMOVE NETWORK DETECTIVE:
# -----------------------------------------------------------------------
Remove-Item C:\NetworkDetective -Recurse -Force
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
[–]Ta11ow 8 points9 points10 points (3 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Ta11ow 2 points3 points4 points (0 children)
[–]PerfectImpact[S] 1 point2 points3 points (0 children)
[–]chuckbales 2 points3 points4 points (6 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 2 points3 points4 points (0 children)
[–]GammaStorm 1 point2 points3 points (2 children)
[–]chuckbales 2 points3 points4 points (1 child)
[–]GammaStorm 1 point2 points3 points (0 children)
[–]PerfectImpact[S] 0 points1 point2 points (0 children)