0

So I have a powershell script that I am trying to get up and running. Most of it works but what I am trying to do to make it as easy as possible to run periodically is to have it reference a list of numbers (IPs) in a text file, and then create a new variable for each line of the text file. This part does work using the following.

$iplist = get-content c:\powershell\ips.txt | where-object { $_.Trim() -ne '' }
$startnum = 0

foreach($line in $iplist){    
    $startnum++
    new-variable -name "ip$startnum" -Value $line -Force
}

This is great, but later on in the script it has to use the number stored in each of those dynamically created variables in two other parts of the script. One part is where is reverses the IP address and then stores its reversed variant in another variable, and on another part it uses that reversed IP address as part of a lookup using [System.Net.DNS].

So basically, using that first snippet of code the script does the following, using a text file called ips.txt with 1.2.3.4 on the first line and 5.6.7.8 on the second line

$ip1 = 1.2.3.4 $ip2 = 5.6.7.8

Then I want to take those $ip1 and $ip2 values and reverse them. I know the reverse part works cause it works with a static input, but when i try it with variables it doesn't work, below is a snippet of the code i'm trying to do this with. Its basically an updated version of the code snippet from above.

$iplist = get-content c:\powershell\ips.txt | where-object { $_.Trim() -ne '' }
$startnum = 0

foreach($line in $iplist){    
    $startnum++
    new-variable -name "ip$startnum" -Value $line -Force
    new-variable -name "ipParts$startnum" -Value "$ip$startnum".Split('.')
    [array]::Reverse($ipParts$startnum)
    $ipparts$startnum = [string]::join('.',$ipParts$startnum)
}

When I run that though I get the following errors

   At line:8 char:30
+     [array]::Reverse($ipParts$startnum)
+                              ~
Missing ')' in method call.
At line:8 char:30
+     [array]::Reverse($ipParts$startnum)
+                              ~~~~~~~~~
Unexpected token '$startnum' in expression or statement.
At line:4 char:26
+ foreach($line in $iplist){
+                          ~
Missing closing '}' in statement block or type definition.
At line:8 char:39
+     [array]::Reverse($ipParts$startnum)
+                                       ~
Unexpected token ')' in expression or statement.
At line:9 char:53
+     $ipparts.$startnum = [string]::join('.',$ipParts$startnum)
+                                                     ~
Missing ')' in method call.
At line:9 char:53
+     $ipparts.$startnum = [string]::join('.',$ipParts$startnum)
+                                                     ~~~~~~~~~
Unexpected token '$startnum' in expression or statement.
At line:9 char:62
+     $ipparts.$startnum = [string]::join('.',$ipParts$startnum)
+                                                              ~
Unexpected token ')' in expression or statement.
At line:10 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

There are two parts to this, but i'm hoping if I can figure out this first part then the second part when I use the final value that is supposed to be stored in $ipParts$startnum to do the lookups will be easier.

EDIT

So I had an idea and change the script to this

$iplist = get-content c:\powershell\ips.txt | where-object { $_.Trim() -ne '' }
$startnum = 0

foreach($line in $iplist){    
    $startnum++
    new-variable -name "ip$startnum" -Value $line -Force -ErrorAction SilentlyContinue
    new-variable -name "ipParts$startnum" -Value "$ip$startnum".Split('.')
    $iptemp = get-variable -name "ipparts$startnum"
    [array]::Reverse("$iptemp")
    $iptemp = [string]::join('.',"$iptemp")
    set-variable -name "ipParts$startnum" -Value "$iptemp"
    }

    write-host "iplist is $iplist......ip1 is $ip1....ip2 is $ip2....ipparts1 is $ipparts1.......ipparts2 is $ipparts2....iptemp value is $iptemp"

Basically use set-variable to modify it as a string with other variable names in it, kind of works, but when i did write-host on the last part to make sure its actually writing the proper values to the variables, on $ipparts1, $ipparts2, and $iptemp, i have the following value "System.Management.Automation.PSVariable" I'm not entirely sure what that means.

mrlizard
  • 1
  • 2
  • 1
    `$ipParts$startnum` won't parse correctly when it is not surrounded in quotes. To use variables in the name of a variable, it's going to be easier to just use `Get-Variable ipParts$startnum -ValueOnly` – AdminOfThings Dec 16 '20 at 22:07
  • Enclosing in quotes actually got rid of alot of the errors, except for this part "$ipparts$startnum" = [string]::join('.',"$ipParts$startnum") that gave me this error The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. – mrlizard Dec 16 '20 at 22:12
  • 2
    Why don't you use a single hashtable instead of creating entire new variables for each IP? Would make this whole thing much simpler. – zett42 Dec 16 '20 at 22:19
  • To be honest i really don't have much experience in scripting, so i'm not sure what a hash table is, i have kind of an idea, but not really sure. most of what i have done has been super basic. could that be used with the foreach loop i have at the end of the script that does the DNS lookups? let me see if i can find a way to post what that is. – mrlizard Dec 16 '20 at 22:33
  • @MrLizard Are you aware of the concept of arrays? It is very uncommon to dynamically create variable names. You rather use arrays or hashtables (like zett42 suggested) or any container type in general. – stackprotector Dec 17 '20 at 06:52
  • Does this answer your question: [how to programmatically iterate through variable names](https://stackoverflow.com/a/65250334/1701026)? use something like: `$ip = @{}; foreach ($line in $iplist) { $ip[$startnum++] = $line }` instead. – iRon Dec 17 '20 at 09:06

0 Answers0