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: 2
      Replies: 1697
      Points: 3,385
      Helping Hand
      Rank: Community Hero

      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: 27
      Replies: 739
      Points: 2,015
      Helping Hand
      Rank: Community Hero

      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
      }
      
    • #196355
      Participant
      Topics: 5
      Replies: 285
      Points: 213
      Helping Hand
      Rank: Participant
      $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
      
    • #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

      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: 27
      Replies: 739
      Points: 2,015
      Helping Hand
      Rank: Community Hero

      Does the file have blank lines?

    • #196673
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      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:

      $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
          }
      }
      
    • #196940
      Participant
      Topics: 3
      Replies: 4
      Points: 66
      Rank: Member

      Hi,

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

    • #196943
      js
      Participant
      Topics: 27
      Replies: 739
      Points: 2,015
      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: 10
      Replies: 1381
      Points: 1,509
      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.

      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                                                                                                                                                              
      
    • #197000
      js
      Participant
      Topics: 27
      Replies: 739
      Points: 2,015
      Helping Hand
      Rank: Community Hero

      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
      
      
      • This reply was modified 2 weeks, 3 days ago by js.
      • This reply was modified 2 weeks, 3 days ago by js.
      • This reply was modified 2 weeks, 3 days ago by js.
Viewing 11 reply threads
  • You must be logged in to reply to this topic.