Found my initial problem with the script I originally posted. Here's the next issue, and I'm sure this is an easy one.
The goal of the following script is to run the specified ADSI command against each computer found in a hostname list text file--example as follows:
Computer01
Computer02
Computer03
The issue is that when running the script, it takes EVERY computer name in the Computer Name file and mashes them together to make one giant computer name, which, obviously is not the computer I am trying to connect to!
Here is the script. The ADSI function that I am trying to run is about 3/4's of the way down.
Function Get-OpenFile($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "Text Files (*.txt)|*.txt)|CSV Files (*.csv)|*.csv)"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
$OpenFileDialog.ShowHelp = $true
}
$InputFile = Get-OpenFile
$Computers = get-content -path $InputFile
Do {
write-host = "Do you want to Enter a Domain Group to Add to the Local Administrators Group? Type Y for Yes, or N for No"
$GroupAddOperation = read-host = " "
If ($GroupAddOperation -eq "N") {break}
Do {
write-host = "Enter Group Name"
$groupname = Read-Host " "
$DomainGroup = $groupname
$LocalGroup = "Administrators"
$Domain = "domainname.com"
$pc = $computers
([ADSI]"WinNT://$pc/$LocalGroup,group").psbase.Invoke("Add",([ADSI]"WinNT://$Domain/$DomainGroup").path)
Write-Host "Do you want to Add Another Group? Enter Y for Yes or N for No"
$AddAnotherGroup = Read-Host " "
}
Until ($AddAnotherGroup -eq "N")
}
Until ($GroupAddOperation -eq "N")
If I run the ADSI command with a "Write-Host" command in front of it, here's what the output is to the PS Console:
([ADSI] WinNT://Computer01.domainname.com Computer02.domainname.com Computer03.domainname.com/Administrators group).psbase.Invoke(Add,([ADSI]WinNT://us.kworld.kpmg.com/us-sg eaudit business).p
ath)
Essentially what I need is for the ADSI command to be run on ALL THREE computers in sequence. What am I doing wrong?
As TessallatingHeckler linked to. Get your computers from a file or from the hash table and assign them to a variable.
$computers = @("Computer01", "Computer02", "Computer03")
foreach($computer in $computers)
{
([ADSI]"WinNT://$computer/$LocalGroup,group").psbase.Invoke("Add",([ADSI]"WinNT://$Domain/$DomainGroup").path)
}
Hope this helps.