2015-11-02 32 views
0

我试图从一个CSV文件中读取threatTypethreatSubtypethreatLocation并利用这些信息来:使用CSV域生成保存位置论据Web客户端下载文件

1)指定$storageFile其中包括threatType组件和threatSubtype,

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

+0

'$ storagefile =“ {$ storageDir} \ {$ _。threatType} _ {$ _。threatSubtype} _ $(get-date -f MM-dd -yy-ss).csv“'为'$ webclient.DownloadFile返回'MethodInvocationException' url,$ storagefile)' – ayoungblood

+0

可能还有其他问题。首先,我指出的代码是错误的,需要修正。 '$ storagefile'在循环中看起来是否正确? – Matt

+0

哦。傻我。我错过了这个循环以及'foreach($ intelSource中的$ property)'应该被删除。我没有看到服务的目的。你已经循环了'$ intelSource',所以你不需要再循环它。 – Matt

回答

0

感谢Matt为99%的修复:

$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/发现此方法)