locked
Removing White Space from an Array RRS feed

  • Question

  • Hello all,

     I have created an array to read an input of IP addresses.

    $IPaddr = @()
    Do 
    { 
        $IPaddr += Read-Host 'Please type a server IP you want to add to the Domain.'
    }
    Until ($IPaddr -eq '')

    IT works with one exception,  at the very end it adds to blank inputs to the variable. so I get 2 extra blank lines of input. see below.

    PS C:\Users\user> 
    $IPaddr = @()
    Do 
    { 
        $IPaddr += Read-Host 'Please type a server IP you want to add to the Domain.'
    }
    Until ($IPaddr -eq '')
    
    Please type a server IP you want to add to the Domain.: 123
    Please type a server IP you want to add to the Domain.: 123
    Please type a server IP you want to add to the Domain.: 123
    Please type a server IP you want to add to the Domain.: 123
    Please type a server IP you want to add to the Domain.: 
    Please type a server IP you want to add to the Domain.: 
    
    PS C:\Users\user> $IPaddr
    123
    123
    123
    123
    
    
    
    PS C:\Users\user>

    How would I get rid of that white space?  Thank you.

    

    • Moved by Bill_Stewart Monday, May 7, 2018 10:03 PM Help vampire
    Tuesday, March 6, 2018 7:33 PM

All replies

  • You might use a little more professional way of getting a desired input like mandatory parameter of a function.

    function Get-IPAddress {
        param(
            [Parameter(Position=0, Mandatory=$true, HelpMessage="Please enter a valid IP-Address!")]
            [ValidateScript({$_ -match [IPAddress]$_ })]
            [IPAddress]
            $IP
        )
        Write-Host "This is the given IP-Address: '$($IP)'"
    }
    
    Get-IPAddress 

    Even better would be to present a choise of possible options/IPs to the user to choose from. This way you'd avoid a lot of possible false input.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, March 6, 2018 7:55 PM