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

Welcome Forums General PowerShell Q&A Get multiple columns from file, then pipe to a command help

Viewing 11 reply threads
  • Author
    Posts
    • #196319
      Participant
      Topics: 2
      Replies: 2
      Points: 35
      Rank: Member

      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?

       

       

       

       

       

    • #196328
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      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.

    • #196343
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

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

    • #196355
      Participant
      Topics: 5
      Replies: 328
      Points: 521
      Helping Hand
      Rank: Major Contributor
    • #196532
      Participant
      Topics: 2
      Replies: 2
      Points: 35
      Rank: Member

      This worked!  Thank you!

    • #196535
      Participant
      Topics: 2
      Replies: 2
      Points: 35
      Rank: Member

      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

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

       

    • #196541
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      Does the file have blank lines?

    • #196673
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      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:

    • #196940
      Participant
      Topics: 8
      Replies: 9
      Points: 158
      Rank: Participant

      Hi,

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

    • #196943
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

      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.

    • #196994
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      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.

    • #197000
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,687
      Helping Hand
      Rank: Community Hero

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

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

      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.

      • This reply was modified 8 months, 2 weeks ago by js.
      • This reply was modified 8 months, 2 weeks ago by js.
      • This reply was modified 8 months, 2 weeks ago by js.
Viewing 11 reply threads
  • The topic ‘Get multiple columns from file, then pipe to a command help’ is closed to new replies.