Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Correct way to construct variable based on other variable's name

$
0
0

Hello!
I have done this topic based on some modifications of this thread. The problem is that I can't understand how to manipulate with selection of the columns as variable when iterating over loop.  My goal is to define multiple variables for 'select' and choose correct one when the adequate topic is selected on loop. It seems that my problem is that I'm not correctly construct the variable for 'select' by using 'invoke-expression'.

The source file is the following:

topic=interface_bridge ruleaction=add name=vlan10
topic=interface_bridge ruleaction=add name=vlan13
topic=interface_ethernet set=1 comment=DMZ
topic=interface_ethernet set=2 comment=Servers

The script is the following:

$file="c:\scripts\mk.txt"
#selections
$interface_ethernet='comment','name','set','switch','master-port'
$interface_bridge='comment','name','protocol-mode'
#topics
$topic_int_bridge='interface_bridge'
$topic_int_ethernet='interface_ethernet'
$topics=@($topic_Int_bridge, $topic_int_ethernet)

foreach ($topic in $topics)
{
$match='^topic='+$topic+'\s+'
$result_File = 'config_'+$topic+'.txt'
#construct selection
$select = '$'+$topic

$interface_ethernet='comment','name','set','switch','master-port'
$interface_bridge='comment','name','protocol-mode'
##automatically select correct topic on loop - This does not work correctly on loop
##the main idea is to constuct select variable based on topic name
$select_topic=invoke-expression ('$'+$topic)
$results = Get-Content mk.txt | Where-Object { $_ -Match $match }| foreach { new-object PSObject -Property $($_ -replace '^' -replace ' ',"`n" | convertfrom-stringdata) } 

$results | select $select_topic
 }

I'm hang on the following: when I run the script it only takes first selection ($interface_bridge) gives the result about first topic (bridge), and for second (ethernet) - only comments (this means that the second selection for interface_ethernet (variable $interface_ethernet) is not used.

comment      name       protocol-mode
-------         ------        ---------------
               vlan10
               vlan13
DMZ
Servers


Expected result should be the following:

comment	    name       protocol-mode
-------         ------          ---------------
               vlan10
               vlan13
comment	  set	        name	 switch   master-port       
-------       ----       -----------    -------   -------------
DMZ            1 
Servers        2     



Question:

How to modify the script that the result is printed for each topic with adequate selection:

1) when looping over topics, when the topic interface_ethernet is processed select and print results for variable $interface_ethernet

(selected items 'comment','name','set','switch','master-port')

2) when looping over topics, when the topic interface_bridge is processed select and print results for variable $interface_bridge

(selected items 'comment','name','protocol-mode')

3) etc...

Each topic should be on separated iteration block because for each will be generated csv file with each own header. 

Thanks.

Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>