Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

modify script to write to a file instead of host

Posted on 2011-09-30
7
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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