Author Posts

May 20, 2017 at 9:09 am

Hi Guys,
I have almost finished by script to remove un-authorized accounts from a server group, so if an account is found it removes it and outputs the result to a website perfectly. However when no accounts are found as per the criteria I have asked it to output "Nothing to remove" on the webpage. The thing is that when no accounts are found the output is written multiple times rather than just one line.
Here is my main part of the code

  foreach ($server in $serverlist) {
$objGroup = [ADSI]("WinNT://$server/Administrators")
 $objGroupMembers = $objGroup.psbase.Invoke("Members") | foreach {$_."GetType".Invoke().InvokeMember("Name", 'GetProperty',$null, $_, $null)}
 

        ForEach($Member in $objGroupMembers)
        {
            $adminaccount = If($Member -like "adm*")
            {
             
                  
             $objRemoveGroup = [ADSI]("WinNT://contoso/$($Member)")
             $objGroup.Invoke("Remove",$objRemoveGroup.PSBase.Path)
             $output = write-output "The account $($member.toupper()) was removed from $($server)"
             $adm+= $output
             $admserver+= $server
             
           

             } 

             if(!$adminaccount){
             
             $output2 = ConvertTo-Html –body "Nothing was done!" | Out-File -Append $report

             } 
             }
             $output1 = $adm | Select-Object @{Name='Name';Expression={$_}},@{Name='Names';Expression={"Wrong account"}}
             $output1 | ConvertTo-Html –body "The below ADM's have been removed" | Out-File -Append $report
             


How can I get if nothing is found just to output "Nothing was done" just one time?

-A

May 20, 2017 at 10:04 am

oK I figured it out!

 $serverList = (Get-Content C:\powershell\Scripts\GetLocalAdmin\ME\MEServers.csv) -notmatch 'DNSHostName'

foreach ($server in $serverlist) {
$objGroup = [ADSI]("WinNT://$server/Administrators")
 $objGroupMembers = $objGroup.psbase.Invoke("Members") | foreach {$_."GetType".Invoke().InvokeMember("Name", 'GetProperty',$null, $_, $null)}
 

        ForEach($Member in $objGroupMembers)
        {
           
            
            If($Member -like "adm*")
            {
             
                  
             $objRemoveGroup = [ADSI]("WinNT://contoso/$($Member)")
             $objGroup.Invoke("Remove",$objRemoveGroup.PSBase.Path)
             $output = write-output "The account $($member.toupper()) was removed from $($server)"
             $adm+= $output
             $admserver+= $server
             
             }  

 
         if($adm)
         {
          $output1 = $adm | Select-Object @{Name='Name';Expression={$_}},@{Name='Names';Expression={"Wrong account"}}
          $output1 | ConvertTo-Html –body "The below ADM's have been removed" | Out-File -Append $report

            
            }

        else
        {

        $output2 | ConvertTo-Html –body "The below ADM's have been removed" | Out-File -Append $report
        $output2 = ConvertTo-Html –body "Nothing was found!" | Out-File -Append $report
        
        }