Adding Server names to output of a script

I'm trying to write a scrip to ascertain if a particular user account is among the local admin group. The script I have thus far runs fine and lists line by line whether the user account does exist or does not exist. However each line needs to indicate which server is reporting whether the account exists or does not exist. That is my issue. Here is the script and yes I'm a newbie:

foreach($serverin(gcc:\temp\servers.csv)){


$objComputer

=[ADSI]("WinNT://"+$server+",computer")



$colUsers

=($objComputer.psbase.children |


   

Where-Object{$_.psBase.schemaClassName -eq"User"} |


       

Select-Object-expandName)



$blnFound

=$colUsers-contains"joeschmoo"



if

($blnFound)

    {

"The user account exists."}


else


    {

"The user account does not exist."}}

September 8th, 2015 4:12pm

Can you please post the  cannot post colorized script from the ISE without using the code posting control on the toolbar.

If we can't read you script it is likely tat we will not help you.

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 4:28pm

You should use this module: https://gallery.technet.microsoft.com/Local-Account-Management-a777191b

You might also spend some time here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

September 8th, 2015 4:48pm

foreach($server in (gc c:\temp\servers.csv)){
$objComputer = [ADSI]("WinNT://" + $server + ",computer")

$colUsers = ($objComputer.psbase.children |
    Where-Object {$_.psBase.schemaClassName -eq "User"} |
        Select-Object -expand Name)

$blnFound = $colUsers -contains "vmware"

if ($blnFound)
    {"The user account exists."}
else
    {"The user account does not exist."}}
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 11:28am

All your script does is print to the console. What exactly are you trying to do here?
September 9th, 2015 11:36am

Here is how to post and format script in a forum:

foreach($server in (gc c:\temp\servers.csv)){
$server $objComputer = [ADSI]("WinNT://" + $server + ",computer") $colUsers = ($objComputer.psbase.children | Where-Object {$_.psBase.schemaClassName -eq "User"} | Select-Object -expand Name) $blnFound = $colUsers -contains "vmware" if ($blnFound){ "The user account exists." }else{ "The user account does not exist." } }


Note that it is now possible to "see" the logic of the code.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 11:37am

The output the script is generating produces the desired result letting me know if a specified account is a member of the local admin group on each server. However the name of each server is not represented in the output. Ultimately a .csv file should be generated listing line by line the name of each server and the results of the account search.
September 9th, 2015 11:46am

gc c:\temp\servers.csv |
	ForEach-Object{
		$server=$_
		if(([ADSI]"WinNT://$server,computer").Children |?{$_.schemaClassName -eq 'user' -and $_.Name -eq 'vmware'}){
			[pscustomobject]@{Server=$server;IsFound=$true}
		}else{
			[pscustomobject]@{Server=$server;IsFound=$false}
		}
	}		
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 11:51am

I appreciate the patience and constructive criticism. I will keep in mind proper posting and formatting in the forum going forward.
September 9th, 2015 12:29pm

Perfect! Thanks so very much. Looks to have been rather simple, I think I'm over thinking a lot of things in PowerShell but my motto is live and learn. Thanks again for your help.
Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 12:30pm

A starter: http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/17/weekend-scripter-best-practices-for-powershell-scripting-in-shared-environment.aspx

September 9th, 2015 12:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics