用于防止在PowerShell输出文件中换行的命令行语法



以下命令将输出包装到调用脚本的窗口宽度。也就是说,输出文件是"word"包装的。如何在不修改脚本的情况下防止输出文件中出现这种包装?

PS C:UsersUser1> & '\fileServerc$PowerShell ScriptsherScript.ps1' > output.txt

试试这个(我无法测试)

& '\fileServerc$PowerShell ScriptsherScript.ps1' | out-string -width 4096 | out-file c:output.txt

您可以使用set-content

而不是使用>,即out-file

使用Write-Hostcmdlet作为管道的最后一条语句。普通的未修饰powershell输出看起来像是查看父控制台窗口的尺寸,并将输出行修剪/包装为宽度-1。Write-Host cmdlet绕过此步骤,直接写入stdout,而不进行任何进一步的处理。

下面是一个示例,它读取一个JSON文件,并编写javascript输出,将JSON添加到一个大字符串中(保留注释):

powershell -Command "$input | ForEach-Object { "manifestBlob += """ + ($_ -replace """", "\""") + "n"";" } | Write-Host" < manifest.json > buildmanifest.js

下面是一个示例输入文件:

//  File: manifest.json
//
//  Description:
//      manifest for chrome plug-in
{
    "manifest_version": 2,
    "version": "0.0.0",
    // the ID is: sdfjkghsdfjkghjksdfghjkfhjkdfjff
    "key": "sdfjkhsdfjkghjksdfghkjsdhgsdjkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsdfjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl",
    "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["content.js"], "run_at": "document_start" } ],
    // this is the standard LOCAL install location - but if this extension is published to the app-store, this value gets overridden (that is okay and even good)
    "update_url": "file:///C:/Program%20Files/MyProduct/Update.xml",
}

以及使用写入主机时的输出:

manifestBlob += "n";
manifestBlob += "//  File: manifest.jsonn";
manifestBlob += "//n";
manifestBlob += "//  Description:n";
manifestBlob += "//      manifest for chrome plug-inn";
manifestBlob += "n";
manifestBlob += "{n";
manifestBlob += "    "manifest_version": 2,n";
manifestBlob += "n";
manifestBlob += "    "version": "0.0.0",n";
manifestBlob += "n";
manifestBlob += "    // the ID is: sdfjkghsdfjkghjksdfghjkfhjkdfjffn";
manifestBlob += "    "key": "sdfjkhsdfjkghjksdfghkjsdhgsdjkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsdfjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl",n";
manifestBlob += "n";
manifestBlob += "    "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["content.js"], "run_at": "document_start" } ],n";
manifestBlob += "n";
manifestBlob += "    // this is the standard LOCAL install location - but if this extension is published to the app-store, this value gets overridden (that is okay and even good)n";
manifestBlob += "    "update_url": "file:///C:/Program%20Files/MyProduct/Update.xml",n";
manifestBlob += "}n";

最后,举一个例子,说明如果省略Write-Host cmdlet(假设控制台宽度为60)会发生什么可怕的事情:

manifestBlob += "n";
manifestBlob += "//  File: manifest.jsonn";
manifestBlob += "//n";
manifestBlob += "//  Description:n";
manifestBlob += "//      manifest for chrome plug-inn";
manifestBlob += "n";
manifestBlob += "{n";
manifestBlob += "    "manifest_version": 2,n";
manifestBlob += "n";
manifestBlob += "    "version": "0.0.0",n";
manifestBlob += "n";
manifestBlob += "    // the ID is: sdfjkghsdfjkghjksdfghjkf
hjkdfjffn";
manifestBlob += "    "key": "sdfjkhsdfjkghjksdfghkjsdhgs
djkgfhjklsdfhgjklsdfhgjklsdhfgkljsdfhgkljsdhklgjsdhfjklghsd
fjklghsdjklfghjksdfhgjksdhfgjklhsdfjkl",n";
manifestBlob += "n";
manifestBlob += "    "content_scripts": [ { "matches": 
["http://*/*", "https://*/*"], "js": ["content.js"]
, "run_at": "document_start" } ],n";
manifestBlob += "n";
manifestBlob += "    // this is the standard LOCAL install 
location - but if this extension is published to the app-st
ore, this value gets overridden (that is okay and even good
)n";
manifestBlob += "    "update_url": "file:///C:/Program%2
0Files/MyProduct/Update.xml",n";
manifestBlob += "}n";

最新更新