Solved

modify script to write to a file instead of host

Posted on 2011-09-30
7
297 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:annayeg
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Brent Challis
ID: 36895416
Add
| Out-File filename to the end.

Here is a reference for the cmdlet:

http://technet.microsoft.com/en-us/library/ee176924.aspx
0
 
LVL 1

Author Comment

by:annayeg
ID: 36905177
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
0
 
LVL 8

Accepted Solution

by:
Brent Challis earned 500 total points
ID: 36913523
The Out-File needs to be used where ever there is output.  I have modified the script to take a FileName parameter and if it is not passed in the output stays as per the original code, hopefully, where as if you provide the file name all the output will go to that file.  I have left the rest of the code as is.

I have not tested for the existence of the file.

As an aside, the use of Global for the scripts is not really needed as that places the values in the context of the host, a better approach would be to use Script or simply make sure that variables that you wish to access throughout the script are declared at the script level.
param($ParentGroupNames,[string]$FileName)

$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
                  if ([System.String]::IsNullOrEmpty($FileName))
                  {
                    Write-Host ("{0}{1}" -f $Indent,$Object.DisplayName) -ForegroundColor "yellow"
                  }
                  else
                  {
                    ("{0}{1}" -f $Indent,$Object.DisplayName) | Out-File $FileName -Append
                  }
                  $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
                  if ([System.String]::IsNullOrEmpty($FileName))
                  {
                    Write-Host ("{0} {1}" -f $Indent,$userfound.Name)
                  }
                  else
                  {
                    ("{0} {1}" -f $Indent,$userfound.Name) | Out-File $FileName -Append
                  }
                  $Global:myCol += $userfound
                  Clear-Variable userfound -ErrorAction SilentlyContinue
                  }
            }
      }

if ([System.String]::IsNullOrEmpty($FileName))
{
    Write-Host ""
}
else
{
    "" | Out-File $FileName
}

ForEach ($ParentGroupName in $ParentGroupNames)
      {
      $Global:Indent = $null
      $ParentGroup = Get-QADGroup -Name $ParentGroupName
      if ([System.String]::IsNullOrEmpty($FileName))
      {
          Write-Host "====================="
          Write-Host " TREE VIEW PER GROUP"
          Write-Host "====================="
          Write-Host ("{0}" -f $ParentGroup.DisplayName) -ForegroundColor "yellow"
      }
      else
      {
          "=====================" | Out-File $FileName -Append
          " TREE VIEW PER GROUP"  | Out-File $FileName -Append
          "=====================" | Out-File $FileName -Append
          ("{0}" -f $ParentGroup.DisplayName) | Out-File $FileName -Append
      }
      If ($ParentGroup -eq $null)
            {
            Write-Warning "Group $ParentGroupName not found."
            break
            }
      Else
            {
            $FirstMembers = $ParentGroup.Members
            ForEach ($member in $firstmembers)
                  {
                  Get-MySubGroupMembersRecursive $member
                  }
            }
      }
if ([System.String]::IsNullOrEmpty($FileName))
{      
    Write-Host "====================="      
    Write-Host " All Unique Members: "
    Write-Host "====================="      
}
else
{
    "=====================" | Out-File $FileName -Append    
    " All Unique Members: " | Out-File $FileName -Append
    "=====================" | Out-File $FileName -Append 
}

if ([System.String]::IsNullOrEmpty($FileName))
{
    $myCol | Sort Name | Select Name, Email -Unique
}
else
{
    $myCol | Sort Name | Select Name, Email -Unique | Out-File $FileName -Append
}

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:annayeg
ID: 36934292
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
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 36934802
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.
0
 
LVL 1

Author Comment

by:annayeg
ID: 36943977
Thank you sooo much.  Worked Perfectly.
0
 
LVL 1

Author Closing Comment

by:annayeg
ID: 36943984
Worked Perfectly!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question