PowerShell: Generate Self-Signed Certificate and Encode to Base64

Here’s how you can generate a self-signed SSL certificate in PowerShell. Be sure to use an Administrative PowerShell session. After creating the self-signed certificate, you can also export the certificate as a .pfx file and encode the output to a base64 string. This is useful for creating resources like the Application Gateway in Azure via ARM template.

$Cert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName "my.selfsigned.site.biz"
$securePwd = ConvertTo-SecureString -String "password!" -Force -AsPlainText
Export-PfxCertificate -Cert "Cert:\LocalMachine\My\$($Cert.Thumbprint)" -FilePath 'c:\temp\MyCert.pfx' -Password $securePwd
$fileBytes = Get-Content 'c:\temp\MyCert.pfx' -Encoding Byte
[System.Convert]::ToBase64String($fileBytes) | Out-File 'c:\temp\MyCert.txt'

Hope this helps!

Posted in Azure, PowerShell | Leave a comment

Azure PowerShell: Deploy Linux Custom Script Extension

Here’s how you can deploy an Azure VM Extension via PowerShell:

# Before running this, upload your bash script to an Azure storage account container, grab its name and key via PowerShell or the Azure portal
$Subscription = "MySubscription"
$ResourceGroupName = "MyResourceGroup"
$VMName = "myvm"
$Location = "eastus"
$OldCseName = "PreviousCSE"
$NewCseName = "NewCSE"
$StorageAccountName = "storageaccountname"
$StorageAccountKey = "longstorageaccountkey"
$StorageContainerName = "storageaccountcontainername"
$ScriptName = "myscript.sh"
Select-AzureRmSubscription -Subscription $Subscription
# You can only have one extension of each type - remove the existing one before you deploy
Remove-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -Name $OldCseName -Force
# Now, set the new extension
Set-AzureRmVMExtension `
  -ResourceGroupName $ResourceGroupName `
  -VMName $VMName `
  -Location $Location `
  -Name $NewCseName `
  -Publisher "Microsoft.OSTCExtensions" `
  -ExtensionType "CustomScriptForLinux" `
  -TypeHandlerVersion "1.5" `
  -Settings @{"fileUris" = [Object[]]"https://$StorageAccountName.blob.core.windows.net/$StorageContainerName/$ScriptName";"commandToExecute" = "bash $ScriptName"} `
  -ProtectedSettings @{"storageAccountName" = $StorageAccountName;"storageAccountKey" = $StorageAccountKey}
# If your script isn't idempotent, remove it after success
Remove-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -Name $NewCseName -Force

Hope this helps!

Posted in Azure, Bash, PowerShell | Leave a comment

So, you can’t delete a folder in Windows because of “Path too long” errors?

If you have got yourself into a situation where you can’t delete a directory in Windows because the folder structure is too deep for Windows, you can use robocopy to save the day. Here’s a little snippet for wiping out those “path too long” directory trees:

mkdir tmp
robocopy tmp target_too_long_for_windows_delete /s /mir
rmdir tmp
rmdir target_too_long_for_windows_delete

Hope this helps!

Posted in Batch Files, Windows | Leave a comment

DOS to Linux line endings in VIM

Switching between Windows and Linux machines every day, you’ll undoubtedly end up with files in the Linux system with DOS line endings. You can easily change the line endings in the editor VIM using the following commands:

:w ++ff=dos
:w ++ff=mac
:w ++ff=unix

You can also change the file from the command line directly without “opening” VI, e.g.

vi +':wq ++ff=unix' ./some_file.txt

There is also a small common utility to help with line endings called dos2unix. It allows you to easily convert files from the command line.

Hope this helps!

Posted in Bash, Linux | Leave a comment

PowerShell: Extract Files from Zip Archive

Here’s a quick PowerShell snippet to extract files from a .zip archive:

Add-Type -Assembly System.IO.Compression.FileSystem
$PathToZipFile = "C:\temp\myarchive.zip"
$ExtractPattern = "*.log"
$ExtractDirectory = "C:\temp\extracted"
New-Item -ItemType Directory -Path $ExtractDirectory -Force | Out-Null
$ZipFile = [IO.Compression.ZipFile]::OpenRead($PathToZipFile)
$ZipFile.Entries | 
    Where-Object { $_.Name -like $ExtractPattern } | 
    ForEach {[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$ExtractDirectory\$($_.Name)", $true)}

Hope this helps!

Posted in PowerShell | Leave a comment

T-SQL: Find Out Who Has Locks In Your Databases

Here are a couple of ways to see who is locking up your SQL Server database.

First, the undocumented system stored procedure sp_who2 gives you a few more columns than the documented sp_who.

And second, this custom query gives a more tailored view of the user and the locks. It also gives you the option of tailoring the sort to your needs.

  name AS database_name,
FROM sys.dm_exec_sessions
LEFT OUTER JOIN sys.dm_tran_locks 
  ON sys.dm_exec_sessions.session_id = sys.dm_tran_locks.request_session_id
JOIN sys.databases 
  ON sys.dm_tran_locks.resource_database_id = sys.databases.database_id
  resource_type <> 'DATABASE'
  AND name ='MyDatabase'

Hope this helps!

Posted in T-SQL | Leave a comment