If else not working please help

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 weeks, 1 day ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #54040
    Profile photo of Sven Guli
    Sven Guli
    Participant

    Hi, I am not a PowerShell expert by any means. Using google I was able to construct a script but it's not quite working as intended. It's supposed to create a local admin account on a list of servers from a text file. As it is creating the file it's supposed to check if the server is online and if so, check to see if the account already exists.

    Well, the creation part works but the correct error message is not returned when the account does exist. Can someone please help?
    Thanks.

    $UserName = "ADMIN2"
    $Password = Read-Host "What password would you like to use?" -AsSecureString
    $Message1 = "Account already exists"
    $Message2 = "Server Unreachable"
    $Message3 = "ADMIN2 Created"
    # Loop through the list of servers
    Get-Content c:\AI\Serversmini2.txt | ForEach-Object {
    $ServerName = $_
    $User = $null
    $exaccount=$null
    # Connect to the server. Connection is used to create users.
    $Server = [ADSI]"WinNT://$ServerName"

    # Check for connectivity to server
    if (test-connection $ServerName -Count 1) {
    # Connect to the administrators group so we can add members.
    $AdminGroup = [ADSI]"WinNT://$ServerName/Administrators, group"
    $exaccount = (get-ciminstance win32_useraccount -filter "localaccount='true' and name='ADMIN2'")
    if ($exaccount -eq $null) {
    # Create the user
    $User = $Server.Create('User', $UserName)
    # Set password
    $User.SetPassword("$Password")
    # Set Password to never expire
    $flag = $User.UserFlags = 65536
    # Save the changes
    $User.SetInfo()
    # Add the new user to the admin group
    $AdminGroup.Add($User.Path)
    $Server, $Message3 | Out-File -Append C:\AI\AccountCreation.txt
    }
    Else{
    $Server, $Message1 | Out-File -Append C:\AI\AccountCreation.txt
    }
    }
    Else{
    $Server, $Message2 | Out-File -Append C:\AI\AccountCreation.txt
    }
    }

    #54044
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There are a lot of examples in the community from managing local accounts\groups. Take a look at this script: https://4sysops.com/archives/powershell-script-to-create-local-user-accounts-or-local-groups/. Minimally, look at the script logic. In your script you are trying to connect to the remote computer with ADSI before you use Test-Connection. Also, your Test-Connection does not contain a -Quiet switch, so it is not returning a boolean (true\false) value:

    PS C:\Users\Rob> Test-Connection -ComputerName "Server123" -Count 2
    Test-Connection : Testing connection to computer 'Server123' failed: No such host is known
    At line:1 char:1
    + Test-Connection -ComputerName "Server123" -Count 2
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (Server123:String) [Test-Connection], PingExce 
       ption
        + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectio 
       nCommand
     
    Test-Connection : Testing connection to computer 'Server123' failed: No such host is known
    At line:1 char:1
    + Test-Connection -ComputerName "Server123" -Count 2
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (Server123:String) [Test-Connection], PingExce 
       ption
        + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectio 
       nCommand
     
    

    versus getting boolean:

    PS C:\Users\Rob> Test-Connection -ComputerName "Server123" -Count 2 -Quiet
    False
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.