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\"
$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

Azure PowerShell: Copy All Files in Blob Storage to Local Directory

Here’s a Azure PowerShell script that downloads all blobs in a storage container to your local disk:

$storageAccountName = "mysuperstorage"
$containerName = 'my-blobs'
$destinationFolder = 'C:\temp\my-blobs'
New-Item -ItemType Directory -Force -Path $destinationFolder
$storageAccountContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $storageAccountName}).Context
$blobs = Get-AzureStorageBlob -Container $containerName -Context $storageAccountContext
foreach ($blob in $blobs)
  Get-AzureStorageBlobContent -Container $containerName -Blob $blob.Name -Destination $destinationFolder -Context $storageAccountContext

Hope this helps!

Posted in Azure, PowerShell | Leave a comment

PowerShell: Read Secure String and Convert Back to Plain Text

Here’s a quick way to read in a secure string and then convert it back to plain text:

$SecureString = Read-Host -Prompt "What is the secret?" -AsSecureString
$ClearTextString = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))

Also, take a look at the ConvertTo-SecureString and ConvertFrom-SecureString functions of PowerShell.

Hope this helps!

Posted in PowerShell | Leave a comment

PowerShell: Generate a Pseudo-Random String

Here’s a quick way to generate a pseudo-random string with PowerShell:

$Length = 20
$AllowedCharacters = "abcdefghijklmnopqrstuvwxyz0123456789".ToCharArray()
$MyRandomString = -join (Get-Random -Count $Length -InputObject $AllowedCharacters)

Hope this helps!

Posted in PowerShell | Leave a comment