Skip to content

Instantly share code, notes, and snippets.

@sarangbk
sarangbk / RandomPasswordAsSecureString.ps1
Created October 11, 2017 09:50
Quick and dirty way to generate a random password as secure string for use in a PowerShell script
$Password= -join ((33..63) + (65..90) + (97..122) | Get-Random -Count 12 | % {[char]$_}) | ConvertTo-SecureString -AsPlainText -Force
class SPThumbnailPathGenerator {
_SPThumbnailPath: string;
constructor(path: string) {
this._SPThumbnailPath = path;
}
SPThumbnailPath_W ()
{
let imgPath = this._SPThumbnailPath;
let pathWithOutFileName = imgPath.substring(0, imgPath.lastIndexOf("/") + 1);
@sarangbk
sarangbk / rowcountall.sql
Created May 11, 2017 11:34
Get row count in each user table in a Azure SQL DB
SELECT t.name as TableName, s.row_count as NumberofRows from sys.tables t
JOIN sys.dm_db_partition_stats s
ON t.object_id = s.object_id
AND t.type_desc = 'USER_TABLE'
AND t.name not like '%dss%'
AND s.index_id IN (0,1)
Add-AzureRmAccount
Set-AzureRmContext -SubscriptionId '<Enter SubscriptionId here only if you have more than one subscriptions for the same email address/identity>'
$firewallRuleName = '<firewalll rule to remove/update if it already exists>'
$serverName = '<Database Server name for which rule is to be added>'
$resourceGroupName = '<Resource group of database server>'
$myIp = (Invoke-WebRequest ifconfig.me/ip).Content.Trim()
Write-Host 'Creating firewalll rule for ip',$myIp
Remove-AzureRmSqlServerFirewallRule -FirewallRuleName $firewallRuleName -ResourceGroupName $resourceGroupName -ServerName $serverName -WarningAction Continue -Force -ErrorAction Continue
New-AzureRmSqlServerFirewallRule -ResourceGroupName $resourceGroupName -ServerName $serverName -FirewallRuleName $firewallRuleName -StartIpAddress $myIp -EndIpAddress $myIp
@sarangbk
sarangbk / Base64 to String (VARCHAR) and String (VARCHAR) to Base64.sql
Created June 20, 2016 19:48
MSSQL Convert a VARCHAR to Base64 encoding and vice-versa
-- VARCHAR TO Base64
IF OBJECT_ID (N'[dbo].[uFnStringToBase64]', N'FN') IS NOT NULL
DROP FUNCTION uFnStringToBase64;
GO
CREATE FUNCTION [dbo].[uFnStringToBase64]
(
@InputString VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
@sarangbk
sarangbk / Down-Level-Logon-To-UPN-Logon.sql
Last active June 20, 2016 22:10
-- Convert Down Level Logon format e.g. domain\username to User Principal Name (UPN) format username@domain.extension
-- Convert Down Level Logon format e.g. domain\username to User Prindipal Name (UPN) format username@domain.extension
-- The formats are explained here https://msdn.microsoft.com/en-us/library/windows/desktop/aa380525(v=vs.85).aspx
IF OBJECT_ID (N'[dbo].[uFnGetDomainAtFromDomainSlash]', N'FN') IS NOT NULL
DROP FUNCTION uFnGetDomainAtFromDomainSlash;
GO
CREATE FUNCTION [dbo].[uFnGetDomainAtFromDomainSlash](@AccountNameWithDomainSlash VARCHAR(MAX), @DomainExtension VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @AccountName VARCHAR(MAX),
@sarangbk
sarangbk / Get RowCount of all tables in a MSSQL database
Last active May 7, 2016 12:22
Get RowCount of all tables in a MSSQL database
-- Run this against the database from which you want the row counts
-- Ref https://msdn.microsoft.com/en-us/library/ms190324.aspx
SELECT
QUOTENAME(SCHEMA_NAME(sysObjects.schema_id)) + '.' + QUOTENAME(sysObjects.name) AS [Table]
, SUM(sysPartitions.Rows) AS [Rows]
FROM
sys.objects AS sysObjects
INNER JOIN sys.partitions AS sysPartitions
ON sysObjects.object_id = sysPartitions.object_id
WHERE
@sarangbk
sarangbk / mssqldbrename.md
Last active August 29, 2015 14:08
Rename a Database in MSSQL

Rename a MSSQL Database

Renaming a db fails in MSSQL as it is not in single user mode by default and hence exclusive lock cannot be obtained. Use the following script to put the DB in single user mode -> rename it and then restore it to multiuser mode.

USE master

ALTER DATABASE [Original DB Name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE

ALTER DATABASE [Original DB Name] MODIFY NAME = [Renamed DB Name]

@sarangbk
sarangbk / VSSCShortcuts.md
Last active August 29, 2015 14:08
Handy VS Source control Shortcuts

VS Source Code Shortcuts

ALT+V, E, S - Launch Source Control Explorer

ALT+V, E, H - Show Pending Changes

CTRL+T, CTRL+H Show History

Once in Source Control Explorer