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.
September 8th, 2015 4:28pm
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."}}
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.
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}
}
}
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.
September 9th, 2015 12:30pm
September 9th, 2015 12:32pm