Powershell : Random Server selection (with variables)

If you need to randomly select some servers from your Active Directory to then complete further scanning and analysis with then sometimes the human cannot be random enough, that is where you need to call on Powershell.

First, lets address getting a list of server names that match a variable and adding them to text file and exporting them on screen as well:

Script : RandomServerSelection.ps1

# Define the number of servers to pick
$serverCount = 30

# Get all servers from Active Directory that start with ST1W and are running a Server OS
$allServers = Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Name -like "HoneyProd-*"} | Select-Object -ExpandProperty Name

# Check if we have enough servers matching the criteria
if ($allServers.Count -lt $serverCount) {
    Write-Warning "Only $($allServers.Count) servers found matching the criteria. All will be selected."
    $randomServers = $allServers
} else {

# Select 30 random servers
    $randomServers = $allServers | Get-Random -Count $serverCount
}

# Display the selected servers
Write-Output "Randomly selected servers:"
$randomServers

# Output the list of selected servers to servers.txt
$randomServers | Out-File -FilePath "servers.txt"

# Display the number of servers selected
Write-Output "`nTotal servers selected: $($randomServers.Count)"

That will then give you a list of randomly selected servers based on your variables set as below:


However, with many servers it would be nice to check the RPC is online and working before making a selection as you could be selecting servers that that offline or not accessible, so the script below will give you the same style of report but with a confimation that RPC can be contacted:

Script : RandomServerSelection-RPC.ps1

# Define the number of servers to pick
$serverCount = 30

# Get only servers with the "ST" prefix from Active Directory by filtering on the Name attribute and OperatingSystem attribute
$servers = Get-ADComputer -Filter {OperatingSystem -like "*Server*" -and Name -like "ST*"} | Select-Object -ExpandProperty Name

# Select 30 random servers
$randomServers = $servers | Get-Random -Count $serverCount

# Define the remote communication port (RPC port)
$rpcPort = 135

# Initialize an array to hold servers that are reachable on the RPC port
$reachableServers = @()

# Check each selected server for connectivity on the RC port with a 3-second timeout
foreach ($server in $randomServers) {
    try {
        # Test connectivity on the RC port with a timeout of 3 seconds
        $connectionTest = Test-NetConnection -ComputerName $server -Port $rpcPort -InformationLevel Quiet -WarningAction SilentlyContinue

        if ($connectionTest) {
            Write-Output "$server is reachable on port $rpcPort"
            $reachableServers += $server
        } else {
            Write-Output "$server is NOT reachable on port $rpcPort"
        }
    } catch {
        # Fix: Escape the colon and concatenate the error message
        Write-Output ("Error connecting to " + $server + ": " + $_.Exception.Message)
    }
}

# Output the list of reachable servers to services.txt
$reachableServers | Out-File -FilePath "servers.txt"

# Display the final list of reachable servers
Write-Output "Reachable servers:"
$reachableServers


Previous Post Next Post

نموذج الاتصال