Posts RSS Comments RSS 253 Posts and 411 Comments till now

BSonPosh v1 Powershell Module finally ships!

Thanks to 12 hours of time in an airplane I was able to put the finishing touches on my BSonPosh Module.

I have added help for all my cmdlets and created format files.

Download Here: BSonPosh V1

[Update]:: I had to post an updated zip file as I was missing some KMS cmdlets. Thanks Jason for pointing that out!

IN Progress: AD replication test script

Almost done with my new and improved AD replication script. If you are interested in giving it a shot drop me a line via the contact form or just leave a comment.

NOTE: It does make a canary change in AD. It sets the value of wWWHomePage on the Users container to a datetime stamp.

BSonPosh Module goes Beta (2)

I released a private beta a little bit back but this is an open beta (and there is a whole bunch more in it.)

How to Install
————–

  • Download: BSonPosh Powershell Module
  • Extract ZIP to your module folder $Env:PSModulePath. I found it better use the global module path (although it requires admin rights.)
  • Import-module bsonposh

Report bugs (please)
—————————

  • via Discussions
  • via Wave located here http://bsonposh.com/bug-report-page ( requires Google wave account )
  • via Contact form http://bsonposh.com/contact-me

A little taste of the 88 cmdlets
——————————

  • Get-CPU
  • Get-DiskSpace
  • Get-LocalGroupMember
  • Get-OSVersion
  • Get-RebootTime
  • Get-Uptime
  • Get-WindowsUpdate
  • Get-SysInternals
  • Get-Routetable

Even added a whole suite of Registry functions
———————————————

  • Get-RegistryHive
  • Get/Set/New-RegistryKey
  • Get/Set/New-RegistryValue
  • Remove-RegistryKey
  • Remove-RegistryValue
  • Test-RegistryKey
  • Test-RegistryValue

Hope you find use for these and please provide feedback. Thanks!

Re-Awarded the Microsoft MVP for Powershell.

I was very happy to get the email today. We (MVPs) generally feel we do enough but we are always concerned that MS thinks so 🙂

I would also like to welcome “Mr. Antoine Habert” to the Powershell MVP ranks. I *think* he is the only new one and I don’t believe we lost any.

Need Powershell v2 feedback ASAP!

Jeffrey and party are trying their darnedest to get this thing out the door but they need your help.

Please download: XP and W2K3 Release Candidate Versions of PowerShell are Now Available for Download and provide feedback asap!

Some AD Functions for DCs and name conversion.

Below are some functions I have written or used recently that I don’t believe I shared before

Get-DC: Gets a DirectoryServices.ActiveDirectory.DomainController object by Name or Domain. If nothing is passed it gets a DC from the current domain

Get-DCConnectionObject: Gets the connection objects for the given DC. Default is all DCs

ConvertTo-Sid: Converts Name to SID.

ConvertTo-Name: Converts Sid to Name.

################################################################################
function Get-DC
{
    Param($Name,$Domain)
   
    if($Name)
    {
        $Context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("DirectoryServer",$Name)
        [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController($Context)
    }
    if($Domain)
    {
        $Context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain",$Domain)
        [System.DirectoryServices.ActiveDirectory.DomainController]::FindAll($Context)
    }
    if(!$Name -and !$Domain)
    {
        $DCName = ([adsi]"LDAP://rootDSE").dnsHostname.ToString()
        $Context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("DirectoryServer",$DCName)
        [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController($Context)
    }
}
################################################################################
function Get-DCConnectionObject
{
    Param($name = ".*")
    $Myforest = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    $MyDCs = $Myforest.Domains | foreach-object{$_.DomainControllers} | ?{$_.name -match $name}
    $MyDCs | %{$_.InboundConnections}
}
################################################################################
function ConvertTo-Sid($UserName,$domain = $env:Computername)
{
   $ID = New-Object System.Security.Principal.NTAccount($domain,$UserName)
   $SID = $ID.Translate([System.Security.Principal.SecurityIdentifier])
   $SID.Value
}
################################################################################
function ConvertTo-Name($sid)
{
   $ID = New-Object System.Security.Principal.SecurityIdentifier($sid)
   $User = $ID.Translate( [System.Security.Principal.NTAccount])
   $User.Value
}

Off to BriForum next week

I am off to Briforum next week. What vendors (found here) should I bang over the head for Powershell support?

blog: Avoid hardcoding in scripts. Here are some Simple discovery options in Powershell

When writing scripts I have always been a fan of making them as generic as possible. This may make the script a tad more complicated, but it allows it to be dynamic and also allows you to share these scripts between environments (i.e. Lab, QC, Production.) Basically we want to avoid hardcoding Domains, Domain Controllers, OUs, Containers, and site info.

Below I provide some simple examples of getting this information dynamically. This will allow you to discover the information instead of hardcoding it in the script.

To get forest information like Domains, Sites, ForestMode, RootDomain, and Forest masters you can use this:
  1. $Forest = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

To get Domain information like Domain Controllers, DomainMode, Domain Masters, and Forest Root.
  1. $Domain = [DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()

To get the current Site information for the local machine like Subnets, Sitelinks, Location, Bridgehead Servers, and Domain Controllers.
  1. $MySite = [DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()

With these variables you can find all the Active Directory infrastructure information you could possibly want.

Here are some more specific examples on how to use these variables:

To find all your Global Catalogs in the forest
  1. $Forest.GlobalCatalogs

To find all the Domain Controllers in the current domain
  1. $Domain.DomainControllers

To see what application partitions your forest has
  1. $forest.ApplicationPartitions

To see the forest roles
  1. $forest | select SchemaRoleOwner,NamingRoleOwner

To see the domain roles
  1. $domain | select PDCRoleOwner,RidRoleOwner,InfrastructureRoleOwner

To see the subnets in the current site
  1. $MySite.subnets

To see the bridgehead Servers
  1. $MySite.BridgeheadServers

blog: Creating Custom Objects with “Select-Object”

Once you grab a hold of the "Object" concept in Powershell it brings a whole new light to scripting. The power that is at your finger tips is basically limitless, but to harness this power you not only need to know how to use objects but also create them as well.

Here is my way of creating objects.
  1. $myobj = ""| select-Object Server,Result
IMO this is the simplest way to create custom objects.

Effectively what happens is you set your $myobj to a empty string and pipe to select-object. While this may seem odd, its actually quite useful because what select-object returns is a PSCustomObject with the properties that you specify. This allows you to create you object with defined properties that you can fill out later in one line.

The "proper" way to create a custom object is to do this.
  1. $myobj = new-object System.Object
  2. $myobj | add-member -membertype noteproperty -name Server -value $sname
  3. $myobj | add-member -membertype noteproperty -name Result -value $sResult
While this isn't that much more work... I think its harder to read (and more typing) so I go with this shortcut
  1. $myobj = "" | select-Object Server,Result
  2. $myobj.Server = $sname
  3. $myobj.Result = $sResult
I will give you one warning about select-object (not related, but while I'm on this subject.) select-object does Change the object type and therefore you lose methods and properties you may expect to be there. I have seen numerous post on the news groups that have this exact problem.

If you pipe Get-ChildItem to Get-member you get tons of methods and properties for both System.IO.FileInfo and System.IO.DirectoryInfo
  1. PS> get-childitem | gm
Now pipe it to select-object and do a Get-Member
  1. PS> (get-childitem | select-object Fullname,length) | gm
  2.  
  3. TypeName: System.Management.Automation.PSCustomObject
  4.  
  5. Name MemberType Definition
  6. ---- ---------- ----------
  7. Equals Method System.Boolean Equals(Object obj)
  8. GetHashCode Method System.Int32 GetHashCode()
  9. GetType Method System.Type GetType()
  10. ToString Method System.String ToString()
  11. FullName NoteProperty System.String FullName=C:WindowsSystem32409
  12. length NoteProperty length=null
Just be careful to remember this. I actually avoid using select-object to filter objects simply because of this. Instead... I just use the properties.

Setting SNMP configuration in VMWare using Powershell

I have a ton of respect for the VMWare guys and the incredibly useful set of cmdlets they provide (FOR FREE) found HERE, but man have I been banging my head against the wall trying to simply set the SNMP configuration on my ESX hosts.

To help ease the pain for the REST of the world here are some thoughts/gotchas.

– You must have the object return from Get-VMHostSNMP to use Set-VMHostSNMP or Test-VMHostSNMP. Simply providing the name will not work
– You cannot use the name with Get-VMHostSNMP. It appears you must connect to the actual ESX host (using connect-viserver) and then run Get-VMHostSNMP
– Once you “get” the VMHostSNMP object you can reuse without worrying about the connect-viserver command.
– You need to add a ReadOnlyCommunity to each host
– You need to Enable traps with the -enable:$true parameter
– TargetCommunity, TargetPort, TargetHost parameters require that you also pass -addtarget
– You need to enable SNMP on the ESX firewall

You may find the script below useful for getting a VMHostSNMP object for each ESXHost on your Virtual Center

Using the script below you can then do things like
$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter

To Add a Community
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -ReadOnlyCommunity “YourCommunity”}

To Enable
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -Enabled:$true }

To Add a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -TargetCommunity “YourCommunity” -AddTarget }

To Test
$SNMPHostObjects | %{ Test-VMHostSnmp $_ }

To Remove a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1” -removeTarget}

Next »