Hello,
I'm new to Powershell. Any assistance in discovering the error or my mistake in the script would be greatly appreciated.
I have a simple PS script that I intend to run and discover all disabled accounts in AD residing in various different OUs.
I run the script with
powershell -ExecutionPolicy ByPass -File script.ps1
Here is the entire script and the error generated is in the bottom of the script
$strfilter = "(&(objectClass=user)(objectCategory=person))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$objSearcher.PropertiesToLoad.Add("cn") | Out-Null
$objSearcher.PropertiesToLoad.Add("member") | Out-Null
$objSearcher.PropertiesToLoad.Add("proxyAddresses") | Out-Null
$objSearcher.PropertiesToLoad.Add("displayName") | Out-Null
$objSearcher.PropertiesToLoad.Add("distinguishedname") | Out-Null
$objSearcher.PropertiesToLoad.Add("useraccountcontrol") | Out-Null
$users = $objSearcher.FindAll()
foreach ($user in $users)
{ "Testing $($user.properties.item(""distinguishedname""))""UAC: $($user.useraccountcontrol)"
if($user.useraccountcontrol -band 2)
{ write-host -foregroundcolor red "`t account is disabled" }
ELSE
{ write-host -foregroundcolor green "`t account is not disabled" }
}
PS C:\> powershell -ExecutePolicy ByPass -file .\gcnaccounts.ps1
Missing expression after unary operator '-'.
At line:1 char:2
+ - <<<< ExecutePolicy ByPass -file .\gcnaccounts.ps1
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
PS C:\>