Trouble with ElseIF

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Chris Chris 5 days, 6 hours ago.

  • Author
    Posts
  • #70904
    Profile photo of Chris
    Chris
    Participant

    Hi Guys,

    I'm trying to make a little tool, but at the moment i'm struggling to get my ElseIF working. I can't see the flaw in my logic,

    it currently looks like this:

    Import-Module ActiveDirectory
    
    $StudentNo = CustomInputBox "Please Give the Student Number"
    
    $status = Get-ADUser -Filter "SAMAccountName -like '$StudentNo'" | Select-Object Enabled
    
    if ("$status -Match '@{Enabled=True}'") {
    
       Show-MessageBox "Account is Enabled"
       } ElseIf ("$status -Match '@{Enabled=False}'") {
    
       Show-MessageBox "Account is Disabled" 
       }

    I have it using Windows Forms, whatever i type in (in this case it takes a student number which is the SAMAccountName)
    it shows as enabled, even if the status, when checked seperately is @{Enabled=False}.

    If you want the whole code, i can provide, but this seems to be the problem area?
    Thank you.

  • #70924
    Profile photo of Chris Cass
    Chris Cass
    Participant

    Maybe try

    if ($status.enabled -eq 'True') {
    
       Show-MessageBox "Account is Enabled"
       } Else {
    
       Show-MessageBox "Account is Disabled" 
       }
    
  • #70940
    Profile photo of Chris Bakker
    Chris Bakker
    Participant

    As it is (probably, cant check sorry) a boolean:

    if ($status.enabled) {

    Show-MessageBox "Account is Enabled"
    } Else {

    Show-MessageBox "Account is Disabled"
    }

  • #70943
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    I think the problem is with your code. Can you send us the code? Here is what I have.

    $StudentNo = Read-Host "Please give the student number"
    $status = Get-ADUser -Filter {SamAccountName -like $StudentNo} | Select-Object enabled 
    if ($Status.enabled -eq "True"){
        Write-Output "Account is eabled"
    }else{
        Write-Output "Account is disabled"
    
    
  • #70970
    Profile photo of Chris
    Chris
    Participant

    Thank's for all your help guys,

    I got it working using the following

    if ($status.Enabled -eq $true) {
    
       Show-MessageBox "Account is Enabled"
       } ElseIf ($status.Enabled -eq $false) {
    
       Show-MessageBox "Account is Disabled" 
       }

    Again, thank you!

You must be logged in to reply to this topic.