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.