0

I have a simple script to resolve hostname into IP address and then create from it key-value pair to use in jenkins:

$ips = Resolve-DnsName -type a HOSTNAME | FT IPAddress -HideTableHeaders
echo $ips
>> 192.168.3.1

But when I want to use this output in key-value pair, I receve following:

$keyvalue = $(echo 'ipaddr:'"$ips")

PS C:\Users\vegas.s> echo $keyvalue
ipaddr:
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData    Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

I want to receive following:

ipaddr:192.168.3.1

What am I doing wrong?

4
  • 1
    You're storing the result of Format-Table. You should only use that when displaying stuff, and only as the very last element of a pipeline, not as an intermediate result. Leave that out and use $ips.IPAddress ($($ips.IPAddress) within a string) to get the propery value without adornment. Nov 6, 2018 at 12:52
  • @JeroenMostert unfortunately, $ips.IPAddress didnt return anything - i.imgur.com/sOFTg31.png Nov 6, 2018 at 13:00
  • 1
    Of course -- that's because you still have the Format-Table in there. Try $ips = (Resolve-DnsName -type a www.google.com).IPAddress. Nov 6, 2018 at 13:01
  • @JeroenMostert - youre right! When I use $ips = Resolve-DnsName -type a HOSTNAME and echo $ips.IPAddress - it returns correct value! You can arrange it as an answer. Nov 6, 2018 at 13:03

1 Answer 1

1

try this:

#join the string and ip separated by colon
$keyvalue = 'ipaddr',$ips -join ':'

PS C:\Users\vegas.s> echo $keyvalue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.