Announcement

Collapse
No announcement yet.

output results to a file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • output results to a file

    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.

    Code:
    $User = Read-Host   "Input TSO ID"
     $UserList = get-content .\RemoveUserFromGroups.txt
     $logfile = "c:\log.txt"
     Foreach ($Item in $UserList) {
     
     try{
         Remove-ADGroupMember -Identity $item -Members $User -Confirm:$false
            Write-host "$item has been removed "
     
     
     }
     
      catch
         {
             Write-Host "$item has not been removed" 
         }
     
     
     }| out-file $logfile -Append
     Read-Host "Removed Groups please check. Press enter to close window.Any groups not removed will show up in the window." 
    Last edited by mmoore; 17th December 2014, 09:48.

  • #2
    Re: output results to a file

    You can use Add-Content to append a text file. You can also redirect the command output by piping to Out-File.

    See if either of those work for you.

    Edit: have a look at lots of good info there
    https://www.google.com/?gws_rd=ssl#q=powershell+logging
    Last edited by JeremyW; 17th December 2014, 14:09.
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Re: output results to a file

      I tried google. That is why i posted here. I am having trouble since it is in a loop.

      Comment


      • #4
        Re: output results to a file

        OK, so if you were to use the Add-Content cmdlet it would look something like this:

        Code:
        $User = Read-Host   "Input TSO ID"
         $UserList = get-content .\RemoveUserFromGroups.txt
         $logfile = "c:\log.txt"
         Foreach ($Item in $UserList) {
         
         try{
             Remove-ADGroupMember -Identity $item -Members $User -Confirm:$false
                Add-Content -value "$item has been removed " -Path $logfile 
         
         
         }
         
          catch
             {
                 Add-Content -value "$item has not been removed"  -Path $logfile
             }
         
         
         }
        Read-Host "Removed Groups please check. Press enter to close window.Any groups not removed will show up in the window."
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment

        Working...
        X