excel - 从 Excel 中的数据通过 PowerShell 添加防火墙规则
问题描述
我需要从 excel 文件的长列表中创建新的防火墙规则。为此,我将使用 PowerShell 和 ImportExcel 模块。考虑到我是 Windows 和 PowerShell 中的菜鸟,我需要帮助才能实际执行此操作。
excel 的第一列代表 IpAndPorts,我需要阻止并添加为防火墙规则。我需要从“第 2 行”获取数据,直到 excel 数据结束。
下面是excel的例子:
Excel 行 | IpAndPorts | 协议 |
---|---|---|
2 | 35.180.0.0/16 | TCP |
3 | 12.13.14.1/8883,433 | TCP |
4 | 1.2.3.4/80 | HTTP |
我还需要将 New-NetFirewallRule 命令中的 -DisplayName 和 -Name 设置为动态的,以便稍后找到防火墙规则(一些代码示例)。
我将用于创建防火墙规则的 PowerShell 中的命令:
New-NetFirewallRule -DisplayName "Block WiFi {Excel-Row} " -Name "Block Wifi {Excel-Row} " -Direction Inbound -InterfaceType Wireless -Action Block -RemoteAddress {Part before / IpAndPorts} -LocalPort {Part after / IpAndPorts} -协议{协议}
从 Excel 获取第 3 行数据后的命令示例:
New-NetFirewallRule -DisplayName "Block WiFi 3 " -Name "Block Wifi 3 " -Direction Inbound -InterfaceType Wireless -Action Block -RemoteAddress 12.13.14.1 -LocalPort 8883,433 -Protocol TCP
更新: 这是我设法编写的代码:
# ---------------------------------------
# Here we should get data from excel
# and prepare variables
$FirewallList = Import-Excel C:\Temp\list.xlsx
$All = $FirewallList.IpAndPorts
$AllProtocols = $FirewallList.Protocol
# ---------------------------------------
# Here it should be number of excel row,
# starting from 2 and incementing until last data in row
# Don't know how to do that
$Index = "1"
# ---------------------------------------
# Split IpAndPorts data to get IP and PORT
$Ip = $All.Split("/")[0]
$Port = $All.Split("/")[1]
# ---------------------------------------
# Here we should populate parameters
#$Params = @{
# "DisplayName" = 'Block-WiFi-' + $Index
# "Name" = 'Block-WiFi-' + $Index
# "Direction" = 'Inbound'
# "InterfaceType" = 'Wireless'
# "Action" = 'Block'
# "RemoteAddress" = $Ip
# "LocalPort" = $Port
# "Protocol" = $AllProtocols
#}
# Here we should start the command
# New-NetFirewallRule @Params
# ---------------------------------------
# Test how the command will be assembled
# based on data from excel
$Test = New-Object PSObject -Property @{
DisplayName = "Block-WiFi-" + $Index
Name = "Block-WiFi-" + $Index
Direction = 'Inbound'
InterfaceType = 'Wireless'
Action = 'Block'
RemoteAddress = $Ip
LocalPort = $Port
Protocol = $AllProtocols
}
Write-Host "New-NetFirewallRule
-DisplayName $($Test.DisplayName)
-Name $($Test.Name)
-Direction $($Test.Direction)
-InterfaceType $($Test.InterfaceType)
-Action $($Test.Action)
-RemoteAddress $($Test.RemoteAddress)
-LocalPort $($Test.LocalPort)
-Protocol $($Test.Protocol)
"
# Test results. Just printing
Write-Host "------------- PRINT FROM EXCEL ----------------"
$All
$AllProtocols
Write-Host "---------------------------------------------"
$Ip
$Port
如何制作循环以从 excel 中获取每个值,添加到 $Params 和午餐命令?
提前致谢..!!!
解决方案
当您加载 Excel 文件时
$FirewallList = Import-Excel C:\Temp\list.xlsx
$All = $FirewallList.IpAndPorts
$AllProtocols = $FirewallList.Protocol
尝试做$FirewallList.Count
- 这就是行数。
然后您可以通过更改(从 0 开始)来访问每一行,与上面的其他变量相同i
。$FirewallList[i]
对于您的示例:
$FirewallList.Count
3
$FirewallList[0]
IpAndPorts Protocol
---------- --------
35.180.0.0/16 TCP
您还可以使用 foreach 循环遍历所有元素。
推荐阅读
- javascript - 当对象的键是长字符串时,为什么我看不到对象的所有键和值?
- html - ng-include 中的表达式 | 带有字符串的 src
- javascript - 将 pandas 数据框作为表格数组传递给 javascript
- python - 当某些参数未知时,如何获得带有 sympy 的分段函数?
- reactjs - 在 React 中分配 onChange 事件时如何更新输入值?
- java - 自定义 hyperjaxb3 生成实体的表名?
- react-native - --indexed-ram-bundle 对 React Native 捆绑的意义
- c# - xamarin 表单中的自动完成功能
- powerbi - 是否真的需要 onpermise powerbi 服务器的网关
- android - 应用程序在启动时崩溃时如何阅读谷歌播放堆栈跟踪