Dimitri Koens on February 15th, 2012

When you’re using the PowerShell module for Hyper-V from James O’Neill (a.k.a. jamesone) then you’ll find out that many cmdlets don’t accept string collections as variable. Here’s an example:

Import-Module c:\psmodules\HyperV\HyperV.psd1
$Hosts = "HV01,HV02,HV03,HV04"
Stop-VM -VM VM03 -Server $HostsString -Force

Throws this error:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\psmodules\HyperV\VM.ps1:73 char:26
+             Get-WmiObject <<<<  -computername $Server -NameSpace $HyperVNamespace -Query $WQL | Add-Member -MemberType ALIASPROPERTY -Name “VMElementName” -Value “ElementName” -PassThru
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The module doesn’t interpret the $HostsString as a collection.

Here’s a possible solution:

Stop-VM -VM VM03 -Server $HostsString.split(",") –Force

What I’ve done is include a split-method to split the string and return it as a collection. Now the command works!

 

Dimitri Koens on January 23rd, 2012

Studying for SQL exam 70-432? Make sure you take training 6431 AND 6432. The Microsoft website doesn’t mention the 6432 training:

http://www.microsoft.com/learning/en/us/exam.aspx?id=70-432#tab3

When you take a look at the learning plan (mentioned on the same webpage) you will find that Microsoft recommends both 6431 and 6432 courses.

The 6432 training contains information on partitioning, triggers, stored procedures, etc. If you’re only following the 6431 training you’ll miss a lot of valuable information to pass the exam!

You can also prepare for the exam by studying this book: MCTS Self-Paced Training Kit (Exam 70-432): Microsoft® SQL Server® 2008 – Implementation and Maintenance. This should contain all the information required to pass the exam.

Good luck!

Dimitri

Dimitri Koens on January 11th, 2012

With DPM 2010 you can create Bare Metal Restore (BMR) backups next to System State backups. These backups can become inconsistent more often than regular backups. This can be because there’s not enough space on the DPM server to provide a BMR backup.

It’s possible to run a test backup so you can see how large a BMR backup actually is. It also tells you what volumes are included in the backup, so you can confirm whether the correct volumes are being backed up (for instance: a D-drive with only a huge pagefile will not be backed up).

Perform the following command on the server you are protecting:

wbadmin.exe start backup -allcritical -backuptarget:\\otherserver\share

Replace otherserver with the name of a server that you can write a test backup to. This is not the server you are protecting. Now confirm the included volumes before starting the actual backup. Start the backup and measure the size of the backup as soon as it’s done. Make sure you have this disk space on the volume of the DPM server.

Don’t forget to enable the Windows Server Backup feature on all Windows Server 2008 and later servers you’re protecting with DPM! This is something the DPM Agent doesn’t do for you but it’s required for a BMR backup.

Dimitri Koens on December 6th, 2011

With the built-in Best Practices Analyzer we can run several tests and implement any out comings. The BPA is incorporated in the Windows Operating System since Windows Server 2008. With PowerShell we can run a BPA-scan, store it as a baseline and compare it with our current situation. Here is a PowerShell script to establish the baseline:

$BpaModel = "Microsoft/Windows/WebServer"
$BaselineFile = "baseline.xml"
Import-Module BestPractices
Invoke-BpaModel $BpaModel
Get-BpaResult $BpaModel | Export-CliXML $BaselineFile

And using this script we can compare the current situation with our baseline:

$BpaModel = "Microsoft/Windows/WebServer"
$BaselineFile = "baseline.xml"
Import-Module BestPractices
Invoke-BpaModel $BpaModel
$Bpa = Get-BpaResult $BpaModel
$BpaBaseline = Import-CliXML $BaselineFile
Compare-Object $BpaBaseline $Bpa -property Severity, Title, Resolution |
     Where { $_.SideIndicator -eq "=>" }

You can replace the first line of both script with the name of your model, for example: Microsoft/Windows/DNSServer. You can query for all the installed BPA models using Get-BpaModel. Popular Best Practices that are included with Windows are: Active Directory, DNS and IIS.

I can recommend you schedule the second script every week or so, and e-mail the results as soon as a change is detected.

Dimitri

Dimitri Koens on November 25th, 2011

Hi all,

I just finished work on my all-new Hyper-V Quick Reference. It contains a lot of information I got when using and implementing Hyper-V and teaching it at Microsoft Learning Partners like Global Knowledge, Compu’Train, Twice, New Horizons and Centric. Although this document is best used as a quick reference in practice, it can also be used as a great preparation for Exam 70-659 TS: Windows Server 2008 R2, Server Virtualization.

Please send me a message and let me know what you think of the Hyper-V Quick Reference. You can contact me with questions, remarks, etc regarding this document. Join me on Linked in and Facebook to receive valuable information regarding PowerShell, SCOM, SQL Server and Virtualization.

Get the Hyper-V Quick Reference here: Hyper-V Quick Reference v2-00

Dimitri

Dimitri Koens on November 19th, 2011

It’s official! Since the start of my new website and blog I had 1024 visitors in the first month! Thanks to everyone that left a nice comment or sent me a message by e-mail or Linked In. A lot of people downloaded the PowerShell Quick Reference. I hope it helps them with their first PowerShell encouter.

Some statistics:

  • The PowerShell Quick Reference has been downloaded 500 times!
  • Bing is responsible for 30% of the visitors finding my website by a search engine (so Bing works!).
  • 30% of all the visitors is from the United States.
  • I had two visitors from Vietnam. I have been there only one time, so let’s hope I can even that one out, one time… ;-)

A lot more is coming: several other Quick References are in the making. You can expect a lot more blog posts and even videos. So maken sure you check out www.dimensionit.tv often.

Did you know you can subscribe to the RSS feed?

Hope to see you soon!

Dimitri

Dimitri Koens on November 17th, 2011

There are some situations where you want to boot a remote computer that’s powered off, but don’t have the tools to do that. For example: Microsoft SCCM allows you to boot a remote computer (wake it up from power off) to install Windows Updates. If you don’t have SCCM there are thousands of alternative software solutions, but we cannot trust most of them, right?

Look no further! We can power on remote computers through PowerShell! The script demonstrates how to remote boot a PC with a UDP packet we’re creating through the System.Net.Sockets.UdpClient programming interface (part of dot Net).

Just replace the MAC-address with the address of the computer you want to boot, start the script, and boot!

$MacAddress = [byte[]](0x00, 0x25, 0xB3, 0x0D, 0xA8, 0xF9)
$UDPclient = New-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 102)
6..101 | foreach { $packet[$_] = $MacAddress[($_%6)]}
$UDPclient.Send($packet, $packet.Length)
Dimitri Koens on November 16th, 2011

PowerShell provides some commands to manipulate services, but that doesn’t work on remote computers! The script you can find below demonstrates that stopping a service on a remote computer using Stop-Service is not working.

First I verify that the MSDTC service is running on the local machine and a remote machine: server2. If I want to stop the service on both machines I use the Stop-Service cmdlet. But this command doesn’t work on server2 when I’m piping the output from Get-Service to Stop-Service!  :-(

There are two solutions. The first is you can use the InputObject parameter as part of Stop-Service.

You can also use WMI to stop a remote service.  Just retreive the specific service from a remote computer by using the Get-WmiObject cmdlet and use the StopService method to stop the service.

WMI is intended to work remote so this command gives the correct result!  :-)

"Verifying services"
Get-Service MSDTC
Get-Service MSDTC -computer server2
"Stopping services"
Get-Service MSDTC | Stop-Service
Get-Service MSDTC -computer server2 | Stop-Service   # Stop-Service does not accept pipelined input!
"Verifying services."
Get-Service MSDTC
Get-Service MSDTC -computer server2
"Stopping remote service, alternative"
Stop-Service -InputObject (Get-Service MSDTC -ComputerName server2)
"Stopping remote service using WMI"
(Get-WmiObject win32_service -computer server2 | Where { $_.name -eq 'MSDTC' }).stopservice()

Thanks to Martin Tengvall for pointing out the -InputObject variant.

Dimitri Koens on November 15th, 2011

It’s possible to compress a folder or file since Windows NT. It’s a feature of the NTFS-filesystem. Normally we would do that on the properties of the folder or file in the Windows Explorer, but you can do this with PowerShell too! Just use this command:

