You could turn your script into a function and add a bit of "driver" code to take a CSV with the data and turn it into function calls.
Function AddOne{
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet("User", "Group")]
[String]
$ObjectType,
[Parameter(Mandatory = $true, Position = 2)]
[ValidateScript({ ($_.split("\").count -eq 2) })]
[string]$ObjectName,
[Parameter(Position = 3)]
[String[]]$ComputerName = $env:COMPUTERNAME
)
$ResultsFile = "c:\temp\result.csv"
$ObjDomain = $ObjectName.Split("\")[0]
$ObjName = $ObjectName.Split("\")[1]
$ComputerCount = $ComputerName.Count
$count = 0
Add-Content -Path $ResultsFile -Value "ComputerName,Status,Comments"
foreach ($Computer in $ComputerName) {
$count++
$Status = $null
$Comment = $null
Write-Host ("{0}. Working on {1}" -f $Count, $Computer)
if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Write-Verbose "$Computer : Online"
try {
$GroupObj = [ADSI]"WinNT://$Computer/Administrators"
$GroupObj.Add("WinNT://$ObjDomain/$ObjName")
$Status = "Success"
$Comment = "Added $ObjectName $ObjectType to Local administrators group"
Write-Verbose "Successfully added $ObjectName $ObjectType to $Computer"
}
catch {
$Status = "Failed"
$Comment = $_.toString().replace("`n", "").replace("`r", "")
Write-Verbose "Failed to add $ObjectName $ObjectType to $Computer"
}
Add-Content -Path $ResultsFile -Value ("{0},{1},{2}" -f $Computer, $Status, $Comment )
}
else {
Write-Warning "$Computer : Offline"
Add-Content -Path $ResultsFile -Value ("{0},{1}" -f $Computer, "Offline")
}
}
}
# Use the function
# CSV looks like this:
#Computer,Account,Type
#WS01,Domain\Account,User
#WS01,Domain\Account,Group
Import-CSV c:\Junk\MyCsv.csv |
ForEach-Object{
AddOne -ComputerName $_.Computer -ObjectName $_.Account -ObjectType $_.Type
}