How to pass computernames to this ps1 file?

I have a ps1 file (attached) which expects the -computername param like:
GetLocalAccount.ps1 -computername "host1","host2,..."

Open in new window


Instead of entering each computername manually, how can I pass a file list computerlist01.txt of computer names to it?

e.g. computerlist01.txt
host1
host2

Open in new window

GetLocalAccount.txt
LVL 27
NVITEnd-user supportAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The most simple approach is to just use the file content as parameter in one go:
GetLocalAccount.ps1 -computername (get-content computerlist01.txt)

Open in new window

Because -computername is the first positional parameter, it should also work using the pipeline:
get-content computerlist01.txt | GetLocalAccount.ps1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportAuthor Commented:
Qlemo,

Your first solution works. The 2nd has error. Not sure but I think because it's running PSVersion 2.0?

get-content computerlist01.txt | .\GetLocalAccount.ps1

The input object cannot be bound to any parameters for the command 
either because the command does not take pipeline input or the input and 
its properties do not match any of the parameters that take pipeline 
input. 

At line:1 char:55
+ get-content computerlist01.txt | .\GetLocalAccount.ps1 <<<< 
    + CategoryInfo          : InvalidArgument: (DCVXSBY1:PSObject) [GetLocalAccount.ps1], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,GetLocalAccount.ps1
 
The input object cannot be bound to any parameters for the command 
either because the command does not take pipeline input or the input and 
its properties do not match any of the parameters that take pipeline 
input. 


At line:1 char:55
+ get-content computerlist01.txt | .\GetLocalAccount.ps1 <<<< 
    + CategoryInfo          : InvalidArgument: (DDX6XXV1:PSObject) [GetLocalAccount.ps1], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,GetLocalAccount.ps1

Open in new window

NVITEnd-user supportAuthor Commented:
Actually, it "over-works" haha, meaning it repeats the search for each computername. The computernames higher in the list have the most repeats. The computernames at the bottom, the least.

Here's the code:
#--------------------------------------------------------------------------------- 
#The sample scripts are not supported under any Microsoft standard support 
#program or service. The sample scripts are provided AS IS without warranty  
#of any kind. Microsoft further disclaims all implied warranties including,  
#without limitation, any implied warranties of merchantability or of fitness for 
#a particular purpose. The entire risk arising out of the use or performance of  
#the sample scripts and documentation remains with you. In no event shall 
#Microsoft, its authors, or anyone else involved in the creation, production, or 
#delivery of the scripts be liable for any damages whatsoever (including, 
#without limitation, damages for loss of business profits, business interruption, 
#loss of business information, or other pecuniary loss) arising out of the use 
#of or inability to use the sample scripts or documentation, even if Microsoft 
#has been advised of the possibility of such damages 
#--------------------------------------------------------------------------------- 

#requires -Version 2.0

<#
 	.SYNOPSIS
       This script can be list all of local user account.
    .DESCRIPTION
       This script can be list all of local user account.
    .PARAMETER  <AccountName>
		Specifies the local user account you want to search.
	.PARAMETER	<ComputerName <string[]>
		Specifies the computers on which the command runs. The default is the local computer. 
	.PARAMETER  <Credential>
		Specifies a user account that has permission to perform this action. 
	.EXAMPLE
        C:\PS> C:\Script\GetLocalAccount.ps1
		
		This example shows how to list all of local users on local computer.	
	.EXAMPLE
        C:\PS> C:\Script\GetLocalAccount.ps1 | Export-Csv -Path "D:\LocalUserAccountInfo.csv" -NoTypeInformation
		
		This example will export report to csv file. If you attach the <NoTypeInformation> parameter with command, it will omit the type information 
		from the CSV file. By default, the first line of the CSV file contains "#TYPE " followed by the fully-qualified name of the object type.
    .EXAMPLE
        C:\PS> C:\Script\GetLocalAccount.ps1 -AccountName "Administrator","Guest"
		
		This example shows how to list local Administrator and Guest account information on local computer.
    .EXAMPLE
        C:\PS> $Cre=Get-Credential
		C:\PS> C:\Script\GetLocalAccount.ps1 -Credential $Cre -Computername "WINSERVER" 
		
		This example lists all of local user accounts on the WINSERVER remote computer.
#>

