194 lines
7.6 KiB
PowerShell
194 lines
7.6 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 = 0
|
|
$endRange3rdOctet = 2
|
|
|
|
# Define the range for the 4th octet (adjust as needed)
|
|
$startRange4thOctet = 1
|
|
$endRange4thOctet = 254
|
|
|
|
# Define the arrays to store discovered machines
|
|
$windowsMachines = @()
|
|
$neighborMachines = @()
|
|
|
|
# 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++) {
|
|
$targetIPs += "$subnetOctet1.$subnetOctet2.$subnetOctet3.$subnetOctet4"
|
|
|
|
# Check if the host is reachable
|
|
$isReachable = Test-Connection -ComputerName $targetIP -Count 1 -ErrorAction SilentlyContinue
|
|
|
|
if ($isReachable) {
|
|
# Resolve the IP address to a hostname
|
|
$targethostName = (Resolve-DnsName -Name $targetIP).NameHost
|
|
# Detect MAC address of target
|
|
$targetmacAddress = (Get-NetNeighbor -IPAddress $targetIP).LinkLayerAddress
|
|
# For organizational purposes, here is the place to put other tests other than samba:
|
|
#
|
|
# Check if port 445 (SMB) is open to identify a Windows machine
|
|
$smbResult = Test-NetConnection -ComputerName $targetIP -Port 445 -ErrorAction SilentlyContinue
|
|
if ($smbResult.TcpTestSucceeded) {
|
|
$targetSMB = "Yes"
|
|
$windowsMachines += [PSCustomObject]@{
|
|
Hostname = $targethostName
|
|
IP = $targetIP
|
|
MAC = $targetmacAddress
|
|
}
|
|
}
|
|
else {
|
|
$targetSMB = "No"
|
|
}
|
|
$neighborMachines += [PSCustomObject]@{
|
|
Hostname = $targethostName
|
|
IP = $targetIP
|
|
MAC = $targetmacAddress
|
|
Windows = $targetSMB
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
###########
|
|
#
|
|
# Multithreading WIP Code
|
|
#
|
|
#
|
|
## Install the PSThreadJob module if not already installed
|
|
#Install-Module -Name PSThreadJob -Force -Scope CurrentUser
|
|
|
|
## Import the PSThreadJob module
|
|
#Import-Module PSThreadJob
|
|
#function Test-Range {
|
|
# param (
|
|
# [string]$ipRange
|
|
# )
|
|
# $ips = 1..254 | ForEach-Object {
|
|
# "$ipRange.$_"
|
|
# }
|
|
# for ($i = 0; $i -lt $ips.Length; $i++) {
|
|
# $targetIP = $ips[$i]
|
|
## Check if the host is reachable
|
|
# $isReachable = Test-Connection -ComputerName $targetIP -Count 1 -ErrorAction SilentlyContinue
|
|
# if ($isReachable) {
|
|
# # Resolve the IP address to a hostname
|
|
# $targethostName = (Resolve-DnsName -Name $targetIP).NameHost
|
|
# # Detect MAC address of target
|
|
# $targetmacAddress = (Get-NetNeighbor -IPAddress $targetIP).LinkLayerAddress
|
|
## For organizational purposes, here is the place to put other tests other than samba:
|
|
# $ftpResult = Test-NetConnection -ComputerName $targetIP -Port 21 -ErrorAction SilentlyContinue
|
|
# if ($ftpResult.TcpTestSucceeded) {
|
|
# $targetFtp = "Yes"
|
|
# }
|
|
# $dnsResult = Test-NetConnection -ComputerName $targetIP -Port 53 -ErrorAction SilentlyContinue
|
|
# if ($dnsResult.TcpTestSucceeded) {
|
|
# $targetDns= "Yes"
|
|
# }
|
|
## Check if port 445 (SMB) is open to identify a Windows machine
|
|
# $smbResult = Test-NetConnection -ComputerName $targetIP -Port 445 -ErrorAction SilentlyContinue
|
|
# if ($smbResult.TcpTestSucceeded) {
|
|
# $targetSMB = "Yes"
|
|
# $windowsMachines += [PSCustomObject]@{
|
|
# Hostname = $targethostName
|
|
# IP = $targetIP
|
|
# MAC = $targetmacAddress
|
|
# }
|
|
# }
|
|
# else {
|
|
# $targetSMB = "No"
|
|
# }
|
|
# $neighborMachines += [PSCustomObject]@{
|
|
# Hostname = $targethostName
|
|
# IP = $targetIP
|
|
# MAC = $targetmacAddress
|
|
# Windows = $targetSMB
|
|
# DNS = $targetDns
|
|
# FTP = $targetFtp
|
|
# }
|
|
# }
|
|
# }
|
|
#}
|
|
## Start a thread job for each IP range
|
|
#$jobs = $ipRanges | ForEach-Object {
|
|
# Start-ThreadJob -ScriptBlock { Test-Range } -ArgumentList $_
|
|
#}
|
|
|
|
## Wait for all thread jobs to complete
|
|
#$jobs | Wait-Job
|
|
|
|
## Receive the results from all thread jobs
|
|
#$jobs | ForEach-Object {
|
|
# $windowsMachines += Receive-Job -Job $_
|
|
# $neighborMachines += Receive-Job -Job $_
|
|
#}
|
|
|
|
## Stop and remove the thread jobs
|
|
#$jobs | Remove-Job
|
|
|
|
####
|
|
#
|
|
# End Section: Multithreading WIP Code
|
|
#
|
|
#
|
|
###########
|
|
|
|
# Export the list of discovered Windows machines to a CSV file
|
|
$windowsMachines | Export-Csv -Path $windowsFilePath -NoTypeInformation
|
|
$neighborMachines | Export-Csv -Path $neighborFilePath -NoTypeInformation
|
|
Write-Host "List of Windows Machines on the LAN saved to $windowsFilePath."
|
|
Write-Host "Details of all network devices saved to $neighborFilePath."
|
|
|
|
# Keep the PowerShell window open for testing.
|
|
Read-Host "Press Enter to exit..." |