out-file help

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 years, 2 months ago.

  • Author
    Posts
  • #19128
    Profile photo of Grant Harrington
    Grant Harrington
    Participant

    Should be a simple thing, but I'm stuck. I simply would like what is displayed on the screen in ISE to be sent to a text file at the end of the script. I've tried out-file, but creates a blank text file, the content that is on screen is not there.

    (pre)$MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
    $Today = Get-Date
    Write-host ""
    Write-host ""
    Write-host ""
    Write-host "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"
    Write-host ""
    get-acl "\\$MyACLs\c$\windows\system32\winevt\Logs\" -verbose | select *
    Write-host ""
    Write-host "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"(/pre)

    I simply want what is outputed on screen to be saved as a .txt file in a specified path.

    Right now, I'm copy/paste the screen into a text file (the extra lines at top with write-host, are simply so on screen I have a visual cue as to where start my copy/paste.)

  • #19129
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You've come across one of the reasons why Write-Host is generally frowned on. Using things like Out-File, output from Write-Host is lost. You can try Start-Transcript when you're using PowerShell.exe, but for now, that doesn't work anywhere else. (PowerShell 5.0, currently in a preview release, has transcript support in all hosts, not just powershell.exe)

  • #19130
    Profile photo of Grant Harrington
    Grant Harrington
    Participant

    Thank you.

    So no way to modify the code (ie, write-output), encapsulate it somehow to be piped to " > ", ect..?

    I read about start-transcript, but haven't tried it.

    For my current workflow, this will work (copy/paste), just hoping to be able to apply a technique that produces that result to other scripts down the road.

  • #19131
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    The generally accepted best practice is to use Write-Output for objects that your script is returning, and Write-Verbose for any "screen-only" feedback. If your script's only output is intended to be on the screen, rather than being assigned to a variable or piped to another command, then you can also just use Write-Output in place of either Write-Host or Write-Verbose. This makes it easy to redirect your script's output with the redirection operators, Out-File, etc.

  • #19132
    Profile photo of Grant Harrington
    Grant Harrington
    Participant

    Thanks for your input. I'll continue doing research on this topic.

  • #19138
    Profile photo of Grant Harrington
    Grant Harrington
    Participant

    I don't know if it's the "proper" way to do so, but I've achieved what I need. I modified the code of the original post, but the concept is what I'm after, and it works, I have other scripts I plan to use this on (to avoid copy/paste from the ISE console to a text file) .Put a header/footer above the result of the get-acl command, and send it to a .txt file automatically.

    (pre)$MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
    $Today = Get-Date
    $hf = "### AC-09 = Security Log ACLs – $MyACLs – $Today ###"
    $ga = get-acl "c:\$MyACLs" -verbose | select *

    Function Concat($a){
    $a
    }
    Concat $hf,$ga,$hf | out-file C:\Temp\temp.txt(/pre)

  • #19207
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Have you considered using a here-string? You can format the text in a WYSIWYG format and just output the variable to a text file:

    $MyACLS = Read-Host "Enter Computer Name to obtain log folder's ACLs"
    $Today = Get-Date
    $ACL = get-acl "\\$MyACLs\c$\windows\system32\winevt\Logs\" -verbose | Select *
    
    $hereString = @"
    
    
    
    ### AC-09 = Security Log ACLs – $MyACLs – $Today ###
    
    $ACL
    
    ### AC-09 = Security Log ACLs – $MyACLs – $Today ###
    "@
    
    $hereString | Out-File C:\MyAcl.txt
    

You must be logged in to reply to this topic.