When I started using Powershell parameters, I had a parameter for everything. Now I have switched to using a single parameter for all relevant input and parse the parameter data for processing.
All I’m doing is setting the parameter ConfigTarget and then parsing the input to see how the script should get the computer target list.
- If ConfigTarget is set to Domain, then perform a Get-ADComputer against the domain.
- If ConfigTarget is set to an OU DN, then perform a Get-ADComputer using the OU DN as the starting point.
- If ConfigTarget is set to an OU CN, the script converts the CN into a DN, and then performs a Get-ADComputer using the OU DN as the starting point.
- If ConfigTarget is set to a single computername, the script performs the script action against the single computer.
- If ConfigTarget is set to a file path, the script imports the file, parses the data and imports as a computer list, and then performs the script action against the list of computers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | <# .PARAMETER ConfigTarget Set the ComputerType parameter to determine what computer(s) the script targets. Options: Enter "Domain" to have the script target all computers pf a specific type in the domain. Enter an OU (DN or CN) from which the script will get a computer list. Enter a computer name (hostname only). Enter a file path the contains a text or csv file containing a computer list. PARAMETER ALIAS: Computer or Type or CT Example: Update-LocalAdminPw.ps1 -ConfigTarget "Domain" Example: Update-LocalAdminPw.ps1 -ConfigTarget "OU=Workstations,OU=ComputerTarget,DC=example,DC=com" Example: Update-LocalAdminPw.ps1 -ConfigTarget "example.com\ComputerTarget\Workstations" Example: Update-LocalAdminPw.ps1 -ConfigTarget "COMPUTERNAME" Example: Update-LocalAdminPw.ps1 -ConfigTarget "c:\temp\computerlist.txt" #> Param ( [parameter(Mandatory=$False)] [alias("Target","OUName","ComputerName","Computer")] [string]$ConfigTarget = "Domain", ) Import-Module ActiveDirectory ############################### # Format Computer Target List # ############################### # If $ConfigTarget is a single computername set here IF (($ConfigTarget -notlike "*DC=*") -OR ($ConfigTarget -notlike "*/*") -OR ($ConfigTarget -ne "Domain") -OR ($ConfigTarget -notlike "*:\*") -OR ($ConfigTarget -notlike "*,*") ) { ## OPEN IF ConfigTarget is a computer name [array] $TargetComputerList += $ConfigTarget } ## OPEN IF ConfigTarget is a computer name # If $ConfigTarget is a comma delimited list set here IF ($ConfigTarget -like "*,*") { ## OPEN IF comma delimited list Write-Output "User list file identified as comma delimited `r " $ComputerList = $ConfigTarget -Replace(" ","") [array]$ComputerListArray = $ComputerList -split(",") [array] $TargetComputerList = $ComputerListArray } ## CLOSE IF comma delimited list ######################################## # Get Computer Target List from a file # ######################################## IF ($ConfigTarget -like "*:\*") { ## OPEN IF File $FileCheck = Test-Path $File IF ($FileCheck -eq $True) { ## OPEN IF The path to the Computers list file is true $ComputerList = Get-Content $File $ComputerListCount = $ComputerList.Count Write-Output "Importing $ComputerListCount Users from $File... `r " } ## CLOSE IF The path to the Computers list file is true ELSE { Write-Warning "Path to Computer list: $File is invalid. `r " ; exit } IF ($ComputerList -like ",") { ## OPEN IF the imported list of Computers is comma delimited Write-Output "User list file identified as comma delimited `r " $ComputerList = $ComputerList -Replace(" ","") [array]$ComputerListArray = $ComputerList -split(",") $Computers = $ComputerListArray } ## CLOSE IF the imported list of Computers is comma delimited } ## CLOSE IF File ################################### # Get a list of computers from OU # ################################### IF (($ConfigTarget -like "*DC=*") -OR ($ConfigTarget -like "*/*")) { ## OPEN IF OUName is specified IF (!$ComputerType) { Write-Warning "When defining the target as an OU, the ComputerType parameter must be defined. `r " ; exit } Write-Verbose "Check the OU path info `r " IF ($ConfigTarget -like "*DC=*") # This identifies the OU input as a DN OU path # The OU DN is required for the next component that gathers the computers in the OU path. { ## OPEN IF OUPath contains "DC=" which means it is a DN Name $OUCheck = $True Write-Output "The specified OU path ($ConfigTarget) is already a DN. Continuing... `r " } ## CLOSE IF OUPath contains "/" which means it is a Canonical Name IF ($ConfigTarget -like "*/*") # This identifies the OU input as a CN OU path which is then converted to be a DN # The OU DN is required for the next component that gathers the computers in the OU path. { ## OPEN IF OUPath contains "/" which means it is a Canonical Name # Create the array by spitting the text input on the "/" $OUCheck = $True $OUPathArray = $ConfigTarget.split("/") IF ($OUPathArray[0] -like "*.*") { ## OPEN IF The first item in the array is a domain name $Len = $OUPathArray.length $Len-- $Num = 1 $NewArray = @(1..$Len) for($i=0; $i -lt $Len; $i++) { ## OPEN FOR $NewArray[$i] = $OUPathArray[$Num] $Num++ } ## CLOSE FOR $OUPathArray = $NewArray } ## CLOSE IF The first item in the array is a domain name # Reverse the order of the aray to build the DN appropriately [array]::Reverse($OUPathArray) $OUPathCount = $OUPathArray.count # Loop through the OU data to build the DN $OUPath = "OU=" $OUDelimiter = ",OU=" For ($CountOU = 0;$CountOU -le $OUPathCount;$CountOU++) { ## OPEN Bracket FOR loop to ensure the LDAP path is properly built IF ($CountOU -eq 0) {$OUPath = $OUPath + $OUPathArray[0];$CountOU++ } IF ($OUPathArray[$CountOU]) {$OUPath = $OUPath + $OUDelimiter + $OUPathArray[$CountOU]} } ## CLOSE Bracket FOR loop to ensure the LDAP path is properly built # Build the full DN by appending the domain DN to the OUPath created in the loop above $ComputerTargetOU = "$OUPath,$ADDomainDistinguishedName" write-output " `r " write-output "OU is now set to $ComputerTargetOU `r " write-output " `r " } ## CLOSE IF OUPath contains "/" which means it is a Canonical Name IF ($OUCheck -eq $False) { Write-Warning "OU entered ($ConfigTarget) is not a valid Canonical Name or Distinguished Name. Exiting... `r " ; exit } Switch ($ComputerType) # This determines what type of computer object is returned: workstation or server { ## OPEN Switch ComputerType Workstation { ## OPEN Switch Workstation Option # Gather a list of all active workstations in specified OU tree including necessary attributes write-output "Discovering active workstations in $ComputerTargetOU ..." `r $AllActiveWorkstations = Get-ADComputer -SearchBase $ComputerTargetOU -filter {(OperatingSystem -like "*Windows*") -and (OperatingSystem -notlike "*Server*") -and (passwordLastSet -ge $ComputerStaleDate) -and (Enabled -eq $TRUE) } $AllActiveComputersCount = $AllActiveWorkstations.Count write-output ""`r write-output "There were $AllActiveComputersCount active workstations discovered in $ComputerTargetOU ..." `r write-output ""`r [array]$ComputerList = $AllActiveWorkstations } ## CLOSE Switch Workstation Option Server { ## OPEN Switch Server Option # Gather a list of all active Servers (not including Domain Controllers) in specified OU tree including necessary attributes write-output "Discovering Active Windows SERVERS in $ComputerTargetOU ..." `r $AllActiveServers = Get-ADComputer -SearchBase $ComputerTargetOU -filter {(OperatingSystem -like "*Windows*") -and (OperatingSystem -like "*Server*") -and (passwordLastSet -ge $ComputerStaleDate) -and (Enabled -eq $TRUE)-and (PrimaryGroupID -eq 515) } $AllActiveComputersCount = $AllActiveServers.Count write-output "" `r write-output "There were $AllActiveComputersCount Active Windows SERVERS discovered in $ComputerTargetOU ..." `r write-output "" `r [array]$ComputerList = $AllActiveServers } ## CLOSE Switch Server Option } ## CLOSE Switch ComputerType ForEach ($Computer in $ComputerList) { ## OPEN Computer in ComputerList $TargetComputerList += $Computer.Name } ## CLOSE Computer in ComputerList } ## CLOSE IF OUName is specified IF ($ConfigTarget -eq "Domain") { ## OPEN IF ConfigTarget = Domain Switch ($ComputerType) # This determines what type of computer object is returned: workstation or server { ## OPEN Switch ComputerType Workstation { ## OPEN Switch Workstation Option # Gather a list of all active workstations in specified OU tree including necessary attributes write-output "Discovering active workstations in $ADDomainDNSRoot ..." `r $AllActiveWorkstations = Get-ADComputer -filter {(OperatingSystem -like "*Windows*") -and (OperatingSystem -notlike "*Server*") -and (passwordLastSet -ge $ComputerStaleDate) -and (Enabled -eq $TRUE) } $AllActiveComputersCount = $AllActiveWorkstations.Count write-output ""`r write-output "There were $AllActiveComputersCount active workstations discovered in $ADDomainDNSRoot ..." `r write-output ""`r [array]$ComputerList = $AllActiveWorkstations } ## CLOSE Switch Workstation Option Server { ## OPEN Switch Server Option # Gather a list of all active Servers (not including Domain Controllers) in specified OU tree including necessary attributes write-output "Discovering Active Windows SERVERS in $ADDomainDNSRoot ..." `r $AllActiveServers = Get-ADComputer -filter {(OperatingSystem -like "*Windows*") -and (OperatingSystem -like "*Server*") -and (passwordLastSet -ge $ComputerStaleDate) -and (Enabled -eq $TRUE)-and (PrimaryGroupID -eq 515) } $AllActiveComputersCount = $AllActiveServers.Count write-output "" `r write-output "There were $AllActiveComputersCount Active Windows SERVERS discovered in $ADDomainDNSRoot ..." `r write-output "" `r [array]$ComputerList = $AllActiveServers } ## CLOSE Switch Server Option } ## CLOSE Switch ComputerType ForEach ($Computer in $ComputerList) { ## OPEN Computer in ComputerList $TargetComputerList += $Computer.Name } ## CLOSE Computer in ComputerList } ## CLOSE IF ConfigTarget = Domain # Sort the list alphabetically $ComputerList = $ComputerList | sort $TargetComputerListCount = $TargetComputerList.Count # Output Computer list to ComputerWorkFile write-output ""`r write-output "Saving computer job list to computer work file..."`r write-output ""`r $TargetComputerList | out-file $ComputerWorkFile -force |