Skip to main content

Posts

Showing posts from February, 2013

Displaying information on the host with PowerShell

PowerShell scripters have a variety of methods that they can use to display information on the host.  Before we begin this lesson, remember that PowerShell should send an object as it’s output to the PowerShell pipeline.  In many cases, the author of a cmdlet may want to display other information.  This can include debugging or progress information.  Here are a few cmdlets and some information to help you understand their usage. Write-Output This is the proper way of sending output into the PowerShell pipeline.  Remember that this should be the object that is the end result of the code that was just executed.  This is also how we return information from a function to its calling statement. Write-Host This is the easiest cmdlet to use to instantly display information on the host.  The – ForeGroundColor and – BackGroundColor parameters allow you to use 16 different colors to emphasis your displaying information.  Example: PS C:\> Write-Host "Hello World" -ForegroundC

How to list all members of a Distribution group and their emails with PowerShell

This question came from the need of a user who needed to extract the following information: Distribution Group Name | Group Email | Group Member | Member’s Email   Function Get-DistributionEmail {       # Get a list of all the distribution groups in the domain.     $Groups = Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' -Properties Mail       # Create a dynamic array to hold the objects for the output.     $Obj = @()         # Loop through each group.     ForEach ( $Group in $Groups )     {         # Enumerate all the members of the group.         $Members = Get-ADGroupMember -Identity $Group . name                 # Collect the data for the output.         ForEach ( $Member in $Members )         {             $GroupObj = New-Object PSObject             $GroupObj | Add-Member `              -MemberType NoteProperty `              -Name "Group" `              -Value $Group . Name               

List all the scripts my GPOs run.

This is an interesting question that I picked up from my moderator duties on PowerShell.com.  The question was how do I know what scripts are being run by my GPOs?  The function below will enumerate all of your GPOs and then let you know what scripts are being run.       Function Get-GPOScripts {     $GPOS = Get-GPO -all     ForEach ( $GPO in $GPOs )     {             $Obj = New-Object -TypeName PSOBject         $Obj | Add-Member -MemberType NoteProperty -Name "GPO" -Value $GPO . Displayname           [ xml ] $xml = Get-GPOReport -Name $GPO . DisplayName -ReportType xml           $User = $xml . documentelement . user . extensiondata . extension . script . command         $computer = $xml . documentelement . Computer . extensiondata . extension . script . command           $Incriment = 1         $UserScript = @()         ForEach ( $U in $User )         {             $US = New-Object -TypeName PSObject             $S