PowerShell DSC: Get Product IDs of Installed Products

The PowerShell DSC Package resource requires a ProductId and the exact product name.

If you aren’t sure what product id or product name associated with the software you wish to install, you can try this method:

  1. Install the software manually on a similar target machine
  2. Open a PowerShell command prompt or the PowerShell ISE
  3. Run the following command:
            Get-WmiObject Win32_Product

This command lists most of the installed software on the system. Find your target and record the ProductId and Name. They will need to match your DSC configuration exactly. Next, in your PowerShell DSC configuration, update the Package resource with the values you’ve discovered, e.g.

Package InstallSomeSoftware
  Ensure = "Present"  
  Path  = "D:\DSC\SomeSoftware_v4.4.msi"
  Name = "Some Software"
  ProductId = "ad9b6775-28d2-4ef4-b2e3-58941ea51c26"
  Arguments = "/qn"
  DependsOn = "[Script]DownloadSomeSoftware"

Also note that the Arguments should be whatever your software package requires to run a “silent install.”

Hope this helps!

Posted in PowerShell, PowerShell DSC | Leave a comment

Azure: Run Windows Custom Script Extension with VM Credentials

By default, the Windows Custom Script Extension in Azure will run as Local System.

Here’s how you can pass your VM credentials from your Azure Resource Manager (ARM) template to the custom script extension and ensure they are not easily visible.

First, in your ARM template’s definition of the Custom Script Extension resource, ensure “commandToExecute” is defined in the “protectedSettings” section of the properties.

Note how the username and password of the VM creation are being passed to the command. The admin password is wrapped in double quotes to ensure special characters are escaped correctly on the command line.

"protectedSettings": { 
  "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File CustomScripts/MyCustomScriptExtension.ps1 -vmAdminUsername ', parameters('adminUsername'), ' -vmAdminPassword \"', parameters('adminPassword'), '\"')]"

Next, in the custom script referenced by the “commandToExecute” above, you use Invoke-Command to run as the user you provided to the script.

# MyCustomScriptExtension.ps1
param (
$password =  ConvertTo-SecureString $vmAdminPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential("$env:USERDOMAIN\$vmAdminUsername", $password)
Write-Verbose -Verbose "Entering Custom Script Extension..."
Invoke-Command -Credential $credential -ComputerName $env:COMPUTERNAME -ArgumentList $PSScriptRoot -ScriptBlock {
  # Elevated custom scripts go here 
  Write-Verbose -Verbose "Entering Elevated Custom Script Commands..."

Note that you can still expose your sensitive information to the execution context if you use elements like Start-Transcript in the script wrapper as it will dump the full command line in clear text to the log.

Hope this helps!

Posted in Azure, PowerShell | Leave a comment

Assign a Drive Letter from the Command Line Using DiskPart

Here’s how you can assign a drive letter to a volume using the command line tool DiskPart. For more information, see this TechNet article.

list volume
select volume n
assign letter F

Hope this helps!

Posted in Batch Files, Windows, Windows 7, Windows Server 2008 R2, Windows Server 2012 | Leave a comment

T-SQL: Trailing Spaces And String Comparison

Here’s a useful tidbit regarding trailing spaces and SQL Server queries. Note the count of the results from the query below. Because SQL Server follows the ANSI/ISO SQL-92 specification, the string ‘test’ and ‘test    ‘ (with spaces at the end) are treated as the same value; only the length will be different. See How SQL Server Compares Strings with Trailing Spaces for more information.

;WITH StringsToTest AS
  SELECT 'test       ' AS test
  SELECT 'test'
  SELECT '      test'
SELECT COUNT(test), test
FROM StringsToTest 

Hope this helps!

Posted in T-SQL | Leave a comment

Azure SDK Installation Issues

If you are trying to install the Azure SDK with the Web Platform Installer and the process gets “stuck” on step two, run the following from an administrative command prompt to remove and recreate the SQL localdb instance:

Delete the SQL instance:

sqllocaldb stop MSSQLLocalDB
sqllocaldb delete MSSQLLocalDB

If they exist, delete the following database files:


Recreate the SQL instance:

sqllocaldb start MSSQLLocalDB

Hope this helps!

Posted in Azure, PowerShell | Leave a comment

PowerShell: Add MIME Type to IIS Static Content Collection

Here’s a PowerShell snippet that will add a custom header to the response from a designated website using the Add-WebConfigurationProperty cmdlet:

Import-Module WebAdministration
Add-WebConfigurationProperty //staticContent -name collection -value @{fileExtension='.woff2'; mimeType='application/x-font-woff'}

The Add-WebConfigurationProperty cmdlet is part of the IIS WebAdministration module.

Hope this helps!

Posted in PowerShell | Leave a comment