使用CSV字段为WebClient下载文件生成保存位置参数

时间:2015-11-02 16:40:56

标签: powershell powershell-ise downloadfile

我试图从CSV文件中读取threatTypethreatSubtypethreatLocation并将该信息用于:

1)指定$storageFile,其中包含threatTypethreatSubtype的组件,

2)指定要下载的位置。

$storageDir = "C:\Threat Intelligence\Zeus"
$webclient = New-Object System.Net.WebClient
$intelSource = "C:\Threat Intelligence\sources.csv"

import-Csv $intelSource | ForEach-Object {
        $storagefile = "${storageDir}\${_.threatType}_${_.threatSubtype}_$(get-date -f MM-dd-yy-ss).csv"
        $url = $_.threatLocation
        foreach ($property in $intelSource)
        {
        $webclient.DownloadFile($url,$storagefile)
        }
    }

除了设置$storageFile的值之外,脚本的每个其他部分都有效。我无法带来$_.threatType$_.threatSubtype

所需的文件名输出+路径为:C:\Threat Intelligence\Zeus\zeus_ip_11-02-15-39.csv其中"zeus""ip"分别为threatTypethreatSubtype

1 个答案:

答案 0 :(得分:0)

99%的修正费用归Matt所示:

$storageDir = "C:\Threat Intelligence\Zeus\"
$webclient = New-Object System.Net.WebClient
$intelSource = "C:\Threat Intelligence\sources.csv"

import-Csv $intelSource | ForEach-Object {
        $storageFile = "$($storageDir)\$($_.threatType)_$($_.threatSubtype)_$($(get-date -f MM-dd-yy-ss)).csv"
        $url = $_.threatLocation
        $webclient.DownloadFile($url,$storageFile)
        }

更改:已删除第二个ForEach$storageFile =已更改为在所有变量之前使用$((通过https://jrich523.wordpress.com/2011/07/01/powershell-working-with-strings/发现此方法)