none
Can't get correct output from function RRS feed

  • Question

  • Hello. I'm still a newbie in writing functions. Please help me to get where I am wrong.

    I'm trying to write a the function that will be switching between domain suffixes to get correct one. Function get computername at input, switch suffixes and give correct fqdn at output. But it doesn't work as inteded.

    Function FullServerName ($name)
    {
        $outname = $name
        $ses = New-PSSession -ComputerName $outname -Credential $cred
        if ($ses -ne $null) {Remove-PSSession -Session $ses; Write-Output "Will use $outname to connecnt";return;}
        else {Write-Output "Can't connect to "$outname}
    
        $outname = $name+".domain1.com"
        $ses = New-PSSession -ComputerName $outname -Credential $cred
        if ($ses -ne $null) {Remove-PSSession -Session $ses; Write-Output "Will use $outname to connecnt";return;}
        else {Write-Output "Can't connect to "$outname}
    
        $outname = $name+".domain2.com"
        $ses = New-PSSession -ComputerName $outname -Credential $cred
        if ($ses -ne $null) {Remove-PSSession -Session $ses; Write-Output "Will use $outname to connecnt";return;}
        else {Write-Output "Can't connect to "$outname;}
        $outname
    }
    
    $h = FullServerName($server)
    
    

    Tuesday, February 6, 2018 4:00 PM

Answers

  • To test connectivity we would use:'

    Test-WsMAn <fqdn>

    This can be modified to  test any protocol/port or authentication type.

    help Test-WsMan -Online 


    \_(ツ)_/

    Tuesday, February 6, 2018 8:34 PM

All replies

  • What errors or issues are you getting?

    I don't understand why you are using Enter-PSSession to test the connection to the computer. Just use:

    Test-Connection -ComputerName $outname -Count 1

    If it returns true then the computer name is correct.

    Tuesday, February 6, 2018 4:38 PM
  • Test-Connection is not enough. 

    Computer can ping server succesfully by only name, but can't connect to it because Kerberos requires FQDN.

    Tuesday, February 6, 2018 4:43 PM
  • Then use Test-Connection using the FQDNs like the last two blocks in your function. If you get a response then you know the name and domain is correct.

    $outname = $name+".domain2.com"
    Test-Connection -ComputerName $outname -Count 1

    What is the part that "doesn't work as intended"?

    You could just use:

    ([System.Net.DNS]::GetHostByName($name)).HostName

    Which will return the FQDN in one go so there is no need to check :)


    Tuesday, February 6, 2018 4:55 PM
  • Unfortunately Enter-PSSession is more reliable way because in my environment success ping doesn't mean success connection to any other ports.

    And how can i get hostname when server may be in another domain so my DNS can't find it?

    Function works strange because, as I think, I didn't put return in correct places. I want function to stop executing and return fqdn at first success Enter-PSSession.

    Tuesday, February 6, 2018 8:19 PM
  • To test connectivity we would use:'

    Test-WsMAn <fqdn>

    This can be modified to  test any protocol/port or authentication type.

    help Test-WsMan -Online 


    \_(ツ)_/

    Tuesday, February 6, 2018 8:34 PM