【问题标题】:PS is not creating a new file everytimePS不是每次都创建一个新文件
【发布时间】:2018-08-10 02:48:55
【问题描述】:

我每次执行时都使用以下 PS cmdlet 创建一个新的 json 文件。应该覆盖现有的 json 文件

$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | New-Item -path $myFileName -Force

但是,如果它们已经是具有相同文件名的现有文件,则不会创建新的 json 文件。

【问题讨论】:

    标签: windows powershell powershell-4.0 powershell-remoting


    【解决方案1】:

    New-Item 不是针对这种情况选择的函数(因为它实际上只能用于创建 New-Items)。

    你应该改用Out-File

    $jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
    $jsonformatOutput | Out-File -Filepath $myFileName
    

    这会将变量写入文件$myFileName,如果该文件仍然存在,则覆盖该文件。

    如果您想将内容添加到现有文件而不是覆盖它,您可以使用-Append

    【讨论】:

      猜你喜欢
      • 2014-07-29
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多