script with two conditions

Welcome Forums General PowerShell Q&A script with two conditions

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
4 years, 1 month ago.

  • Author
    Posts
  • #21153

    Participant
    Points: 0
    Rank: Member

    hi friends

    I am new to PowerShell and i am studying hard since I've found it excellent to automate administrative tasks in my network.

    I am trying to write an script which checks if ADDS service (which its service name is NTDS) exist & also if it is running, then my command which creates a set of objects in AD be executed (new-ADobject .......)

    I am familiar with get-service Cmdlet & also with basic "if statement", which is
    if (my condition) {
    do stuff
    }

    but I don't know how to combine these things to make the final code.

    may someone please write me the code, so that a begin to study about the different parts of the final code?

    thanks a lot

  • #21155

    Participant
    Points: 60
    Rank: Member

    Hey Maria,

    You could try something like this :

    $service = Get-Service -Name NTDS
    If (($service) -and ($service.status -eq 'Running')) 
    {
      Write-Output -InputObject 'NTDS Service exists and is running'
    }
    

    The first part of the condition validates that the value in the $service variable is not null (i.e. the service has been found matching the name), and the second part confirms the service itself is running.

  • #21157

    Participant
    Points: 0
    Rank: Member

    Hi tim.
    thank you very much
    regards

The topic ‘script with two conditions’ is closed to new replies.