1

I have this function where I want the output file name should be same as the filename with "_new.log" added to the end. This is my script. it does not work.

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

IIS-CleanUp1 "u_ex130801.log"

It throws the following error:

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

Any help is much appreciated:

  • T
4

1 回答 1

3

它将 the+作为第二个参数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
于 2013-09-05T14:59:04.427 回答