I have a text file containing a list of servers, one per line, like:
SERVER1
SERVER2
SERVER3
When I do a Get-Content on the file, I do see the output as:
SERVER1
SERVER2
SERVER3
So now I am writing a function that I want to take in the multiple servers as an array, and iterate over the array in the function. The function is currently like this:
function Get-LocalAdministrators
{
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$computers
)
foreach ($computername in $computers)
{
$ADMINS = get-wmiobject -computername $computername -query "select * from win32_groupuser where GroupComponent=""Win32_Group.Domain='$computername',Name='administrators'""" | % {$_.partcomponent}
foreach ($ADMIN in $ADMINS)
{
$admin = $admin.replace("\\$computername\root\cimv2:Win32_UserAccount.Domain=","") # trims the results for a user
$admin = $admin.replace("\\$computername\root\cimv2:Win32_Group.Domain=","") # trims the results for a group
$admin = $admin.replace('",Name="',"\")
$admin = $admin.REPLACE("""","") # strips the last "
$objOutput = New-Object PSObject -Property @{
Machinename = $($computername)
Fullname = ($admin)
#DomainName =$admin.split("\")[0]
#UserName = $admin.split("\")[1]
}
$objReport+=@($objOutput)
}
return $objReport
}
}
Then I plan to call the function as:
Get-Content “C:\temp\computers.txt” | Get-LocalAdministrators
However, when I run the function, I can see that the value of $computers is {SERVER3} (i.e. only the last line in the file.) I have tried to find the answer to this via teh Google, and although there are a lot of array references/examples, I cannot find one where it combines reading the values from a file, into an array in a param statement. Please forgive my PS newb ignorance, and provide the clue that I need... Thanks.
UPDATE: Link to screenshot of script running in PowerGUI Script Editor showing the value of $computers during the run: debug run screenshot