about_Redirection

Aggiornamento: maggio 2014

Si applica a: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

ARGOMENTO

about_Redirection

DESCRIZIONE BREVE

Spiega come reindirizzare l'output da Windows PowerShell® a file di testo.

DESCRIZIONE LUNGA

Per impostazione predefinita, Windows PowerShell invia l'output del comando alla console di Windows PowerShell. Tuttavia, è possibile indirizzare l'output a un file di testo ed è possibile reindirizzare l'output di errore al regolare flusso di output.

È possibile usare i metodi seguenti per reindirizzare l'output:

  • - Usare il cmdlet Out-File, che invia l'output del comando a un file di testo. In genere, usare il cmdlet Out-File quando è necessario usarne i parametri, come ad esempio i parametri Encoding, Force, Width oppure NoClobber.

  • - Usare il cmdlet Tee-Object, che invia l'output del comando a un file di testo e quindi lo invia alla pipeline.

  • - Usare gli operatori di reindirizzamento di Windows PowerShell.

OPERATORI DI REINDIRIZZAMENTO DI WINDOWS POWERSHELL

Gli operatori di reindirizzamento consentono di inviare tipi particolari di output a file e al flusso di output riuscito.

Gli operatori di reindirizzamento di Windows PowerShell usano i caratteri seguenti per rappresentare ogni tipo di output:

        *   All output
        1   Success output
        2   Errors
        3   Warning messages
        4   Verbose output
        5   Debug messages

NOTA: Tutti gli operatori di reindirizzamento All (*), Warning (3), Verbose (4) e Debug (5) sono stati introdotti in Windows PowerShell 3.0. Non funzionano nelle versioni precedenti di Windows PowerShell.

Gli operatori di reindirizzamento di Windows PowerShell sono i seguenti.

      Operator  Description                Example  
      --------  ----------------------     ------------------------------
      >         Sends output to the        Get-Process > Process.txt
                specified file.

      >>        Appends the output to      dir *.ps1 >> Scripts.txt
                the contents of the  
                specified file.

      2>        Sends errors to the        Get-Process none 2> Errors.txt
                specified file.
 
      2>>       Appends errors to          Get-Process none 2>> Save-Errors.txt
                the contents of the 
                specified file.
 
      2>&1      Sends errors (2) and       Get-Process none, Powershell 2>&1
                success output (1) 
                to the success 
                output stream.

      3>        Sends warnings to the      Write-Warning "Test!" 3> Warnings.txt
                specified file.

      3>>       Appends warnings to        Write-Warning "Test!" 3>> Save-Warnings.txt
                the contents of the 
                specified file.

      3>&1      Sends warnings (3) and     function Test-Warning 
                success output (1)         {  Get-Process PowerShell; 
                to the success                Write-Warning "Test!" }
                output stream.             Test-Warning 3>&1

      4>        Sends verbose output to    Import-Module * -Verbose 4> Verbose.txt
                the specified file.

      4>>       Appends verbose output     Import-Module * -Verbose 4>> Save-Verbose.txt
                to the contents of the 
                specified file.

      4>&1      Sends verbose output (4)   Import-Module * -Verbose 4>&1
                and success output (1)    
                to the success output
                stream.              
 
      5>        Sends debug messages to    Write-Debug "Starting" 5> Debug.txt
                the specified file.

      5>>       Appends debug messages     Write-Debug "Saving" 5>> Save-Debug.txt
                to the contents of the 
                specified file.

      5>&1      Sends debug messages (5)   function Test-Debug 
                and success output (1)     { Get-Process PowerShell 
                to the success output        Write-Debug "PS" }
                stream.                    Test-Debug 5>&1

      *>        Sends all output types     function Test-Output
                to the specified file.     { Get-Process PowerShell, none  
                                             Write-Warning "Test!"
      *>>       Appends all output types     Write-Verbose "Test Verbose"
                to the contents of the       Write-Debug "Test Debug" } 
                specified file.            
                                           Test-Output *> Test-Output.txt
      *>&1      Sends all output types     Test-Output *>> Test-Output.txt
                (*) to the success output  Test-Output *>&1      
                stream.     

La sintassi degli operatori di reindirizzamento è la seguente:

       <input> <operator> [<path>\]<file>

Se il file specificato esiste già, gli operatori di reindirizzamento non aggiungono dati (> e n>) sovrascrivono i contenuti correnti del file senza avviso. Tuttavia, se il file è di sola lettura, nascosto o un file di sistema, il reindirizzamento non riesce. Gli operatori di reindirizzamento aggiunti (>> e n>>) non scrivono in un file di sola lettura, ma possono aggiungere contenuto a un file di sistema o nascosto.Inserire qui il corpo del testo.

Per forzare il reindirizzamento del contenuto in un file di sola lettura, nascosto o di sistema, usare il cmdlet Out-File con il relativo parametro Force. Quando si stanno scrivendo i file, gli operatori di reindirizzamento usano la codifica Unicode. Se il file ha una codifica differente, l'output potrebbe non essere formattato correttamente. Per reindirizzare il contenuto in file non Unicode, usare il cmdlet Out-File con il relativo parametro Encoding.

VEDERE ANCHE

Out-File

Tee-Object

about_Operators

about_Command_Syntax

about_Path_Syntax