output results to a file

Home Forums Scripting PowerShell output results to a file

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    mmoore
    Member
    #164665

    I have to create a log file on a bunch of my scripts where i remove items and add them. I am not use to adding a log file. How would i go to do that. I added write-host to output what the script is doing to the screen but when i try to pipe it out it says empty pipe.

    Here is the script . I have a couple more that loops like this and trying to find nice easy way to get the results to a file. Also would write-host be the right syntax or would i want to use write-output. I am just wanting whatever i do to be logged.

    [COLOR=orangered][FONT=&quot]$User[/FONT][/COLOR][FONT=&quot] [COLOR=darkgray]=[/COLOR] [COLOR=blue]Read-Host[/COLOR] [COLOR=darkred]”Input TSO ID”[/COLOR][/FONT]
    [COLOR=orangered][FONT=&quot]$UserList[/FONT][/COLOR][FONT=&quot] [COLOR=darkgray]=[/COLOR] [COLOR=blue]get-content[/COLOR] [COLOR=blueviolet].RemoveUserFromGroups.txt[/COLOR][/FONT]
    [COLOR=orangered][FONT=&quot]$logfile[/FONT][/COLOR][FONT=&quot] [COLOR=darkgray]=[/COLOR] [COLOR=darkred]”c:log.txt”[/COLOR][/FONT]
    [COLOR=darkblue][FONT=&quot]Foreach[/FONT][/COLOR][FONT=&quot] ([COLOR=orangered]$Item[/COLOR] [COLOR=darkblue]in[/COLOR] [COLOR=orangered]$UserList[/COLOR]) {[/FONT]

    [COLOR=darkblue][FONT=&quot]try[/FONT][/COLOR][FONT=&quot]{[/FONT]
    [FONT=&quot] [COLOR=blue]Remove-ADGroupMember[/COLOR] [COLOR=navy]-Identity[/COLOR] [COLOR=orangered]$item[/COLOR] [COLOR=navy]-Members[/COLOR] [COLOR=orangered]$User[/COLOR] [COLOR=navy]-Confirm:[/COLOR][COLOR=orangered]$false[/COLOR][/FONT]
    [FONT=&quot] [COLOR=blue]Write-host[/COLOR] [COLOR=darkred]”[/COLOR][COLOR=orangered]$item[/COLOR][COLOR=darkred] has been removed “[/COLOR][/FONT]

    [FONT=&quot]}[/FONT]

    [FONT=&quot] [COLOR=darkblue]catch[/COLOR][/FONT]
    [FONT=&quot] {[/FONT]
    [FONT=&quot] [COLOR=blue]Write-Host[/COLOR] [COLOR=darkred]”[/COLOR][COLOR=orangered]$item[/COLOR][COLOR=darkred] has not been removed”[/COLOR] [/FONT]
    [FONT=&quot] }[/FONT]

    [FONT=&quot]}[COLOR=darkgray]|[/COLOR] [COLOR=blue]out-file[/COLOR] [COLOR=orangered]$logfile[/COLOR] [COLOR=navy]-Append[/COLOR][/FONT]
    [COLOR=blue][FONT=&quot]Read-Host[/FONT][/COLOR][FONT=&quot] [COLOR=darkred]”Removed Groups please check. Press enter to close window.Any groups not removed will show up in the window.” [/COLOR][/FONT][/CODE][CODE][FONT=&quot]$User[/FONT][FONT=&quot] = Read-Host “Input TSO ID”[/FONT]
    [FONT=&quot]$UserList[/FONT][FONT=&quot] = get-content .RemoveUserFromGroups.txt[/FONT]
    [FONT=&quot]$logfile[/FONT][FONT=&quot] = “c:log.txt”[/FONT]
    [FONT=&quot]Foreach[/FONT][FONT=&quot] ($Item in $UserList) {[/FONT]

    [FONT=&quot]try[/FONT][FONT=&quot]{[/FONT]
    [FONT=&quot] Remove-ADGroupMember -Identity $item -Members $User -Confirm:$false[/FONT]
    [FONT=&quot] Write-host $item has been removed “[/FONT]

    [FONT=&quot]}[/FONT]

    [FONT=&quot] catch[/FONT]
    [FONT=&quot] {[/FONT]
    [FONT=&quot] Write-Host $item has not been removed” [/FONT]
    [FONT=&quot] }[/FONT]

    [FONT=&quot]}| out-file $logfile -Append[/FONT]
    [FONT=&quot]Read-Host[/FONT][FONT=&quot] “Removed Groups please check. Press enter to close window.Any groups not removed will show up in the window.” [/FONT][/CODE]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.