Category Archives: Microsoft

Today I got a request from a colleague of mine that was doing inventory: What are the make, model, serial number and purchase date of your monitors? Seeing as this wasn’t something I had readily in my head I had to figure it out, but being the automation enthusiast that I am, I refused to bend over my monitors and snap a photo of it all. I wanted to find it a cooler way, the powershell way 🙂 This information is stored in WMI, so all I had to do was to grab it using Get-WmiObject and then format it rather nicely for him. The powershell script I came up with isn’t my most beautiful work, but here it is: $progress = 1 foreach ($monitor in (Get-WmiObject WmiMonitorID -Namespace root\wmi)) { Write-Host “Monitor #$($progress):” -ForegroundColor Green Write-Host “Manufactur: $(($monitor.ManufacturerName | ForEach-Object {[char]$_}) -join ”)” Write-Host “PN: ” ($($monitor.UserFriendlyName | ForEach-Object…

Read more

A tweet from @JanEgilRing caught my eye this morning, it was showing how you can use powershell to create passwords. The link in the tweet pointed here: Seeing that line and realizing how simple it was, it got me thinking on how I could implement this in my scripts. The only issue I saw with that one-liner was that the passwords it creates do not necessarily comply with high complexity rules. So, how can we approve on this? Firstly, we need to create a regex that we can use to validate that the password created complies with our rules. In our environment this means 12 characters, uppercase, lowercase and either a number or special character. The regex I ended up with is this one: ^.*(?=.{12,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$ (which I found here: ) Now that we have our regex we can simple throw the one-liner into a while loop: while ($pass -notmatch “^.*(?=.{12,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$”) { $pass = -join (‘abcdefghkmnrstuvwxyzABCDEFGHKLMNPRSTUVWXYZ23456789$%&*#’.ToCharArray() |…

Read more

If you are using a KMS server for activating servers and clients in your environment, you may have noticed that there’s really no obvious way to get a list of all the clients that have been activated by the KMS server. One way to get that overview is by using VAMT (, but since that tool is based on pulling info from clients and not from the KMS server it is not suitable for everyone. Thankfully, there’s PowerShell 🙂 Getting a list of all activated KMS clients through PowerShell is actually a simple one-liner: $(foreach ($entry in (Get-EventLog -Logname “Key Management Service”)) {$entry.ReplacementStrings[3]}) | sort-object -Unique What this does is look through the Key Management Service eventlog, grab only the client name and then remove all duplicates (since a client activates itself at regular intervals, there will be duplicates).

A little while ago we have some issues with one of our Exchange environments. The symptoms was that basically everything was slow, opening address books, owa, sending mails and so on. After some digging it occurred to me that this may be DNS related, so I used a one-liner in powershell to see how fast our DNS servers responded: Measure-Command {Resolve-DnsName -DnsOnly -Type A -NoHostsFile -server x.x.x.x} And sure enough, we were seeing response times in the range of 5-7 seconds. Way more than enough to cause our problems. After rebooting the DNS servers ours problems disappeared. But, as always, I had to play around with created a powershell function. The function resolves a few records from locally defined DNS servers and some publicly available DNS servers (or a custom list if you want) and measures the average latency (in milliseconds). The results are put in a list and…

Read more

The option for activating Server 2012 by phone is kinda hidden, so I have to write this down before I forget it: Win+R – SLUI 4

For whatever reason, I had a really difficult time wrapping my head around the usage of Write-Progress. The basic usage is off-course quite easy: Write-Progress -Activity “Something” -Status “Something else” -CurrentOperation “thinking” -PercentComplete 30 This will show a quick progress bar stating that “Something” is going on, the status is “Something else” and it has gotten to 30%. However, when I wanted to use Write-Progress in a large script I quickly got confused when I looked at some of the examples out there. I mean, look at this for example: $wmiQuery = “Select name from win32_service where state = ‘running'” $colItems = Get-WmiObject -Query $wmiQuery For($i = 1; $i -le $colItems.count; $i++) { Write-Progress -Activity “Gathering Services” -status “Found Service $i” ` -percentComplete ($i / $colItems.count*100)} $colItems | Select name I’m sure that if I had a stronger background in programming and/or scripting I could have figured it out quite…

Read more

I stumbled upon this brilliant powershell function for playing sounds using powershell, check it out: He even added a switch for playing the imperial march from Star Wars 🙂    

Today I was presented with a task that sounded pretty boring and repetitive: Install the Windows Search service on all our citrix servers. Now, I could just log on each and every one of our citrix servers, open Server Manager and then install the role service but where’s the fun in that? As always Powershell is a lot more fun: First I had to find the name of all our citrix servers. Luckily we have a strict naming convention so I could just poll Active Directory and store the names in an array: $servers = Get-ADComputer -Filter ‘Name -like “ctx_srv*”‘ | select name Next I only had to create a foreach loop that enters a PSSession on each of the servers and install the role service: $servers | ForEach-Object { Enter-PSSession -ComputerName $ import-module servermanager Add-WindowsFeature FS-Search-Service Exit-PSSession } It is worth mentioning that you can’t connect to remote servers using…

Read more

The Export-Csv cmdlet is a really nice tool if you want to gather info from e.g. Active Directory that you later want to import to Excel. But as you might have noticed it doesn’t play well with non-English letters. Working in Norway with users from all the Nordic countries means that we have a lot of users i Active Directory who have non-English letters in their names (æ ø ĂĄ ö ä and so on). Unless you specify which encoding Export-Csv should use they will show in the csv as ?. The magical switch to Export-Csv is -encoding. E.g. for us we would use unicode as encoding as this will export the Nordic letters: Export-Csv <filename> -Encoding unicode            

If you ever had problems with corrupt databases in an Exchange 2010 DAG setup you might have encountered this error. Trying to activate the database on a server where it has this state will fail. However there is a quite simple fix given that you have a healthy copy of the database on a different server: Update-MailboxDatabaseCopy -Identity DB<server with failed content index state> -SourceServer <server with healty copy> -CatalogOnly Run the command in the Exchange Management Shell and it should fix the Content index state and allow you to activate the database again.