2012-10-18 33 views
1

我試圖寫一個PowerShell腳本將從本地客戶端的Exchange服務器創建Outlook日誌(使用NETMON)。問題是文件名沒有正確遞增。我在網上發現了這個遞增的腳本,並且我理解邏輯(有點)。我是否用正確的方式初始化櫃檯?它不正確地重命名文件。它給第二個文件只是一個_然後第三個文件得到一個_2然後它停止重命名文件。PowerShell的遞增名稱和執行CMDLINE

還援引表達等着我的CMD退出,否則將只是去count--的$下一步?

#Directory to complete script in 
$path = "c:\Outlook_Logs\" 
cd $path 
$cmdline = "Nmcap.exe /network * /captureipv4.address==X.X.X.X /file :\outlook_logs\client_$count.chn:100MB" 

#Writes out number of files in directory to console 
$count = (get-childitem $path -name).count 
Write-Host "Number of Files: $count" 

#Sorts items by decsending order 
$items = Get-ChildItem | Sort Extension -desc 

#Deletes oldest file by file extension number 
del $items[0] 

#Copy file from original directory to backup directory 
Copy-Item c:\Outlook_Logs\* c:\Outlook_Logs_Temp\ 

#Sorts items by decsending order 
$items = Get-ChildItem | Sort Extension -desc 

#Renames files in quotes after NewName argument 
$items | ForEach-Object -begin { $count= (get-childitem $path -name).count } -process { rename- item $_ -NewName "client_$count.cap";Invoke-Expression "$cmdline"; $count-- } 

回答

0

有沒有必要來增加自己的名字。 chn捕獲文件在達到冒號後指定的大小時會自動創建並自動遞增。

當你降低大小1MB並執行該命令會發生什麼?

+0

當我運行CMD兩次它只是覆蓋它以前的文件。它不作爲,你說... –

+0

Nmcap.exe /網絡* /捕獲ipv4.address == X.X.X.X /文件c:\ outlook_logs \ client.chn:1MB –

+0

我不認爲它會記住它停止。一旦你重新運行,它將重新開始。如果你想自己增加它,你需要使用的.cap擴展 –