2013-09-05 19 views
1

我有這個功能,我希望輸出文件名應該與文件名「_new.log」添加到最後。這是我的劇本。這是行不通的。powershell - 添加一個字符串輸出填充名稱是拋出一個錯誤

Function IIS-CleanUp1($file) 
{ 
Get-Content $file | ? { $_ -match $control -and $_ -notmatch '^\#'} | Out-File $file + "_new.log" 
} 

IIS-CleanUp1 "u_ex130801.log" 

它引發以下錯誤:

Out-File : Cannot validate argument on parameter 'Encoding'. The argument "+" does not belong to the set "unicode,utf7, 
utf8,utf32,ascii,bigendianunicode,default,oem" specified by the ValidateSet attribute. Supply an argument that is in th 
e set and then try the command again. 
At C:\ShiyamTemp\IIS_CCHECK_AUG\IIS.ps1:3 char:79 
+ Get-Content $file | ? { $_ -match $control -and $_ -notmatch '^\#'} | Out-File <<<< $file + "_new.log" 
    + CategoryInfo   : InvalidData: (:) [Out-File], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.OutFileCommand 

任何幫助非常感謝:

  • Ť

回答

3

它走的是+作爲第二個參數,以Out-File ,這是Encoding,這就是爲什麼看到上述錯誤。嘗試是這樣的:

.. | Out-File $($file + "_new.log") 

您可能需要處理的文件名和擴展名更好如下:

$f = get-item $file 
$newpath = join-path $f.directoryname $($f.basename + "_new" + $f.extension) 
get-content $f | ... | out-file $newpath 
+0

感謝。它解決了這個問題。但它說u_ex130801.log_new.log。它正在做代碼要求它做的事情。我該如何說u_ex130801_new.log – user1666952

+0

@ user1666952 - 什麼是'$ file'?查看方法的更新答案。 – manojlds

+0

太棒了。再次感謝。像魅力一樣工作 – user1666952

相關問題