【问题标题】:powershell - adding a string to output fill name is throwing an errorpowershell - 将字符串添加到输出填充名称会引发错误
【发布时间】:2013-09-05 14:55:44
【问题描述】:

我有这个函数,我希望输出文件名应该与最后添加“_new.log”的文件名相同。这是我的脚本。它不起作用。

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

非常感谢任何帮助:

  • T

【问题讨论】:

    标签: powershell output


    【解决方案1】:

    它将+ 作为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
    

    【讨论】:

    • 谢谢。它解决了这个问题。但它说 u_ex130801.log_new.log。它正在做代码要求它做的事情。我怎样才能说 u_ex130801_new.log
    • @user1666952 - 什么是$file?请参阅更新的答案以了解方法。
    • 太棒了。再次感谢。像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 2015-10-11
    • 2023-01-11
    • 2011-01-07
    • 2013-09-02
    • 2020-07-20
    • 2011-03-08
    • 2017-04-11
    相关资源
    最近更新 更多