160 lines
6.0 KiB
PowerShell
160 lines
6.0 KiB
PowerShell
##########
|
|
#
|
|
# Welcome to the Discovery script, a component of Deployotron! Deployotron is an open-source tool for managing windows computers on your local network.
|
|
# The Discovery Script, in particular, gathers information about the devices on your local network and stores that information for use by other components of Deployotron! :)
|
|
#
|
|
###
|
|
# Definte output file locations
|
|
$currentDate = Get-Date
|
|
$timeDate = $currentDate.ToString("MM_dd_HHmm")
|
|
#####
|
|
# Uncomment the line below to enable options that save data to the directory the script runs from.
|
|
#$runDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
|
|
# This will be the name of the folder the data is saved to
|
|
$dataDirN = "Discovery-Data"
|
|
####
|
|
# Replace "D:" with the destination or root folder within which resides the data directory.
|
|
$dataDir = Join-Path -Path "D:" -ChildPath $dataDirN
|
|
####
|
|
# Uncomment the line below to save data generated by the discover script in the directory the script is run from, instead of the drive selected above.
|
|
# $dataDir = Join-Path -Path $runDir -ChildPath $dataDirN
|
|
|
|
# This is the name of the folder the logs will be saved in
|
|
$logDirN = "logs"
|
|
$logDir = Join-Path -Path $dataDir -ChildPath $logDirN
|
|
####
|
|
# Uncomment the below line to save logs in the directory the script is run from instead of the data directory.
|
|
#$logDir = Join-Path -Path $runDir -ChildPath $logDirN
|
|
|
|
$transcript = "$timeDate.discover.log"
|
|
$transcriptPath = Join-Path -Path $logDir -ChildPath $transcript
|
|
|
|
# Start logging
|
|
Start-Transcript -Path $transcriptPath
|
|
|
|
# Define the CSV file path to save the results
|
|
$windowsFile = "WindowsMachines.csv"
|
|
#$windowsFilePath = Join-Path -Path $dataDir -ChildPath $windowsFile
|
|
#$neighborFile = "NeighborMachines.csv"
|
|
#$neighborFilePath = Join-Path -Path $dataDir -ChildPath $neighborFile
|
|
|
|
# Define the range for the 1st octet (adjust as needed)
|
|
$startRange1stOctet = 192
|
|
$endRange1stOctet = 192
|
|
|
|
# Define the range for the 2nd octet (adjust as needed)
|
|
$startRange2ndOctet = 168
|
|
$endRange2ndOctet = 168
|
|
|
|
# Define the range for the 3rd octet (adjust as needed)
|
|
$startRange3rdOctet = 001
|
|
$endRange3rdOctet = 001
|
|
|
|
# Define the range for the 4th octet (adjust as needed)
|
|
$startRange4thOctet = 001
|
|
$endRange4thOctet = 254
|
|
|
|
# Define the arrays to store discovered machines
|
|
#$windowsMachines = @()
|
|
#$neighborMachines = @()
|
|
$targets = @()
|
|
|
|
# Loop through the specified IP range
|
|
for ($subnetOctet1 = $startRange1stOctet; $subnetOctet1 -le $endRange1stOctet; $subnetOctet1++) {
|
|
for ($subnetOctet2 = $startRange2ndOctet; $subnetOctet2 -le $endRange2ndOctet; $subnetOctet2++) {
|
|
for ($subnetOctet3 = $startRange3rdOctet; $subnetOctet3 -le $endRange3rdOctet; $subnetOctet3++) {
|
|
for ($subnetOctet4 = $startRange4thOctet; $subnetOctet4 -le $endRange4thOctet; $subnetOctet4++) {
|
|
$targetIP= "$subnetOctet1.$subnetOctet2.$subnetOctet3.$subnetOctet4"
|
|
$targets += [PSCustomObject]@{
|
|
IP = $targetIP
|
|
}
|
|
Write-Host "$targetIP added to list!"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
# Specify the length of each sub-list
|
|
$batchLength = 20
|
|
# Initialize arrays to store sublists
|
|
$targetBatches = @()
|
|
1..($batchNumber) | ForEach-Object {
|
|
$targetBatches += @(($_),($_+1))
|
|
}
|
|
# Loop through the original list and add addresses to the empty $targetBatches lists.
|
|
$targetBatches | ForEach-Object {
|
|
# Add a batch of addresses to the current list
|
|
$_ += $targets[0..($batchLength)]
|
|
# Remove the corresponding objets from $targets so the next batch is not the same as the first.
|
|
$targets = $targets -notmatch $_
|
|
}
|
|
#$targets | ForEach-Object {}
|
|
#for ($i = 0; $i -lt $targets.Count) {
|
|
# $batchList = $targets[$i..($i + $batchLength - 1)].IP
|
|
# $batch += [PSCustomObject]@{
|
|
# IPlist = $batchList
|
|
# }
|
|
# $targetBatches += $batch
|
|
#}
|
|
# Initialize output arrays
|
|
$neigborMachines = @()
|
|
|
|
# Script block to process each sub-list
|
|
$discoverScript = {
|
|
param ($targetIP)
|
|
# Add your processing logic here
|
|
$smbResult = Test-NetConnection -ComputerName $targetIP -Port 445 -ErrorAction Continue
|
|
$dnsResult = Test-NetConnection -ComputerName $targetIP -Port 53 -ErrorAction Continue
|
|
$ftpResult = Test-NetConnection -ComputerName $targetIP -Port 21 -ErrorAction Continue
|
|
$sshResult = Test-NetConnection -ComputerName $targetIP -Port 22 -ErrorAction Continue
|
|
$targetMac = Get-NetNeighbor -IPAddress $targetIP
|
|
$neighborMachines += [PSCustomObject]@{
|
|
isWindowsMachine = $smbResult.TcpTestSucceeded
|
|
isDnsServer = $dnsResult.TcpTestSucceeded
|
|
isFtpServer = $ftpResult.TcpTestSucceeded
|
|
isSshServer = $sshResult.TcpTestSucceeded
|
|
macAddress = $targetMac.LinkLayerAddress
|
|
}
|
|
}
|
|
|
|
# Array for storing jobs
|
|
$jobManager = @()
|
|
|
|
# Output the current $targetBatches array for troubleshooting
|
|
$targetBatches | Export-Csv -Path D:/discovery-data/test/targetbatches.csv -NoTypeInformation
|
|
#foreach ($batch in $targetBatches) {
|
|
# Write-Host "$batch"
|
|
# $job = Start-Job -ScriptBlock $discoverScript -ArgumentList $batch
|
|
# Write-Host "Queuing job: $job ID $batch"
|
|
# $jobManager += $job
|
|
# if ($jobManager.Count -ge 50) {
|
|
# $completedJob = Wait-Job -Job $jobManager -Any
|
|
# $jobManager = $jobManager | Where-Object { $_ -ne $completedJob }
|
|
# }
|
|
#}
|
|
|
|
$targetBatches | ForEach-Object {
|
|
Write-Host "Current Batch:"
|
|
$batchID = $_[0].IP
|
|
Write-Host $batchID
|
|
foreach ($target in $_) {
|
|
$job = Start-Job -Name $batchID -ScriptBlock $discoverScript -ArgumentList $target.IP
|
|
Write-Host "Queueing job $batchID"
|
|
$jobManager += $job
|
|
if ($jobManager.Count -ge 50) {
|
|
$completedJob = Wait-Job -Job $jobManager -Any
|
|
$jobManager = $jobManager | Where-Object { $_ -ne $completedJob }
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$jobManager | Wait-Job
|
|
|
|
$neighborList = $jobManager | Receive-Job
|
|
|
|
$neighborList | Export-Csv -Path D:/discovery-data/test/Neighborlist.csv -NoTypeInformation
|
|
|
|
|