Get multiple columns from file, then pipe to a command help

I have a text file with server name and IP address on the same line:

server1 10.1.1.1

server2 10.1.1.2

server3 10.1.1.3

Then use $list var ex:

$list = get-content C:\temp\test-ps.txt

I’d like to feed the server name and IP into the Add-DnsServerResourceRecordA command ex:

Add-DnsServerResourceRecordA -name SERVER1 -ipv4address 10.1.1.1 -zonename “symbotic.corp” -AllowUpdateAny -CreatePtr -WhatIf

What is the best way to accomplish this?

 

 

 

 

 

Please read the very first pinned post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did! And please format your code as code using the code tag button (pre). If you post some example data or output from a Powershell console you should format this as code as well.

You read your input file with Get-Content oder Import-CSv and iterate over each single line with a loop using your command line.

I don’t have that command, but you can set variables like this:

get-content test-ps.txt | 
foreach-object {
  $name, $ipv4address = -split $_
  Add-DnsServerResourceRecordA -name $name -ipv4address $ipv4address -zonename symbotic.corp -AllowUpdateAny -CreatePtr -WhatIf
}
$list = get-content C:\temp\test-ps.txt

# Replace any spaces in strings with a comma then convert strings to objects
$myobj = $list -replace '\s+',',' -replace ',$' |
ConvertFrom-Csv -Header Name,IPV4Address

# DNS cmdlet accepts pipeline input via property names (headers)
$myobj |
Add-DnsServerResourceRecordA -zonename "symbotic.corp" -AllowUpdateAny -CreatePtr -WhatIf

This worked! Thank you!

[quote quote=196343]I don’t have that command, but you can set variables like this:

PowerShell
6 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
get-content test-ps.txt |
foreach-object {
$name, $ipv4address = -split $_
Add-DnsServerResourceRecordA -name $name -ipv4address $ipv4address -zonename symbotic.corp -AllowUpdateAny -CreatePtr -WhatIf
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

I like this approach but it got an error that $name, $ipv4address are null objects.

 

Does the file have blank lines?

First, didn’t know you could do variable assignment with split like that, so props to @js on that one.

The parse works and does not error, so it’s erroring there. If the command you are running does require both server and IP, you need to wrap an ‘if’ around it:

$txt = @"
Server123;10.3.4.5
Server124;10.3.4.6
Server125;10.3.4.7
;10.3.4.8
Server126;

"@ -split [environment]::NewLine

'Processing {0} lines' -f $txt.Count

foreach ($line in $txt) {
    $server, $ip = $line -split ';'

    if ($server -and $ip) {
        "Running command with {0} and {1}" -f $server, $ip
    }
    else {
        "Don't have enough to run command with server {0} and ip {1}"  -f $server, $ip
    }
}

Hi,

Do you have reference about how to use @" ? I usually use @() for array.

A here string is actually one multiline string: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6#here-strings

By the way, it’s a myth that @( ) is needed to create arrays. You only need commas.

I wouldn’t call it a myth, it’s strongly-typed if I were to describe it. Powershell does automatic type conversions for simplicity, but wrapping it in @() or defining the type defines and\or forces the conversion to an array.

PS C:\Users\rasim> [array]$myArray = 1,2,3 #type

PS C:\Users\rasim> $myArray = @(1,2,3) #type accelerator

PS C:\Users\rasim> $myArray = 1,2,3 #Powershell type conversion

PS C:\Users\rasim> $myArray.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                                                  
True     True     Object[]                                 System.Array                                                                                                                                                              

Just using the comma operator already makes it an array. These two expressions end up being the same.

$a = 1,2,3
$a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


$a = @(1,2,3)
$a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

By the way, setting the type on the left side permanently casts the variable to that type.

[array]$a = 1,2,3
$a = 1
$a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

The only reason to use @( ), is if the output of something may be a single thing or an array, and you want to make sure it’s an array.

function 1ormany { 
  1
}

$a = @(1ormany)
$a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array