Skip to content

Instantly share code, notes, and snippets.

@gregjhogan
gregjhogan / change-ad-password.ps1
Created April 3, 2017 15:42
Change AD password
$domain = ''
$user = ''
$oldPassword = (Read-Host 'Old Password' -AsSecureString)
$newPassword = (Read-Host 'New Password' -AsSecureString)
Set-ADAccountPassword -Server $domain -Identity $user -OldPassword $oldPassword -NewPassword $newPassword
@gregjhogan
gregjhogan / iis-configure-msdeploy-web-management.ps1
Created April 15, 2017 18:46
IIS server web management msdeploy setup
Install-WindowsFeature Web-Mgmt-Service
Set-Service -Name WMSVC -StartupType Automatic
Start-service -Name WMSVC
# download/install msdeploy from here
# https://www.iis.net/downloads/microsoft/web-deploy
# MAKE SURE YOU INSTALL THE "Web Deploy Handler" OPTION
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Management")
$credential = Get-Credential
[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser($credential.UserName, $credential.GetNetworkCredential().Password)
[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant($credential.UserName, "Default Web Site", $false)
@gregjhogan
gregjhogan / curl-push-azure-storage-blob.sh
Created April 20, 2017 18:13
Push a file to a blob in an Azure storage account
curl -X PUT -T ./{file.dat} -H "x-ms-date: $(date -u)" -H "x-ms-blob-type: BlockBlob" "https://{storageaccount}.blob.core.windows.net/backups/{file.dat}?{sas-token}"
@gregjhogan
gregjhogan / msbuild-create-msdeploy-package-options
Created April 23, 2017 01:16
MSBUILD options passed to generate a clean msdeploy package
/p:DeployOnBuild=true /p:DeployTarget=Package /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\" /p:IncludeSetACLProviderOnDestination=false /p:AutoParameterizationWebConfigConnectionStrings=false
@gregjhogan
gregjhogan / azure-sql-shrink-database.sql
Last active April 26, 2017 01:30
Shrink an Azure SQL database
DBCC SHRINKFILE ('data_0', TRUNCATE ONLY)
DBCC SHRINKFILE ('log', TRUNCATE ONLY)
-- view used/available space
SELECT
name,
CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 as SpaceUsedInMB,
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS AvailableSpaceInMB
FROM sys.database_files
@gregjhogan
gregjhogan / windows-vm-disable-host-time-sync.ps1
Created May 7, 2017 02:41
Windows VM disable the VM IC Time Synchronization Provider
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\VMICTimeProvider -Name Enabled -Value 0
Restart-Service "Windows Time"
# check if domain controller is being used for time
# (you don't want to see VM IC Time Synchronization Provider as the source)
w32tm /query /source
w32tm /query /status
@gregjhogan
gregjhogan / winrm-https-self-signed-cert-certificate-auth.ps1
Created June 22, 2017 23:21
Configure WinRM HTTPS w/ self-signed certificate for certificate based authentication
# configure client for certificate authentication
$clientCert = New-SelfSignedCertificate -Type Custom -Container vm-admin -Subject "CN=vm-admin" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2","2.5.29.17={text}upn=vm-admin@localhost") -KeyUsage DigitalSignature,KeyEncipherment -KeyAlgorithm RSA -KeyLength 2048 -CertStoreLocation "Cert:\CurrentUser\My"
Get-ChildItem Cert:\CurrentUser\My\$($clientCert.Thumbprint) | Export-Certificate -FilePath .\vm-admin.cer -Type CERT
$publicKey = [System.Convert]::ToBase64String((Get-Content -Path .\vm-admin.cer -Encoding Byte))
# configure server
$cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName $env:COMPUTERNAME
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Enable-WSManCredSSP -Role Server -Force
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $cert.Thumbprint –Force
@gregjhogan
gregjhogan / azure-spn-certificate-auth-setup.ps1
Created July 19, 2017 05:20
Setup service principal with self-signed certificate for authentication
Set-StrictMode -Version Lastest
$ErrorActionPreference = "Stop"
Logon-AzureRmAccount
$appId = Read-Host "Enter application ID of service principal"
$adApp = (Get-AzureRmADApplication -ApplicationId $appId)[0]
$spnId = (Get-AzureRmADServicePrincipal -ServicePrincipalName $adApp.IdentifierUris[0])[0].ApplicationId.Guid
$endDate = (Get-Date).AddYears(1)
@gregjhogan
gregjhogan / convert-pfx-to-base64.ps1
Last active September 13, 2023 14:32
Export pfx certificate file to base64 string
$pfxFile = 'c:\path\to\cert.pfx'
$password = Read-Host -Prompt 'password' -AsSecureString
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($pfxFile, $password, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$pfxBlob = [System.Convert]::ToBase64String($cert.Export([Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12))
"THUMBPRINT: $($cert.Thumbprint)"
"PFX BLOB: `n$pfxBlob"
@gregjhogan
gregjhogan / generate-cryptographic-key.ps1
Created July 29, 2017 07:00
Generate a cryptographic key or secret
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
$aesManaged.BlockSize = 128
$aesManaged.KeySize = 256
$aesManaged.GenerateKey()
[System.Convert]::ToBase64String($aesManaged.Key)