【发布时间】:2012-06-18 06:03:20
【问题描述】:
我正在使用 Powershell 脚本写入文本文件。一位客户向我展示了这个 Powershell 脚本,用于替换我以前使用的 excel 宏...
$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
}
它工作得很好,但现在我想在它的基础上添加额外的变量。在一个完美的世界里,我希望它从一个 excel 电子表格中读取,抓取每一行数据,每一列都被定义为一个变量。现在使用另一个文本文件也很好。这是我开始的(它不起作用),但你可以看到我要去哪里......
$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
}
当然不行。从本质上讲,如果我可以将上述每个选项都定义为 Excel 电子表格中的变量,例如 $community、$interval、$active 等,我会很高兴。
对此的任何帮助将不胜感激。如果有人可以向我展示如何使用 Excel 电子表格,将每一列定义为变量,并用变量编写上述文本,那就太好了!!!。
谢谢,
smt1228@gmail.com
一个例子如下...
Excel 数据:(用“|”分隔的列
IP | String | Group
10.1.2.3 | Public | Payless
期望的输出:
<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>
加法:
从 CSV 中提取 IP、String、Group 的数据,其中 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
写入 .txt 文件为
IP = 10.1.2.3.
String = public
Group = Group1
并在 CSV 中查找每一行
【问题讨论】:
标签: excel powershell text