-1

I need you help... i am writing a powershell script which will add a computer to a domain. i have done this using the add-computer but my main problem is that i want to check if the join was done successful or not. If was not successful i want to try again until the action done. this is the script:

$j = add-computer -domainname mydomain -credential mydomain\
While ( $j -ne 0){
 $j = add-computer -domainname mydomain -credential mydomain\
}

If the join done or not done the script is running and it never ends.

I tried to do it with the DO... Until:

    do { 
    $j = add-computer -domainname mydomain -credential mydomain\ }
until ($j -eq o)

but i had the same problem...

Can you help me please?

M.agio
  • 1
  • 4
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Joe C Jun 18 '17 at 18:14

1 Answers1

1

You could specify the parameter "-PassThru" for the command "Add-Computer". Based on your input the command would look like this:

$j = Add-Computer -DomainName mydomain -Credential mydomain\ -PassThru

"$j" now contains the information, if the join was successful. You can get the status with:

$j.HasSucceeded

It will give you "$True" on success and "$False" on error. With that information you could form your IF-clause as you like:

IF ( $j.HasSucceeded -eq $false ) { ...

EDIT: A simple example based on your input:

Do {

    Try {    
        $j = Add-Computer -DomainName mydomain -Credential mydomain\test -PassThru -ErrorAction Stop
    }

    Catch {
        $Error[0].Exception
    }

} While ( $j.HasSucceeded -ne $true )

Kind regards

Walhalla
  • 396
  • 2
  • 5
  • 16
  • Thank you for you help. i want to use the while or until because in the case that the join will not be done successful i would like, the script asks from the user to re enter the credential. If i use the if, the check will be done only once. – M.agio Jun 18 '17 at 20:05
  • Yes, but you can try to create your loop with the mentioned clause. e.g. `While ( $j.HasSucceeded -ne $true ) { ...` – Walhalla Jun 18 '17 at 20:20
  • Do you need further assistance? I did edit my answer with a simple example, which you could take as base ... – Walhalla Jun 19 '17 at 15:13
  • Thank you again Walhalla for your help!!! i really appreciate it!!!! Your example is exactly what i am looking for. Thank you again for your help! – M.agio Jun 20 '17 at 20:55
  • Glad to hear, that I was able to support you. I would appreciate, if you would mark my posting as answer. Have a great day. – Walhalla Jun 21 '17 at 18:10