Aldo Lanfranconi
asked on
IF statement on a PowerShell Script
Hello, I am working on a script to check an Active Directory User Account status, if it is locked out.
And also I need to write an specific output.
I am Using Get-ADUser command , My script is as follow:
**********************
param
(
[string]$user = ""
)
$command="Get-ADUser "+$user+" -Properties * | select-object LockedOut | findstr True"
$result=invoke-expression $command
if ($result -contains "True")
{
$state="CRITICAL"
$exitcode=2
}
{
$state="OK"
$exitcode=0}
Write-Host $state
exit $exitcode
************************** ******
but I can´t get to work the IF statement, I have added an echo line to debug, and the result is the next:
************************** ******
PS C:\Program Files\NSClient++> .\check_ad_accounts1.ps1 monitoreo
DEBUG: 1+ >>>> .\check_ad_accounts1.ps1 monitoreo
DEBUG: 7+ >>>> $command="Get-ADUser "+$user+" -Properties * | select-object LockedOut | findstr True"
DEBUG: 8+ >>>> $result=invoke-expression $command
DEBUG: 1+ >>>> Get-ADUser monitoreo -Properties * | select-object LockedOut | findstr True
DEBUG: 10+ >>>> echo $result
True
DEBUG: 12+ if ( >>>> $result -contains "True")
DEBUG: 17+ >>>> {
$state="OK"
$exitcode=0
DEBUG: 21+ >>>> Write-Host $state
DEBUG: 22+ >>>> exit $exitcode
PS C:\Program Files\NSClient++>
And also I need to write an specific output.
I am Using Get-ADUser command , My script is as follow:
**********************
param
(
[string]$user = ""
)
$command="Get-ADUser "+$user+" -Properties * | select-object LockedOut | findstr True"
$result=invoke-expression $command
if ($result -contains "True")
{
$state="CRITICAL"
$exitcode=2
}
{
$state="OK"
$exitcode=0}
Write-Host $state
exit $exitcode
**************************
but I can´t get to work the IF statement, I have added an echo line to debug, and the result is the next:
**************************
PS C:\Program Files\NSClient++> .\check_ad_accounts1.ps1 monitoreo
DEBUG: 1+ >>>> .\check_ad_accounts1.ps1 monitoreo
DEBUG: 7+ >>>> $command="Get-ADUser "+$user+" -Properties * | select-object LockedOut | findstr True"
DEBUG: 8+ >>>> $result=invoke-expression $command
DEBUG: 1+ >>>> Get-ADUser monitoreo -Properties * | select-object LockedOut | findstr True
DEBUG: 10+ >>>> echo $result
True
DEBUG: 12+ if ( >>>> $result -contains "True")
DEBUG: 17+ >>>> {
$state="OK"
$exitcode=0
DEBUG: 21+ >>>> Write-Host $state
DEBUG: 22+ >>>> exit $exitcode
PS C:\Program Files\NSClient++>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER