2012-01-23 20 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";