简体   繁体   中英

Powershell Script loop output to txt file

I am using a Powershell script to write to a text file. A client showed me this Powershell script to use to replace a excel macro I used to use...

$computers= gc "C:\Users\Powershell\DeviceList.txt"
foreach ($computername in $computers)
{
write-output "<$computername>
      active = yes
      group = 
      interval = 5min
      name = $computername
      host = $computername
      community = 
      version = 1
      timeout = 0
      retries = default
      port = 161
      qos_source = 1
</$computername>"  | Out-File -filepath "C:\Users\Powershell\Cisco_Mon.txt" -append
}

It works great but now I wanted to build on it to add additional variables. In a perfect world I would like it to read from an excel spreadsheed grabbing each rowof data and each column being defined as a variable. For now using another text file is fine as well. Here is what I started with (it doesnt work) but you can see where I am going with it...

$computers= gc "C:\Users\Powershell\devicelist.txt"
$groups= gc "C:\Users\Powershell\grouplist.txt"
foreach ($computername in $computers) + ($groupname in $groups)
{
write-output "<$computername>
      active = yes
      group = $groupname
      interval = 5min
      name = $computername
      host = $computername
      community = 
      version = 1
      timeout = 0
      retries = default
      port = 161
      qos_source = 1
</$computername>"  | Out-File -filepath "C:\Users\Powershell\Cisco_Mon.txt" -append
}

Of course it is not working. Essentially I would LOVE it if I could define each of the above options into a variable from an excel spreadsheet, such as $community, $interval, $active, etc.

Any help with this would be very much appreaciated. If someone could show me how to use an excel spreadsheet, have each column defined as a variable, and write the above text with the variables, that would be GREAT!!!.

Thanks,

smt1228@gmail.com

An Example of this would be the following...

Excel Data: (Colums seperated with "|"

IP | String | Group
10.1.2.3 | Public | Payless

Desired Output:

<10.1.2.3>
      active = yes
      group = Payless
      interval = 5min
      name = 10.1.2.3   
      host = 10.1.2.3   
      community = 
      version = 1
      timeout = 0
      retries = default
      port = 161
      qos_source = 1
</10.1.2.3>

Addition:

Pulling data from CSV for IP, String, Group where data is as follows in CSV...

10.1.2.3,public,group1
10.2.2.3,default,group2
10.3.2.3,public,group3
10.4.2.3,default,group4

to be writting into a .txt file as

IP = 10.1.2.3.
String = public
Group = Group1

and look for each line in the CSV

Ok, new answer now. The easiest way would be to save your Excel document as CSV so that it looks like this (ie very similar to how you presented your data above):

IP,String,Group
10.1.2.3,Public,Payless

You can still open that in Excel, too (and you avoid having to use the Office interop to try parsing out the values).

PowerShell can parse CSV just fine: with Import-Csv :

Import-Csv grouplist.csv | ForEach-Object {
  "<{0}>
    active = yes
    group = {1}
    interval = 5min
    name = 10.1.2.3   
    host = 10.1.2.3   
    community = 
    version = 1
    timeout = 0
    retries = default
    port = 161
    qos_source = 1
  </{0}>" -f $_.IP, $_.Group
}

I'm using a format string here where {0} , etc. are placeholders. -f then is the format operator which takes a format string on the left and arguments for the placeholders on the right. You can also see that you can access the individual columns by their name, thanks to Import-Csv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM