July 19th, 2011

Use the BSonPoSH PowerShell Module to Simplify AD Tasks

Doctor Scripto
Scripter

Summary: Learn how to use the BSonPoSH Windows PowerShell module to simplify Active Directory management tasks.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I am wondering if there are any modules that can help me with routine, daily administration tasks. I really hate to re-invent the wheel, so if you can point the way to a nice, juicy module, I would be most appreciative.

—OL

 

Hey, Scripting Guy! AnswerHello OL,

Microsoft Scripting Guy Ed Wilson here. Well, the days are counting down to the Scripting Wife and my road trip. We will be in Columbus, Ohio, for the Central Ohio PowerShell Users Group meeting on July 21, 2011. They have announced a location for this meeting, so make sure you check it out. On Saturday July 23, 2011, we will be at the SQL Saturday in Wheeling, West Virginia, so there is plenty of fun this week. Check out the Scripting Guys Community page for upcoming live and virtual events. 

OL, speaking of community, I think I have just the thing. Microsoft Windows PowerShell MVP Brandon Shell (a.k.a. BSonPoSH) has created a really cool module named, appropriately enough, the BSonPoSH module. Not all community-developed modules are on CodePlex. Some are on MSDN’s Code Gallery, some are on the Scripting Guys Script Repository, some are on PoshCode, and others are stored on individuals’ blogs. This plethora of locations is simply endemic of the richness of the community. Brandon has links to his module, which is stored on the MSDN Code Gallery, from his blog and CodePlex.

After I download the .zip file, unblock it in Explorer, expand the files, and copy them to the appropriate location, I can begin to work with the module using standard module techniques.

For more information about working with modules refer to articles referenced by the modules tag on the Hey, Scripting Guy! Blog.

One thing that might be a bit confusing is that there are multiple modules contained in the BSonPoSH module. After I have installed the module, I like to use the Get-Module command with the listavailable switch. When I do, I see several modules that are supplied by the BSonPoSH module. The command and associated output are shown here.

Image of command and associated output

The module that is supplied by the manifest is the root module, and the one that loads the other modules and commands. To load the root module, I use the Load-Module cmdlet and provide it with the name of the module to load. Normally, I use wildcard characters when loading modules, but because all the modules begin with the letters BS, I decide to use the full module name to ensure that the proper module loads. The command is shown here:

Import-Module bsonposh

By default, no output appears when importing a module. If for some reason I decide I need to see exactly what is happening, I can use the verbose switch. The command and associated output are shown in the following figure.

Image of command and associated output

I like to use the Get-Command cmdlet (gcm is an alias) to see which commands are available from the newly imported module. With the BSonPosh module, I have lots of commands to choose from. I decide to send the output to the Format-Table cmdlet (ft is an alias) so that I have a nice list of the commands.

PS C:\> gcm -Module bsonposh | ft name, commandtype -AutoSize

 

Name                                                   CommandType

Add-Accelerator                                                Function

Add-SharePermission                             Function

Block-Script                                           Function

ConvertFrom-BinaryIP                             Function

ConvertFrom-MaskLength                       Function

ConvertFrom-ShareReturnCode               Function

ConvertTo-BinaryIP                                 Function

ConvertTo-DistinguishedName                Function

ConvertTo-DNSName                             Function

ConvertTo-KMSStatus                             Function

ConvertTo-MaskLength                           Function

ConvertTo-Name                                   Function

ConvertTo-NetbiosName                        Function

ConvertTo-Sid                                       Function

ConvertTo-UACFLag                               Function

Copy-Function                                       Function

Disable-PagingFileAutoManage               Function

Disable-RemoteDesktop                          Function

EditHosts                                               Function

Enable-PagingFileAutoManage                Function

Enable-RemoteDesktop                          Function

Export-Credential                                   Function

Format-XML                                          Function

Get-Accelerator                                     Function

Get-ADACL                                            Function

Get-AlternateDataStream                        Function

Get-Assemblies                                      Function

Get-Bios                                                Function

