param([ValidateNotNullOrEmpty()][String]$credential=$(throw "*** AD credential is mandatory. e.g. -credential ad1\kyle")); # Install targets, machine names in quotes as they appear in AD. $install_targets= @("CC-NOAH-TEST", "CC-KYLE-CUBE"); $command = { $service = Get-Service -Name CyberCrucibleAgent -ErrorAction SilentlyContinue; if ($service.Length -eq 0) { # No service exists $OutputFilePath="\\DC001\cr\CC-Installer-2024-8-11-T0-23.exe"; #Start-Process -wait calc.exe # Test value, useful for trying on one machine. Start-Process -NoNewWindow -FilePath ${OutputFilePath} -ArgumentList "--eula-accept", "--term", "--group-id", "${GroupId}", "--install-path", "`"C:\Program Files\CyberCrucible`"", "--client", "${ClientAuth}", "--rest-pub-key", "ZUSMPfrngi0d1RTRmi8zJOf1L4AnFu3wN7lnVEb09rY=", "--rest-pub-key", "Y74s8bg+cUS1iEPHSiOJntt9EOg+qshHT/KU0Xkq/9M=", "--rest-pub-key", "ym9awfVnv1nKCfrWF7xrjq/E81H20pix6yShr+zZCJ4=" -wait } } # Lazy O(n) check for name. If efficiency over O(n) is needed, consider sorting # $install_targets first, or implementing a O(log(n)) binary search. function is_install_target($name) { foreach ($target in $install_targets) { if ($target -eq $name) { return $true; } } return $false; } function main() { if ($credential -eq $null) { echo "You must run this script with -credential e.g. -credential ad1\kyle"; exit(-1); } # Gets all computers joined to the domain, we are not concerned with filters here, # since the $install_targets will further filter based on netbios name $computers=get-adcomputer -filter *; $total_in_domain = $computers.Length; echo "$total_in_domain total computers found in the domain."; $matching = @(); foreach ($pc in $computers) { if (is_install_target($pc.Name)) { $matching += $pc; } } $num_matching = $matching.Length; if ($num_matching -gt 0) { echo "$num_matching match the provided list of targets."; echo ""; } else { echo "No computers on the domain matched the given list of install targets."; exit(1); } echo "Checking online status..."; $online = @(); foreach ($pc in $matching) { $name = $pc.Name; Write-Host -NoNewLine " Checking $name... "; if (Test-Connection -ComputerName $name -Quiet -Count 1 -ErrorAction SilentlyContinue) { echo "online"; $online += $pc; } else { echo "failed to establish ICMP ping"; } } $num_online = $online.Length; if ($num_online -gt 0) { echo "$num_online / $num_matching matching machines were online."; } else { echo "No matching computers were able to be connected to via ICMP. Cannot continue."; exit(2); } echo ""; echo "Pushing installer to each machine... using credential $credential"; $online_machine_names = ""; foreach ($pc in $online) { if ($online_machine_names.Length -gt 0) { $online_machine_names += ", "; } $name = $pc.Name; $online_machine_names += $name; } invoke-command $online_machine_names -ScriptBlock $command -Credential $credential -verbose } main;