2015-11-02 58 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/發現此方法)