Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

modify script to write to a file instead of host

Posted on 2011-09-30
7
Medium Priority
?
304 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 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Screencast - Getting to Know the Pipeline

971 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