Solved

modify script to write to a file instead of host

Posted on 2011-09-30
7
295 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

23 Experts available now in Live!

Get 1:1 Help Now