GET Local Admin with catch


hi guys,

as can see trying remove local admin group catching errors:

from reason not show me error, skip that:

$computerlist = get-adcomputer -searchbase "dc=xxx,dc=xxx,dc=com" -filter {enabled -eq $true -and name -like "d*" }   foreach ($machine in $computerlist) {       $computername = $machine.name     try     {  		$computerlist = [adsi]"winnt://$computername" 		$computerlist.delete("user", "merope13")         write-host "local admin $computername has been removed successfully" -backgroundcolor green     }      catch      {     $errormessage = $_.exception.message     $result = "($computername) ($errormessage)"     }     if ($errormessage -like '*exception calling delete*')     {      "$computername + user name not found"     }      elseif ($errormessage -like "*testing connection computer*")     {     "$computername + isn't avalible"      }     }   	


my website:www.pelegit.co.il mcitp /mcsa 2012



it cannot done way.

start searching examples of how t use "foreach" loop.  cannot change loop control variable inside of loop.  basic programming in language. when over-write variable loop no longer work correctly.

start small.  don't use try/catch until understand how use basic constructs.  use 1 syntax @ time until understand it.


\_(ツ)_/



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

2008 Windows Deployment Server Properties Error

Can no longer user MS Update - Files required to use Microsoft Update are no longer registered

How do a find data in one file, search for it in another file and if not found, write a custom message to another file