Get-BSonPosh                                        Function

Get-ComputerDomainInfo                       Function

Get-ComputerSystem                             Function

Get-Constructor                                     Function

Get-CPUUsage                                       Function

Get-DCConnectionObject                        Function

Get-DiskPartition                                    Function

Get-DiskSpace                                       Function

Get-Domain                                           Function

Get-DomainController                            Function

Get-Environment                                    Function

Get-FileMD5                                          Function

Get-Forest                                             Function

Get-FSMO                                             Function

Get-GlobalCatalog                                 Function

Get-HyperVSetting                                 Function

Get-InstalledSoftware                             Function

Get-IP                                                   Function

Get-IPRange                                          Function

Get-KMSActivationDetail             Function

Get-KMSServer                                      Function

Get-KMSStatus                                       Function

Get-LocalGroupMember                         Function

Get-MemoryConfiguration                      Function

Get-MotherBoard                                  Function

Get-NetStat                                           Function

Get-Network                                          Cmdlet

Get-NetworkAddress                              Function

Get-NetworkLocation                             Function

Get-NICInfo                                           Function

Get-OperatingSystem                             Function

Get-OSVersion                                       Function

Get-PageFile                                          Function

Get-PageFileSetting                                Function

Get-Processor                                        Function

Get-RebootTime                                    Function

Get-RegistryHive                                    Function

Get-RegistryKey                                     Function

Get-RegistryValue                                   Function

Get-RemoteDesktop                               Function

Get-RemoteSession                                Function

Get-Routetable                                      Function

Get-Schema                                           Function

Get-SchemaClass                                    Function

Get-SchemaOID                                     Function

Get-SchemaProperty                              Function

Get-Share                                              Function

Get-Site                                                 Function

Get-SiteLink                                           Function

Get-SysInternals                                     Function

Get-SystemType                                     Function

Get-TraceFile                                         Function

Get-Uptime                                           Function

Get-WindowsExperienceIndex                 Function

Get-WindowsUpdate                              Function

Import-Credential                                  Function

Invoke-PingMonitor                                Function

Invoke-uac                                             Function

Invoke-WhoAmI                                     Function

New-ADACE                                          Function

New-AlternateDataStream                      Function

New-LocalGroupMember                       Function

New-PInvoke                                         Function

New-RegistryKey                                    Function

New-RegistryValue                                 Function

New-Share                                            Function

New-ShareObject                                   Function

New-TempFile                                       Function

Out-Voice                                              Function

Pig                                                        Function

Ping-Subnet                                           Function

Png                                                       Function

Remove-Accelerator                               Function

Remove-LocalGroupMember                  Function

Remove-RegistryKey                               Function

Remove-RegistryValue                            Function

Remove-Share                                       Function

Remove-SharePermission                        Function

Resolve-Host                                          Function

Restart-RemoteService                            Function

Search-Registry                                      Function

Select-LargeFile                                     Function

Send-FTP                                               Function

Set-ADACL                                             Function

Set-HyperVSetting                                  Function

Set-Network                                          Cmdlet

Set-NetworkLocation                              Function

Set-PageFileSetting                                Function

Set-RegistryValue                                   Function

Set-ServiceAccount                                 Function

Set-Share                                              Function

Set-SharePermission                               Function

Start-RemoteService                               Function

Stop-IE                                                  Function

Stop-RemoteProcess                              Function

Stop-RemoteService                               Function

Test-ADReplication                                 Function

Test-Host                                               Function

Test-Is64Bit                                            Function

Test-KMSIsActivated                               Function

Test-KMSServerDiscovery                       Function

Test-KMSSupport                                   Function

Test-LdapFilter                                       Function

Test-Port                                               Function

Test-PSRemoting                                    Function

Test-RegistryKey                         Function

Test-RegistryValue                                  Function

Test-Service                                           Function

Trace-LDAP                                           Function

TS                                                         Function

update-Gac                                           Function

Update-GPO                                          Function

Wait-Port                                              Function

 

