Solved

modify script to write to a file instead of host

Posted on 2011-09-30
7
294 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:bchallis
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:
bchallis 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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:bchallis
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article will help you understand what HashTables are and how to use them in PowerShell.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now