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:

%USERPROFILE%\AzureStorageEmulatorDb*.mdf
%USERPROFILE%\AzureStorageEmulatorDb*.ldf

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

PowerShell: Add Custom Header to IIS Website Responses

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 //system.webServer/httpProtocol/customHeaders  "IIS:\sites\MySuperWebsite" -AtIndex 0 -Name collection -Value @{name='Access-Control-Allow-Origin';value='*'}

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

Hope this helps!

Posted in PowerShell | Leave a comment

PowerShell: Allow File and Printer Sharing Through Windows Firewall

To allow file and printer sharing, you may need to open the Windows Firewall. Here’s a one line PowerShell script that allows file and printer sharing through the Windows Firewall by enabling the firewall rule group:

Enable-NetFirewallRule -Group "@FirewallAPI.dll,-28502" -Direction Inbound

To see a list of the available firewall groups, you can run the following:

Get-NetFirewallRule | Select-Object -Property Name, Group, Direction

Hope this helps!

Posted in PowerShell, Windows Server 2012 | Leave a comment

Microsoft SQL Server 2014: Change Default Data, Log, and Backup Directories via T-SQL or PowerShell

If you need to change the default data, log, and/or backup directories on a SQL 2014 MS SQL Server default instance after installation, you can use these three SQL commands:

EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer'
	, N'DefaultData'
	, REG_SZ
	, N'K:\SQL\Data'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer'
	, N'DefaultLog'
	, REG_SZ
	, N'L:\SQL\Logs'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer'
	, N'BackupDirectory'
	, REG_SZ
	, N'Z:\SQL\Backups'
GO

If you are using a named instance, you will want to replace the MSSQLSERVER with the instance name. For newer MS SQL Server versions, you will need to change the 12 to the correct version.

For earlier versions of SQL, you would use the following (Note the change in registry location):

EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\MSSQLServer\MSSQLServer'
	, N'DefaultData'
	, REG_SZ
	, N'K:\SQL\Data'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\MSSQLServer\MSSQLServer'
	, N'DefaultLog'
	, REG_SZ
	, N'L:\SQL\Logs'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
	, N'Software\Microsoft\MSSQLServer\MSSQLServer'
	, N'BackupDirectory'
	, REG_SZ
	, N'Z:\SQL\Backups'
GO

You can also do this via PowerShell:

$DataRegKeyPath = "HKLM:\Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer"
$DataRegKeyName = "DefaultData"
$DataDirectory = "K:\SQL\Data"
If ((Get-ItemProperty -Path $DataRegKeyPath -Name $DataRegKeyName -ErrorAction SilentlyContinue) -eq $null) {
  New-ItemProperty -Path $DataRegKeyPath -Name $DataRegKeyName -PropertyType String -Value $DataDirectory
} Else {
  Set-ItemProperty -Path $DataRegKeyPath -Name $DataRegKeyName -Value $DataDirectory
}
 
$LogRegKeyPath = "HKLM:\Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer"
$LogRegKeyName = "DefaultLog"
$LogDirectory = "L:\SQL\Logs"
If ((Get-ItemProperty -Path $LogRegKeyPath -Name $LogRegKeyName -ErrorAction SilentlyContinue) -eq $null) {
  New-ItemProperty -Path $LogRegKeyPath -Name $LogRegKeyName -PropertyType String -Value $LogDirectory
} Else {
  Set-ItemProperty -Path $LogRegKeyPath -Name $LogRegKeyName -Value $LogDirectory
}
 
$BackupRegKeyPath = "HKLM:\Software\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer"
$BackupRegKeyName = "BackupDirectory"
$BackupDirectory = "Z:\SQL\Backups"
If ((Get-ItemProperty -Path $BackupRegKeyPath -Name $BackupRegKeyName -ErrorAction SilentlyContinue) -eq $null) {
  New-ItemProperty -Path $BackupRegKeyPath -Name $BackupRegKeyName -PropertyType String -Value $BackupDirectory
} Else {
  Set-ItemProperty -Path $BackupRegKeyPath -Name $BackupRegKeyName -Value $BackupDirectory
}

Hope this helps!

Posted in MS SQL Server, PowerShell, T-SQL | Leave a comment

Azure: Troubleshooting PowerShell DSC Resources

The errors you get back from the PowerShell DSC extension in Azure can be frustratingly terse. Here’s a way to troubleshoot and try out PowerShell DSC changes without having to deploy the extension over and over again. After the extension fails, RDP to the machine and go through these steps to troubleshoot on the VM you just spun up:

  • Open the PowerShell DSC configuration file that was delivered by the Azure DSC extension in the PowerShell ISE, e.g. C:\Packages\Plugins\Microsoft.Powershell.DSC\2.20.0.0\DSCWork\PowerShellDSC.0\PowerShellDSC.ps1
  • Change your ISE location to a known folder, e.g. “cd C:\temp”
  • Save the file as localhost.ps1 into that folder, e.g. C:\temp\localhost.ps1
  • Change “Node $nodeName” to be “Node localhost”
  • At the bottom of the localhost.ps1 file, execute the configuration by calling the configuration, e.g. “Main”
  • Run the script within the PowerShell ISE by hitting the play button; this should create a localhost.mof file under a folder of the same name of your configuration, e.g. C:\temp\Main
  • Now you are set to troubleshoot the DSC resource with the Start-DscConfiguration cmdlet, e.g. “Start-DscConfiguration -Path .\Main -ComputerName localhost -Wait -Force -Verbose”

If you need additional DSC modules, you will need to install them to your module path (usually C:\Program Files\WindowsPowerShell\Modules) before you execute the configuration.

Hope this helps!

Posted in Azure, PowerShell, PowerShell DSC | Leave a comment