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

1 Answer 1

3

It is taking the + as the second argument to Out-File, which is the Encoding and that is why see the above error. Try something like:

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

You might have to handle file name and extension better as follows:

$f = get-item $file
$newpath = join-path $f.directoryname $($f.basename + "_new" + $f.extension) 
get-content $f | ... | out-file $newpath
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. It fixed the issue. But it says u_ex130801.log_new.log. It is doing what the code is asking it to do. How can I make it say u_ex130801_new.log
@user1666952 - What is $file? See updated answer for an approach.
Awesome. Thanks again. Worked like a charm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.