You gotta love the PowerShell community! Found this little gem in my twitter feed (again) :-). Trevor Sullivan demonstrates how we can create custom intellisense for cmdlets if they haven’t been provided as yet. Great video! Trevor really does a great job explaining this.
The first thing that came to mind was Active Directory! I can’t tell you how often I needed the DistinguishedName of an OU. Now imagine having a dynamic list generated for you! No more errors, just select and you’re good to go! Excited??? I sure am!
Sometimes you need to limit your searchbase depending on you AD size. Let’s say I want to retrieve all users starting from a specific point
A simple typo will generate an error. Distinguished names are notorious for being lengthy…
Now the obvious AD cmdlets would be Get-ADUser,Get-ADGroup & Get-ADComputer. So that got me thinking , just how many AD cmdlets have SearchBase as a parameter?
So I can have Intellisense on all these cmdlets? Awesome!!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Intellisense completed the DistinguisedName on -SearchBase for me. No need to type it in, no errors, just select and go!
Here’s the result:
I’m sure you guys will find your own use for this… Thanks again Trevor for bring this to our attention! Good looking out for the community! Be sure to watch Trevor’s video for in depth explanation.
So here’s another spin on using Pester to validate operational readiness… 😉
Group policies can be pretty tricky! Troubleshooting can be a challenge. There might be even times that you start doubting yourself. Depending on the link order of your Policies, you might not get what you expected…
Operations is dynamic, things get moved around, enabled/disabled, blocked, name it and it’s bound to happen.
How about… some way to validate your GPOs Scope of Management! Once everything is working as it should, create a validation set you can verify later on. Trust me, I’ve been there… Using Pester will definitely give you that edge…
So I improvised a little on Ashley’s McGlone’s GPO Report and made a function Get-GPOsSoM. Just be sure to save it in the same folder as Domain-GPOSoM.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
So I recently blogged about Configuring DNS zones and resource records. While going through my twitter feed, I stumbled upon this little gem by Kevin Marquette. He recently did a session on Pester and uploaded his demo. If you’re interested in Pester (as you should) you should definitely check it out!
So one of the demos was a eureka moment for me: The Active.Directory.System.DC.tests.ps1!
Wait you can do that? I thought Pester was about unit Framework testing not about validating script output. So I can test if my script did what I expected it to do? (Pause to let that sink in). Well alrighty then!!! 😛
So I decided to give it a go for the DNS Configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
“Hey Irwin! Do you have a script to configure DNS autodiscover?” Eh, no not really… I’m assuming you’re doing something with PowerShell DNS cmdlets? 😉 And so begins another PowerShell journey…
My colleague Ofir is our Exchange guy.
“Ok so what exactly do you want to accomplish Ofir? I’m trying to automate registering some Resource records in DNS, but it isn’t working. I’d like to add some IPAddresses to a specific zone… “Ok let’s see what you’ve got!
“So when I use the cmdlet directly it works. When I use varaiables it doesn’t… Ideally the code should be re-usable…” Ofir’s words not mine… I’ll admit, I teared up a bit… Kids, they grow up so fast… Hehe…
I think we can do this…
So Ofir was using read-host to get ZoneName and IPvAddress. Ah! What a lovely opportunity to demonstrate params!
“Ok Ofir, instead of using read-host, we’re better off using parameters. Using [CmdletBinding()] gives you the possibility to use Write-Verbose, no extra charge!”
“Now all we need to do it run the appropriate cmdlet and we’re good!”
Add-DNSServerResourceRecordA @dnsSplat
Ok, so this got Ofir started… Mind you there’s no error handling or anything of that sort…
We did some tinkering on the fly and this was the end result:
<#
Author: I.C.A. Strachan
Version:
Version History:
Purpose: Create AutoDiscover Zone and add ResourceRecord
#>
[CmdletBinding()]
Param(
[string]$fqdn ='domain.local',
[string[]]$ServerIPAddress= @('192.168.1.4', '192.168.1.5')
)
BEGIN{
$dnsRRA = @{
Name = 'AutoDiscover'
Zonename = "autodiscover.$($fqdn)"
AllowUpdateAny = $true
TimeToLive = '01:00:00'
}
$dnsPZ = @{
Name = "autodiscover.$($fqdn)"
ReplicationScope = 'Forest'
DynamicUpdate = 'Secure'
}
Import-Module DNSServer -Verbose:$false
}
PROCESS{
#Only Add Zone is count is zero (doesn't exists)
If (@(Get-DnsServerZone $dnsPZ.name -ErrorAction SilentlyContinue ).Count -eq 0 ){
Write-Verbose "Creating DNS Zone: $($dnsPZ.name)"
Add-DnsServerPrimaryZone @dnsPZ
}
#Get string equivalent of all A records
$RR = Get-DnsServerResourceRecord -ZoneName autodiscover.domain.local -RRType A |
Out-String
$ServerIPAddress | ForEach-Object {
If (!$RR.Contains($_)){
Write-Verbose "Adding resource record $_ to $($dnsPZ.name)"
Add-DNSServerResourceRecordA @dnsRRA -IPv4Address $_
}
}
}
END{}
Ofir was quite happy! Nice! Another satisfied customer. So the other day asked him to send me the code for future reference…
This is what he sent me:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
What??? This wasn’t the code I expected! Turns out Ofir had a lot more he needed to configure. Just pointing him in the right direction was sufficient to generate this! Awesome! Give a man a fish… 😉
Go Ofir! It’s fun to see colleagues get excited about PowerShell. Sometimes all that’s needed is just a nudge in the right direction…