Powershell 如何向命令添加未知数量的变量
我有一个脚本,我正试图修改它,它使用Invoke rest方法通过powershell向防火墙添加对象 当前脚本具有以下代码Powershell 如何向命令添加未知数量的变量,powershell,Powershell,我有一个脚本,我正试图修改它,它使用Invoke rest方法通过powershell向防火墙添加对象 当前脚本具有以下代码 #Import CSV and set variables $csv = import-csv C:\Powershell\groups.csv # RESTful API Call $csv | ForEach-Object { $Name = $_.name $Member1 =$_.member1
#Import CSV and set variables
$csv = import-csv C:\Powershell\groups.csv
# RESTful API Call
$csv | ForEach-Object {
$Name = $_.name
$Member1 =$_.member1
$Member2 =$_.member2
Invoke-RestMethod -Uri https://172.16.16.16:4444/webconsole/APIController?reqxml=<Request><Login><Username>admin</Username><Password>password</Password></Login><Set%20operation=%27add%27><IPHostGroup><Name>$Name</Name><IPFamily>IPv4</IPFamily><HostList><Host>$Member1</Host><Host>$Member2</Host></IPHostGroup></Set></Request>
}
等等
我的问题是,在实际数据中,每个组中有不同数量的主机,有些主机有数百台。我不知道如何在不为每个可能的成员定义变量的情况下将它们输入到命令中。即使是这样,假设我一直把$成员(200)创建(温柔,我不是一个真正的程序员!),然后在调用REST方法命令中逐个手动地输入它们(也可以在这一点上手工操作!)我不确定这个命令会在组中只有少数主机的情况下处理空白输入。
(换句话说,如果我的csv包含以下条目;)
我做到了
# RESTful API Call
$csv | ForEach-Object {
$Name = $_.name
$Member1 =$_.member1
$Member2 =$_.member2
$Member3 =$_.member3
$Member4 =$_.member4
第三组的剩余通话将以以下方式结束:
Invoke-RestMethod -Uri https://172.16.16.16:4444/webconsole/APIController?reqxml=<Request><Login><Username>admin</Username><Password>password</Password></Login><Set%20operation=%27add%27><IPHostGroup><Name>TestGroup3</Name><IPFamily>IPv4</IPFamily><HostList><Host>TestHost7</Host><Host></Host><Host></Host><Host></Host></IPHostGroup></Set></Request>
Invoke RestMethod-Urihttps://172.16.16.16:4444/webconsole/APIController?reqxml=adminpasswordTestGroup3IPv4TestHost7
有人能告诉我一个更好的方法吗 您可以使用
.PSObject.Properties.Name
例如:
$Csv = Import-Csv -Path 'C:\Powershell\groups.csv'
# Request XML template
$RequestTpl = @'
<Request>
<Login>
<Username>admin</Username>
<Password>password</Password>
</Login>
<Set%20operation=%27add%27>
<IPHostGroup>
<Name>{0}</Name>
<IPFamily>IPv4</IPFamily>
<HostList>
{1}
</HostList>
</IPHostGroup>
</Set>
</Request>
'@
# Host list XML template
$RequestHostListTpl = '<Host>{0}</Host>'
$Csv | ForEach-Object {
<#
Get names of all the properties in the current object
Leave only those that don't match '^Name$' regex.
-match, when operates on collections, returns matched items
You can use
$_.PSObject.Properties.Name | Where-Object {$_ -ne 'Name'}
but it's a bit slower.
#>
$Members = @($_.PSObject.Properties.Name) -notmatch '^Name$'
# Build list of hosts
$RequestHostList = foreach ($item in $Members) {
# Only add item if it's not empty
if ($_.$item) {
$RequestHostListTpl -f $_.$item
}
}
# Build request XML
$Request = $RequestTpl -f $_.Name, -join $RequestHostList
# Remove newlines to make it one long string
$Request = $Request -replace '\r|\n'
# Show resulting url
"Invoke-RestMethod -Uri https://172.16.16.16:4444/webconsole/APIController?reqxml=$Request"
# Uncomment to actually invoke API call
#Invoke-RestMethod -Uri "https://172.16.16.16:4444/webconsole/APIController?reqxml=$Request"
}
$Csv=Import Csv-路径“C:\Powershell\groups.Csv”
#请求XML模板
$RequestTpl=@'
管理
密码
{0}
IPv4
{1}
'@
#主机列表XML模板
$requestHostListPL='{0}'
$Csv | ForEach对象{
$Members=@($\.PSObject.Properties.Name)-与“^Name$”不匹配
#生成主机列表
$RequestHostList=foreach($Members中的项目){
#仅在项目不为空时添加项目
如果($。$项){
$requestHostListPL-f$\.$item
}
}
#生成请求XML
$Request=$RequestTpl-f$\名称,-加入$RequestHostList
#删除换行符使其成为一个长字符串
$Request=$Request-替换“\r\n”
#显示结果url
“调用RestMethod-Urihttps://172.16.16.16:4444/webconsole/APIController?reqxml=$Request“
#取消注释以实际调用API调用
#调用RestMethod-Uri“https://172.16.16.16:4444/webconsole/APIController?reqxml=$Request“
}
格式正确的csv文件具有可以枚举的标题,就像问题是它正从以前防火墙的Excel导出保存为csv一样。当我另存为CSV时,它只会为每个附加列添加空白标题(最初,每个组的对象都在同一列中的单独行中,我使用“复制/粘贴”手动转换它们)。我会把答案通读一遍,然后试着理解它,谢谢!
Invoke-RestMethod -Uri https://172.16.16.16:4444/webconsole/APIController?reqxml=<Request><Login><Username>admin</Username><Password>password</Password></Login><Set%20operation=%27add%27><IPHostGroup><Name>TestGroup3</Name><IPFamily>IPv4</IPFamily><HostList><Host>TestHost7</Host><Host></Host><Host></Host><Host></Host></IPHostGroup></Set></Request>
$Csv = Import-Csv -Path 'C:\Powershell\groups.csv'
# Request XML template
$RequestTpl = @'
<Request>
<Login>
<Username>admin</Username>
<Password>password</Password>
</Login>
<Set%20operation=%27add%27>
<IPHostGroup>
<Name>{0}</Name>
<IPFamily>IPv4</IPFamily>
<HostList>
{1}
</HostList>
</IPHostGroup>
</Set>
</Request>
'@
# Host list XML template
$RequestHostListTpl = '<Host>{0}</Host>'
$Csv | ForEach-Object {
<#
Get names of all the properties in the current object
Leave only those that don't match '^Name$' regex.
-match, when operates on collections, returns matched items
You can use
$_.PSObject.Properties.Name | Where-Object {$_ -ne 'Name'}
but it's a bit slower.
#>
$Members = @($_.PSObject.Properties.Name) -notmatch '^Name$'
# Build list of hosts
$RequestHostList = foreach ($item in $Members) {
# Only add item if it's not empty
if ($_.$item) {
$RequestHostListTpl -f $_.$item
}
}
# Build request XML
$Request = $RequestTpl -f $_.Name, -join $RequestHostList
# Remove newlines to make it one long string
$Request = $Request -replace '\r|\n'
# Show resulting url
"Invoke-RestMethod -Uri https://172.16.16.16:4444/webconsole/APIController?reqxml=$Request"
# Uncomment to actually invoke API call
#Invoke-RestMethod -Uri "https://172.16.16.16:4444/webconsole/APIController?reqxml=$Request"
}