Param
(
	[Parameter(Position=0,Mandatory=$false)]
	[ValidateNotNullorEmpty()]
	[Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
	[Parameter(Position=1,Mandatory=$false)]
	[Alias('un')][String[]]$AccountName,
	[Parameter(Position=2,Mandatory=$false)]
	[Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
	
$Obj = @()

Foreach($Computer in $ComputerName)
{
	If($Credential)
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction SilentlyContinue
	}
	else
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction SilentlyContinue
	}
	
	Foreach($LocalAccount in $AllLocalAccounts)
	{
		$Object = New-Object -TypeName PSObject
		
		$Object|Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
		$Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
		$Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
		$Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
		$Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
		$Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
		$Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
		$Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
		$Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
		$Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
		$Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
		$Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
		$Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
		$Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
		$Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
		
		$Obj+=$Object
	}
	
	If($AccountName)
	{
		Foreach($Account in $AccountName)
		{
			$Obj|Where-Object{$_.Name -like "$Account"}
		}
	}
	else
	{
		$Obj
	}
}

Open in new window

Example, with computerlist01.txt containing :
host1
host2
host3
host4

Open in new window


Results:

"Computer","Name","Full Name","Caption","Disabled","Status","LockOut","Password Changeable","Password Expires","Password Required","SID","SID Type","Account Type","Domain","Description"
"HOST1","Acronis Agent User","Acronis Agent User","HOST1\Acronis Agent User","False","OK","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-1002","1","512","HOST1",""
"HOST1","Administrator","","HOST1\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-500","1","512","HOST1","Built-in account for administering the computer/domain"
"HOST1","locadmin","","HOST1\locadmin","False","OK","False","True","False","False","S-1-5-21-2682663483-2807901651-3846698284-1000","1","512","HOST1",""
"HOST1","NoGuest","","HOST1\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2682663483-2807901651-3846698284-501","1","512","HOST1","Built-in account for guest access to the computer/domain"
"HOST1","test","","HOST1\test","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1016","1","512","HOST1",""
"HOST1","wtfmin","","HOST1\wtfmin","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1018","1","512","HOST1",""
"HOST1","__vmware_user__","__vmware_user__","HOST1\__vmware_user__","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1004","1","512","HOST1","VMware User"
"HOST1","Acronis Agent User","Acronis Agent User","HOST1\Acronis Agent User","False","OK","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-1002","1","512","HOST1",""
"HOST1","Administrator","","HOST1\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-500","1","512","HOST1","Built-in account for administering the computer/domain"
"HOST1","locadmin","","HOST1\locadmin","False","OK","False","True","False","False","S-1-5-21-2682663483-2807901651-3846698284-1000","1","512","HOST1",""
"HOST1","NoGuest","","HOST1\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2682663483-2807901651-3846698284-501","1","512","HOST1","Built-in account for guest access to the computer/domain"
"HOST1","test","","HOST1\test","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1016","1","512","HOST1",""
"HOST1","wtfmin","","HOST1\wtfmin","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1018","1","512","HOST1",""
"HOST1","__vmware_user__","__vmware_user__","HOST1\__vmware_user__","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1004","1","512","HOST1","VMware User"
"HOST2","Administrator","","HOST2\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-500","1","512","HOST2","Built-in account for administering the computer/domain"
"HOST2","locadmin","","HOST2\locadmin","False","OK","False","True","False","False","S-1-5-21-2492592069-3047220098-144666526-1000","1","512","HOST2",""
"HOST2","cr_Cosential01","","HOST2\cr_Cosential01","False","OK","False","False","False","True","S-1-5-21-2492592069-3047220098-144666526-1004","1","512","HOST2",""
"HOST2","NETMON","NETMON","HOST2\NETMON","False","OK","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-1003","1","512","HOST2",""
"HOST2","NoGuest","","HOST2\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2492592069-3047220098-144666526-501","1","512","HOST2","Built-in account for guest access to the computer/domain"
"HOST1","Acronis Agent User","Acronis Agent User","HOST1\Acronis Agent User","False","OK","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-1002","1","512","HOST1",""
"HOST1","Administrator","","HOST1\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-500","1","512","HOST1","Built-in account for administering the computer/domain"
"HOST1","locadmin","","HOST1\locadmin","False","OK","False","True","False","False","S-1-5-21-2682663483-2807901651-3846698284-1000","1","512","HOST1",""
"HOST1","NoGuest","","HOST1\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2682663483-2807901651-3846698284-501","1","512","HOST1","Built-in account for guest access to the computer/domain"
"HOST1","test","","HOST1\test","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1016","1","512","HOST1",""
"HOST1","wtfmin","","HOST1\wtfmin","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1018","1","512","HOST1",""
"HOST1","__vmware_user__","__vmware_user__","HOST1\__vmware_user__","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1004","1","512","HOST1","VMware User"
"HOST2","Administrator","","HOST2\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-500","1","512","HOST2","Built-in account for administering the computer/domain"
"HOST2","locadmin","","HOST2\locadmin","False","OK","False","True","False","False","S-1-5-21-2492592069-3047220098-144666526-1000","1","512","HOST2",""
"HOST2","cr_Cosential01","","HOST2\cr_Cosential01","False","OK","False","False","False","True","S-1-5-21-2492592069-3047220098-144666526-1004","1","512","HOST2",""
"HOST2","NETMON","NETMON","HOST2\NETMON","False","OK","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-1003","1","512","HOST2",""
"HOST2","NoGuest","","HOST2\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2492592069-3047220098-144666526-501","1","512","HOST2","Built-in account for guest access to the computer/domain"
"HOST3",,,,,,,,,,,,,,
"HOST1","Acronis Agent User","Acronis Agent User","HOST1\Acronis Agent User","False","OK","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-1002","1","512","HOST1",""
"HOST1","Administrator","","HOST1\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2682663483-2807901651-3846698284-500","1","512","HOST1","Built-in account for administering the computer/domain"
"HOST1","locadmin","","HOST1\locadmin","False","OK","False","True","False","False","S-1-5-21-2682663483-2807901651-3846698284-1000","1","512","HOST1",""
"HOST1","NoGuest","","HOST1\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2682663483-2807901651-3846698284-501","1","512","HOST1","Built-in account for guest access to the computer/domain"
"HOST1","test","","HOST1\test","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1016","1","512","HOST1",""
"HOST1","wtfmin","","HOST1\wtfmin","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1018","1","512","HOST1",""
"HOST1","__vmware_user__","__vmware_user__","HOST1\__vmware_user__","False","OK","False","True","True","True","S-1-5-21-2682663483-2807901651-3846698284-1004","1","512","HOST1","VMware User"
"HOST2","Administrator","","HOST2\Administrator","True","Degraded","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-500","1","512","HOST2","Built-in account for administering the computer/domain"
"HOST2","locadmin","","HOST2\locadmin","False","OK","False","True","False","False","S-1-5-21-2492592069-3047220098-144666526-1000","1","512","HOST2",""
"HOST2","cr_Cosential01","","HOST2\cr_Cosential01","False","OK","False","False","False","True","S-1-5-21-2492592069-3047220098-144666526-1004","1","512","HOST2",""
"HOST2","NETMON","NETMON","HOST2\NETMON","False","OK","False","True","False","True","S-1-5-21-2492592069-3047220098-144666526-1003","1","512","HOST2",""
"HOST2","NoGuest","","HOST2\NoGuest","True","Degraded","False","False","False","False","S-1-5-21-2492592069-3047220098-144666526-501","1","512","HOST2","Built-in account for guest access to the computer/domain"
"HOST3",,,,,,,,,,,,,,
"HOST4",,,,,,,,,,,,,,

Open in new window

Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

NVITEnd-user supportAuthor Commented:
This is the cause, I think. Currently, it concatenates until all the hostnames are processed. I need to figure a way to exit this loop:
Foreach($LocalAccount in $AllLocalAccounts)

Open in new window


and output those results for the given host.
NVITEnd-user supportAuthor Commented:
Got it...

Param
(
	[Parameter(Position=0,Mandatory=$false)]
	[ValidateNotNullorEmpty()]
	[Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
	[Parameter(Position=1,Mandatory=$false)]
	[Alias('un')][String[]]$AccountName,
	[Parameter(Position=2,Mandatory=$false)]
	[Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
	
Foreach($Computer in $ComputerName)
{
	If($Credential)
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction SilentlyContinue
	}
	else
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction SilentlyContinue
	}
	
    $Obj = @()
	Foreach($LocalAccount in $AllLocalAccounts)
	{
		$Object = New-Object -TypeName PSObject
		
		$Object|Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
		$Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
		$Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
		$Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
		$Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
		$Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
		$Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
		$Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
		$Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
		$Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
		$Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
		$Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
		$Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
		$Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
		$Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
		
		$Obj+=$Object
	}
	If($AccountName)
	{
		Foreach($Account in $AccountName)
		{
			$Obj|Where-Object{$_.Name -like "$Account"}
		}
	}
	else
	{
		$Obj
	}
}

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If possible, upgrade your PowerShell to at least 4. It's worth it, and adds some more power to your PS, removing some nasty bugs ;-).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.