命令行语法,以防止在PowerShell输出文件中包装?

时间:2012-01-23 14:55:54

标签: windows-7 powershell command-line redirect textwrapping

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

PS C:\Users\User1> & '\\fileServer\c$\PowerShell Scripts\herScript.ps1' > output.txt

3 个答案:

答案 0 :(得分:11)

试试这个(我无法测试)

& '\\fileServer\c$\PowerShell Scripts\herScript.ps1' | out-string -width 4096 | out-file c:\output.txt

答案 1 :(得分:5)

您可以使用>

而不是使用out-file set-content

答案 2 :(得分:1)

使用 Write-Host cmdlet作为管道的最后一个语句。普通的unadorned powershell输出似乎查看父控制台窗口的尺寸,并将输出行修剪/包装到width-1。 Write-Host cmdlet绕过此步骤并直接写入stdout而不进行任何进一步的重复操作。

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

char string[] = "string literal";

这是一个示例输入文件:

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

使用Write-Host时的输出:

//  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",
}

最后,如果省略Write-Host cmdlet(假设控制台宽度为60),会发生一些可怕的事情:

manifestBlob += "\n";
manifestBlob += "//  File: manifest.json\n";
manifestBlob += "//\n";
manifestBlob += "//  Description:\n";
manifestBlob += "//      manifest for chrome plug-in\n";
manifestBlob += "\n";
manifestBlob += "{\n";
manifestBlob += "    \"manifest_version\": 2,\n";
manifestBlob += "\n";
manifestBlob += "    \"version\": \"0.0.0\",\n";
manifestBlob += "\n";
manifestBlob += "    // the ID is: sdfjkghsdfjkghjksdfghjkfhjkdfjff\n";
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";