If I want to see what kinds of things I can get using the module, I modify the previous command and tell it to give me only commands including the verb Get. This revised command and associated output are shown here.

PS C:\> gcm -Module bsonposh -verb get | ft name, commandtype -AutoSize

 

Name                                                   CommandType

Get-Accelerator                                     Function

Get-ADACL                                            Function

Get-AlternateDataStream                        Function

Get-Assemblies                                      Function

Get-Bios                                                Function

Get-BSonPosh                                        Function

Get-ComputerDomainInfo                       Function

Get-ComputerSystem                             Function

Get-Constructor                                     Function

Get-CPUUsage                                       Function

Get-DCConnectionObject                        Function

Get-DiskPartition                                    Function

Get-DiskSpace                                       Function

Get-Domain                                           Function

Get-DomainController                            Function

Get-Environment                                    Function

Get-FileMD5                                          Function

Get-Forest                                             Function

Get-FSMO                                             Function

Get-GlobalCatalog                                 Function

Get-HyperVSetting                                 Function

Get-InstalledSoftware                             Function

Get-IP                                                   Function

Get-IPRange                                          Function

Get-KMSActivationDetail             Function

Get-KMSServer                                      Function

Get-KMSStatus                                       Function

Get-LocalGroupMember                         Function

Get-MemoryConfiguration                      Function

Get-MotherBoard                                  Function

Get-NetStat                                           Function

Get-Network                                          Cmdlet

Get-NetworkAddress                              Function

Get-NetworkLocation                             Function

Get-NICInfo                                           Function

Get-OperatingSystem                             Function

Get-OSVersion                                       Function

Get-PageFile                                          Function

Get-PageFileSetting                                Function

Get-Processor                                        Function

Get-RebootTime                                    Function

Get-RegistryHive                                    Function

Get-RegistryKey                                     Function

Get-RegistryValue                                   Function

Get-RemoteDesktop                               Function

Get-RemoteSession                                Function

Get-Routetable                                      Function

Get-Schema                                           Function

Get-SchemaClass                                    Function

Get-SchemaOID                                     Function

Get-SchemaProperty                              Function

Get-Share                                              Function

Get-Site                                                 Function

Get-SiteLink                                           Function

Get-SysInternals                                     Function

Get-SystemType                                     Function

Get-TraceFile                                         Function

Get-Uptime                                           Function

Get-WindowsExperienceIndex                 Function

Get-WindowsUpdate                              Function

 

Some of the commands are rather mundane, such as the Get-MotherBoard command:

PS C:\> Get-MotherBoard

 

ComputerName             Manufacturer                 Name               Version             SerialNumber

NEWMRED                    Intel Corporation           DP67DE             AAG10217-205   BTDE111000Z6

 

But there are some really cool commands in this module. For example, one of the things that is still a pain to figure out using the GUI tools is who the FSMO role owners are. This information is easily obtained using the Get-FSMO command. The command and associated output are shown here:

PS C:\> Get-FSMO

 

ComputerName             Role                                          Domain

dc1.iammred.net            PDCMaster                                iammred.net

dc1.iammred.net            RIDMaster                                 iammred.net

dc1.iammred.net            InfrastructureMaster                   iammred.net

dc1.iammred.net            SchemaMaster                           iammred.net

dc1.iammred.net            DomainNamingMaster               iammred.net

 

The commands allow me to use the pipeline and format the output in a different manner. For example, I can pipe the output from the Get-ComputerDomainInfo command to the Format-List (fl is the alias) cmdlet and use the asterisk wildcard character to choose all the properties. This command and associated output are shown here:

PS C:\> Get-ComputerDomainInfo | fl *

 

DCSite                           : Default-First-Site-Name

Domain                         : IAMMRED

Forest                           : iammred.net

ComputerName             : NEWMRED

CurrentDC                     : \\DC3

ClientSite                       : Default-First-Site-Name

 

OL, that is all there is to using the BSonPoSH module. Community Week will continue tomorrow.

 

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.