2012-01-23 17 views

回答

9

试试这个(我不能测试它)

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

'Out-File'也具有'-Width'参数。其文档指出超出宽度的所有内容都被截断(不被包装)。所以我猜这首先截断了4096个字符(奇怪的非圆数字),然后是控制台窗口的宽度。虽然它不会包装它会截断更长的行,这可能不是目的。 – Joey

+3

4096并不奇怪,也不是非圆。这是2^12 –

5

而不是使用>,这是out-file,你可以使用set-content

+1

注意:“Set-Content”的行为不同。有意义的(但没有记录)它锁定了文件,因此无法读取。因此'Set-Content'是日志记录的不错选择。请参阅http://stackoverflow.com/questions/10655788/powershell-add-content-and-out-file-what-is-the-difference –

1

使用写主机 cmdlet的作为你管道的最后一个陈述。普通的unatned powershell输出看起来像是在父控制台窗口的尺寸,并将输出行修剪/换行为width-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.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"; 

最后,一个ter如果省略Write-Host cmdlet(假设控制台宽度为60),则会发生rible事件:

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";