Link to home
Start Free TrialLog in
Avatar of annayeg
annayeg

asked on

modify script to write to a file instead of host

Hi all,

I found this script that works perfectly for what i need to do.  I can't figure out how to write it to a file rather than on the screen.

param($ParentGroupNames)

$Global:myCol = @()

function Indent
      {
      param([Int]$Level)
      $Global:Indent = $null
      For ($x = 1 ; $x -le $Level ; $x++)
            {
            $Global:Indent += "`t"
            }
      }

function Get-MySubGroupMembersRecursive
      {
      param($DNs)
      ForEach ($DN in $DNs)
            {
            $Object = Get-QADObject $DN
            If ($Object.Type -eq "Group")
                  {
                  $i++
                  Indent $i
                  Write-Host ("{0}{1}" -f $Indent,$Object.DisplayName) -ForegroundColor "yellow"
                  $Group = Get-QADGroup $DN
                  If ($Group.Members.Length -ge 1)
                        {
                        Get-MySubGroupMembersRecursive $Group.Members
                        }
                  $i--
                  Indent $i
                  Clear-Variable Group -ErrorAction SilentlyContinue
                  }
            Else
                  {
                  $userfound = Get-QADUser $DN | Select Name, Email
                  Write-Host ("{0} {1}" -f $Indent,$userfound.Name)
                  $Global:myCol += $userfound
                  Clear-Variable userfound -ErrorAction SilentlyContinue
                  }
            }
      }

ForEach ($ParentGroupName in $ParentGroupNames)
      {
      $Global:Indent = $null
      $ParentGroup = Get-QADGroup -Name $ParentGroupName
      Write-Host "====================="
      Write-Host " TREE VIEW PER GROUP"
      Write-Host "====================="
      Write-Host ("{0}" -f $ParentGroup.DisplayName) -ForegroundColor "yellow"
      If ($ParentGroup -eq $null)
            {
            Write-Warning "Group $ParentGroupName not found."
            break
            }
      Else
            {
            $FirstMembers = $ParentGroup.Members
            ForEach ($member in $firstmembers)
                  {
                  Get-MySubGroupMembersRecursive $member
                  }
            }
      }
Write-Host ""
Write-Host "====================="      
Write-Host " All Unique Members: "
Write-Host "====================="      
$myCol | Sort Name | Select Name, Email -Unique
 

Does anyone know how to do it.
Avatar of Brent Challis
Brent Challis
Flag of Australia image

Add
| Out-File filename to the end.

Here is a reference for the cmdlet:

http://technet.microsoft.com/en-us/library/ee176924.aspx
Avatar of annayeg
annayeg

ASKER

When I add the out-file filename to the end it only gives me the All unique Members.  It doesn't give me the nested groups and members of each group.  I am not an expert in powershell and I don't know how to change the logic.  Any idea?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Brent Challis
Brent Challis
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of annayeg

ASKER

Hi there,

When I run the script, I get the nameofthefile not found.
Here's how I run it:
.\get-nameofscript.ps1 filename.txt

I get a warning:  Group filename.txt not found
It needs to be called with two parameters to get the output to a file.  The first parameter is the ParentGroupNames as you had in your original script.  If you do not provide a second parameter the output should go tot the default output of Write-Host, i.e. the screen.  If you provide a second parameter it is assumed to be the name of the file you want the output to go to.
Avatar of annayeg

ASKER

Thank you sooo much.  Worked Perfectly.
Avatar of annayeg

ASKER

Worked Perfectly!