Powershell - 将命令行文件名传递给import-csv

时间:2018-04-06 17:25:32

标签: powershell csv select-object

我正在尝试在将命令输出传递给csv时添加其他列。

我正在运行的命令就是:

Invoke-RestMethod -Uri $uri"<Request><Login><Username>admin</Username><Password>admin</Password></Login><Set operation='add'><IPHost><Name>$Name</Name><IPFamily>IPv4</IPFamily><HostType>IP</HostType><IPAddress>$IPAddress</IPAddress></IPHost></Set></Request>" | Select-Object -Expand Response | Select-Object -Expand IPHost | Select-Object -Expand Status | Export-csv -Append -Path "C:\Powershell\output.csv"

我在 output.csv;

中得到如下输出
"code","#text"
"200","Configuration applied successfully."

我想在第3列和第4列附加 $ Name和$ IPAddress 的值,但我无法弄清楚如何执行此操作。

编辑 -

我希望它看起来像;

"code","#text"    
"200","Configuration applied successfully.","Server 1","192.168.1.1"

(为这些列添加标题并不重要)

Edit2 - 我将在此处扩展我的解释。代码是通过API将一个主机列表添加到防火墙,这是完整的脚本;

$csv = import-csv C:\Powershell\network.csv


$uri = "https://172.16.16.16:4444/webconsole/APIController?reqxml="


$csv | ForEach-Object {
            $Name = $_.name
            $IPAddress =$_.ipaddress

            Invoke-RestMethod -Uri $uri"<Request><Login><Username>admin</Username><Password>admin</Password></Login><Set operation='add'><IPHost><Name>$Name</Name><IPFamily>IPv4</IPFamily><HostType>IP</HostType><IPAddress>$IPAddress</IPAddress></IPHost></Set></Request>" | Select-Object -Expand Response | Select-Object -Expand IPHost | Select-Object -Expand Status Name, IPAddress | Export-csv -Append -Path "C:\Powershell\output.csv" -NoTypeInformation


} 

如果我直接运行原始请求,我会这样做;

https://172.16.16.16:4444/webconsole/APIController?reqxml=<Request><Login><Username>admin</Username><Password>admin</Password></Login><Set operation="add"><IPHost><Name>Server1</Name><IPFamily>IPv4</IPFamily><HostType>IP</HostType><IPAddress>192.168.1.1</IPAddress></IPHost></Set></Request>
浏览器中的

,它提供了以下(XML)输出;

 <Response APIVersion="1700.1">
 <Login>
 <status>Authentication Successful</status>
 </Login>
 <IPHost transactionid="">
 <Status code="200">Configuration applied successfully.</Status>
 </IPHost>
 </Response>

当我通过Invoke-RESTMethod运行时,我在powershell控制台中获得以下内容;

xml                            Response
---                            --------
version="1.0" encoding="UTF-8" Response


PS C:\powershell>

Select-Object命令的每次迭代都会扩展,直到达到结果

Select-Object -Expand Response

给出;

APIVersion Login IPHost
---------- ----- ------
1700.1     Login IPHost


PS C:\powershell>

 Select-Object -Expand Response | Select-Object -Expand IPHost

给出;

 transactionid Status
 ------------- ------
               Status

PS C:\ powershell&gt;

最后,完整的命令;

 Select-Object -Expand Response | Select-Object -Expand IPHost | Select-Object -Expand Status 

给出;

code #text
---- -----
200  Configuration applied successfully.

对于每个主机,这是我想要的(即操作的结果)。然后我只想将其导出到输出文件,同时还指示状态适用于哪个IP主机。我认为CSV是最简单的格式,也许不是吗?

希望这更清楚!

2 个答案:

答案 0 :(得分:0)

您要做的是将输入信息$Name$IPAddress与返回XML的Invoke-RESTMethod调用的输出结合起来并将该组合导出到CSV文件。< / p>

您可以借助定义calculated properties Select-Object来实现此目的:

假设$response包含您的输出 Invoke-RestMethod -Uri $uri"<Request>...</Request>"致电:

# Use Select-Object to extract the properties of interest and also
# add the input variables $Name and $IPAddress as properties:
$response | Select-Object @{ n='code';      e={ $_.Response.IPHost.Status.code } }, 
                          @{ n='status';    e={ $_.Response.IPHost.Status.InnerText } }, 
                          @{ n='Name';      e={ $Name } }, 
                          @{ n='IPAddress'; e={ $IPAddress } } |
              Export-Csv -NoTypeInformation -Append -Path "C:\Powershell\output.csv" 

以上结果如下:

"code","status","Name","IPAddress"
"200","Configuration applied successfully.","Server1","192.168.1.1"

答案 1 :(得分:0)

如果您使用的是foreach循环,则可以创建一个pscustomobject并将其导出:

$csv = Import-Csv C:\Powershell\network.csv
$uri = "https://172.16.16.16:4444/webconsole/APIController?reqxml="

$csv | ForEach-Object {
    $Name = $_.name
    $IPAddress =$_.ipaddress

    $RestMethod = Invoke-RestMethod -Uri $uri"<Request><Login><Username>admin</Username><Password>admin</Password></Login><Set operation='add'><IPHost><Name>$Name</Name><IPFamily>IPv4</IPFamily><HostType>IP</HostType><IPAddress>$IPAddress</IPAddress></IPHost></Set></Request>"

    $Export = [pscustomobject] @{
        'code' = $RestMethod.Response.IPHost.Status.Code
        'text' = $RestMethod.Response.IPHost.Status.text
        'name' = $Name
        'ipaddress' = $IPAddress
    }

    $Export | Export-csv -Append -Path "C:\Powershell\output.csv" -NoTypeInformation 
}