2012-01-23 14 views

risposta

9

Prova questo (non posso provarlo)

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

'Out-File' ha un parametro' -Width', troppo. La sua documentazione afferma che tutto oltre la larghezza viene troncato (non spostato). Quindi immagino che questo tronca prima a 4096 caratteri (che è un numero stranamente non rotondo) e poi alla larghezza della finestra della console. Anche se non lo avvolgerà, troncherà linee più lunghe che potrebbero non essere intese. – Joey

+3

4096 non è dispari e non è rotondo. È 2^12 –

5

Invece di usare >, che è out-file, è possibile utilizzare set-content

+1

Avvertenza: il comportamento di 'Set-Content' differisce. Significativamente (ma non documentato) blocca il file in modo che non possa essere letto. Quindi 'Set-Content' è una cattiva scelta per la registrazione. Vedi http://stackoverflow.com/questions/10655788/powershell-add-content-and-out-file-what-is-the-difference –

1

Utilizzare il Write-Host cmdlet come l'ultima affermazione della tua pipeline. L'output di PowerShell normale e senza ornamenti sembra guardare le dimensioni della finestra della console genitore e taglia e incartona le linee di output su width-1. Il cmdlet Write-Host ignora questo passaggio e scrive direttamente su stdout senza ulteriori munging.

Ecco un esempio, che legge un file JSON, e scrive javascript uscita che aggiunge la JSON ad una grande stringa (commenti conservazione):

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

Ecco un file di input di esempio:

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

E l'uscita quando si utilizza Write-Host:

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

E, infine, un esempio del ter cose Rible che accadono se si lascia fuori il cmdlet Write-Host (assumendo una larghezza console di 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: 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"; 
Problemi correlati