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

Powershell - Passing command line filename to import-csv

我试图在将命令输出通过管道传输到 csv 时添加额外的列。

我 运行ning 的命令是这样的:

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"

(为这些列添加一个header并不重要)

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>

最后是完整的命令;

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

给予;

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

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

希望这更清楚!

您要做的是将输入信息 $Name$IPAddressInvoke-RESTMethod 调用 returns XML 的输出相结合并将该组合导出到 CSV 文件。

您可以借助定义 :

单个 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"

如果您正在使用 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 
}