Invoke-WmiMethod -Path "Win32_Directory.Name='C:\Test'" -Name compress

And to Uncompress the same folder, use this command:

Invoke-WmiMethod -Path "Win32_Directory.Name='C:\Test'" -Name uncompress

We’re talking about NTFS compression here, not the ZIP-feature introduced in Windows XP and later. You can notice that the folder is compressed by the blue color of the folder in Windows Explorer. Or you can open the properties of the folder to see whether the size on disk is smaller than the normal size. Do not try to compress items that are already compressed, like pictures (JPG…), music (MP3…), video (AVI, MPG…).

When you use WMI to execute operating system functions like this, you get a return value. When the return value is 0 (zero) this means the command functioned. Other return values can mean different things (read-only, no disk space, no permissions,…). In general: when we’re executing WMI-commands we’re allways checking the return value. You could implement it like this:

$a = Invoke-WmiMethod -Path "Win32_Directory.Name='C:\Test'" -Name compress
If ($a.returnvalue) -eq 0 { "Items successfully compressed" } else { "Something went wrong!" }

 

Dimitri Koens on November 13th, 2011

The other day I stumbled upon a nice PowerShell command to use the speech API found on modern Windows Operating Systems. With this script PowerShell can talk to you! Great for situations where you want to hear about the progress of your script. Or maybe to tell you there’s an alert instead of showing the alert. Here’s the script:

 

$a = New-Object -COM SAPI.SpVoice
$a.speak("I'm completely operational, and all my circuits are functioning perfectly.")
Dimitri Koens on November 10th, 2011

Not too long ago I migrated my e-mail, calendar and contacts to the cloud. The migration was very smooth except for one thing. For some unknown reason a reminder was added to all my all-day appointments. I use these appointments for birthdays and that sort of events. I never use a reminder on my all-day appointments but after the migration all these appointments were set with a reminder of 15 minutes before the appointment. A daily appointment starts at 0:00 and lasts until 24:00. So that means my phone urged me to wake up at 23:45 so I didn’t forget my sisters birthday!

The next morning I realised what went wrong and that I could expect a lot of nightly wakeup calls if I didn’t remove the reminders. I have a lot of daily events and was wondering if PowerShell was able to build a list of all-day events with a reminder set.

The following script uses the Outlook COM-object to retreive all your events. It selects all-day events that have a reminder set. Please note: in most cases Outlook will show a pop-up or maybe a pop-under where you’re asked for access to Outlook from PowerShell.

 

# Create the outlook application object, and connect to the calendar folder
$olApp = New-Object -COM Outlook.Application
$namespace = $olApp.GetNamespace("MAPI")
$fldCalendar = $namespace.GetDefaultFolder(9)
"Retreiving calendar items"
$items = $fldCalendar.Items
$items | Where { $_.reminderset -eq $true -and $_.alldayevent -eq $true -and $_.isrecurring -eq $true } |
Sort Start | Format-Table Start, Subject -auto | out-file .\agenda-reminders.txt

 

Dimitri Koens on November 9th, 2011

One of my clients asked me how to check for expired certificates. This simple script opens the certificate store through the PS-drive CERT: and lists all certificates that are soon to expire. You can change the threshold to any value in the first line. Here’s the script:

$threshold = 30   #Number of days to look for expiring certificates
$deadline = (Get-Date).AddDays($threshold)   #Set deadline date
Dir CERT:\LocalMachine\My | foreach {
If ($_.NotAfter -le $deadline) { $_ | Select Issuer, Subject, NotAfter, @{Label="Expires In (Days)";Expression={($_.NotAfter - (Get-Date)).Days}} }
}

Dimitri

Dimitri Koens on October 19th, 2011

Hi All,

Celebrating my first post on my new web site, let’s provide you with my ultimate PowerShell Quick Reference! I just finished work on my latest version of the PowerShell Quick Reference. You can grab a copy and use it as a reference when creating your PowerShell scripts. The document contains a lot of information I got when using PowerShell myself and teaching PowerShell at Microsoft Learning partners sites like Global Knowledge, Compu’Train, Twice, New Horizons and Centric.

Click here: PowerShell Quick Reference Dimension IT v2.9

Please send me a message and let me know what you think of the PowerShell Quick Reference.

Dimitri

Tags: ,