Windows 7 防止在PowerShell输出文件中换行的命令行语法?

Windows 7 防止在PowerShell输出文件中换行的命令行语法?,windows-7,powershell,command-line,redirect,word-wrap,Windows 7,Powershell,Command Line,Redirect,Word Wrap,下面的命令将输出包装到调用脚本的窗口的宽度。也就是说,输出文件是“word”包装的。如何在不修改脚本的情况下防止输出文件中出现这种包装 PS C:\Users\User1> & '\\fileServer\c$\PowerShell Scripts\herScript.ps1' > output.txt 试试这个(我无法测试) 您可以使用设置内容而不是使用(即输出文件),使用写入主机cmdlet作为管道的最后一条语句。正常的未经修饰的powershell输出显示为查看父控制

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

PS C:\Users\User1> & '\\fileServer\c$\PowerShell Scripts\herScript.ps1' > output.txt
试试这个(我无法测试)


您可以使用
设置内容
而不是使用
(即
输出文件
),使用写入主机cmdlet作为管道的最后一条语句。正常的未经修饰的powershell输出显示为查看父控制台窗口的尺寸,并将输出行修剪/包装为宽度-1。Write-Host cmdlet绕过此步骤,直接写入stdout而无需任何进一步的修改

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

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

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";
最后一个例子是,如果不使用Write-Host cmdlet(假设控制台宽度为60),会发生可怕的事情:


Out文件
也有一个
-Width
参数。它的文档说明宽度以外的所有内容都被截断(而不是包装)。所以我猜这会先截断4096个字符(这是一个奇怪的非整数),然后截断控制台窗口的宽度。虽然它不会换行,但它会截断可能不需要的较长的行。4096不是奇数,也不是非圆的。2^12
-Width
参数似乎无法阻止换行。在
输出文件
之前的管道中注入
输出字符串-宽度xxx
,对我来说确实起到了作用。警告选择器:
设置内容
的行为有所不同。重要的是(但没有文档记录)它会锁定文件,使其无法读取。因此,
设置内容
对于日志记录来说是一个糟糕的选择。当我使用Write Host时,看不到任何重定向到文件的内容。PowerShell 5.1。
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.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";
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: sdfjkghsdfjkghjksdfghjkf
hjkdfjff\n";
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";