Save the output of powershell script

Tagged: 

This topic contains 11 replies, has 5 voices, and was last updated by  Robin16 4 years ago.

  • Author
    Posts
  • #9621

    Robin16
    Participant

    Hi,
    i'm trying to store the output the powershell script into text file...we can do this way when executing the script

    powershell test.ps1 > testlog.txt

    but i would like to do it within the powershell and my PS script has too many commands and it does lot of operation..is there a way to achieve this ?
    Please let me know

    Thanks in advace

  • #9625

    notarat
    Participant

    There are a few different ways to output the data from your script, but the best way to see how to do so would require you to post your code so we can see what data you're pulling.

    for general purposes, you can use the add-content command to create the file and add your data to it.

    EXAMPLE
    $strCMP=GET-ADComputer ComputerNameHere –Properties cn,MemberOf
    $strGRP=($strCMP.MemberOf | Sort Name | foreach {($_.Split(','))[0].Substring(3)}) -Join ","
    $strCMP=($strCMP.cn | foreach {($_.Split('='))[0]}) -Join ","
    $strDone=$strCMP+","+$strGRP
    Add-Content c:\outputfiles\CompandGroups.txt -value $strdone

    This script pulls the computername and the groups it is assigned to from Active Directory and then writes that information to a text file.

    I could also use this code to save to a filename with a .csv extension to make it easier to view the data in MS Excel or another spreadsheet.

  • #9626

    Robin16
    Participant

    Thanks Notarat for your valuable input.....
    Most of my script output is from write-output...please find the attached PS script and below is how the output looks like...

    Proceeding with the restore
    User logged in successfully
    Destination Database Informatica_Test9 exists
    Successfully dropped the destination DB Informatica_Test9

    #text
    —–
    |Informatica_Test9|#12!Informatica_Test9|#12!Informatica_Test9|#12!D:\MSSQL\DATA\Informatica_Test9.mdf|#12!D:\MSSQL\DATA\Informatic...
    |Informatica_Test9|#12!Informatica_Test9|#12!Informatica_Test9_log|#12!L:\MSSQL\LOG\Informatica_Test9_1_log.ldf|#12!L:\MSSQL\LOG\In...

    Check the job status using: .\qlist job -co s -j JobID

  • #9627

    Dave Wyatt
    Moderator

    This is one of the first questions I asked when I started working with PowerShell a few months ago. There's not really a perfect solution at the moment, but I have a couple of modules up on the Script Center that might help. (One is written purely in PowerShell, but has less functionality. The second is written in C# and uses Reflection to intercept PowerShell Host output; it is a more complete solution, but may stop working and require updates if Microsoft makes changes to PowerShell's implementation at some point.)

    http://gallery.technet.microsoft.com/Write-timestamped-output-4ff1565f
    http://gallery.technet.microsoft.com/Enhanced-Script-Logging-27615f85

  • #9628

    Don Jones
    Keymaster

    Please note that we can't accept PS1 file attachments. Please rename your script file to TXT, or include it in a ZIP file, to attach it. You can also paste your script at PoshCode.org, and then provide a link to it here.

  • #9634

    Robin16
    Participant

    Thanks Don. Please find the attached ps script...and Thanks a lot Dave for the links....could you also please take a look at my file and suggest me how to get the output in a single file with in the scrip ?

    Thanks in advance

  • #9639

    Dave Wyatt
    Moderator

    I'm not sure what you mean by "get the output in a single file within the script". Do you mean all of the output should wind up in a single text file? Or do you mean all of the code should be in a single script file, without having to import modules or dot-source anything? (Or something else entirely?) Please clarify. 🙂

  • #9648

    Robin16
    Participant

    Thanks Dave, i meant all of the output should wind up in a single text file....Please let me know if you have any questions....

  • #9649

    Doug Bishop
    Participant

    Here is code I use. The if($Host.Name...) will start the transcripting only if not running in the ISE, since ISE does not support transcript files.

    $now=Get-Date -format "yyyyMMdd_HHmmss"

    if ($Host.Name -ne "Windows PowerShell ISE Host")
    {
    $TranscriptFile = $ROOT + "LOGS\" + $MyInvocation.MyCommand.Name.Replace(".ps1", "") + "_$now.log"
    Start-Transcript -Path $TranscriptFile
    }
    ...
    ...
    if ($Host.Name -ne "Windows PowerShell ISE Host")
    {
    Stop-Transcript
    }

  • #9653

    Dave Wyatt
    Moderator

    Both of the modules I posted can do that, though the second one is easier to use (requires fewer changes to your existing script):

    # At beginning of script (either in the Begin block, if you're using begin/process/end, or just after the param() block)
    
    # This syntax assumes you've placed the PSLogging module in your modules directory.  If not, you can specify an absolute
    # path to the folder or PSLogging.psd1 file instead of just 'PSLogging' in the Import-Module command.
    
    Import-Module PSLogging
    $logFile = Add-LogFile -Path C:\Path\To\LogFile.log
    
    # ... Your existing script code
    
    # At the end of the script
    
    $logFile | Disable-LogFile
    
    # Output should have been captured to the 'C:\Path\To\LogFile.log' file.
    
  • #9654

    Dave Wyatt
    Moderator

    Start-Transcript is another option. The main disadvantages of that command versus the PSLogging module are that Start-Transcript doesn't work from the ISE, and you can only have one transcript going for the entire session (which may become an issue if you're calling multiple scripts or functions that all try to do their own logging via Start-Transcript). The PSLogging module supports multiple simultaneous subscribers that mostly don't interfere with one another (except when calling Suspend-Logging and Resume-Logging, which is a global setting).

  • #9663

    Robin16
    Participant

    Thansk a lot David and Doug for your quick response.. you guys are really awesome and helpful....

You must be logged in to reply to this topic.