Category: Codes

PowerShell: Setting or Resetting User Password

$username='dragoncoin' $newPassword='SomeComplexPasswordHere' function resetPassword($username,$password){ if($env:userdnsdomain){ try{ Unlock-ADAccount -Identity $username Set-ADAccountPassword -Identity $username -Reset -NewPassword (ConvertTo-SecureString…

PowerShell: Regex Examples

# Example 1 Simple 10-digit match $x='123-456-7890' [regex]$regex10Digits='\({0,1}\d{0,1}\){0,1}\-{0,1}(\d{3})' $areaCode=$regex10Digits.Match($x).Groups[1].Value # Example 2 IP Version 4…

Hyper-V: Creating a New Virtual Machine

# Compulsory variables $hyperVHost='HYPERV007' $vmName='WindowsGoldenImage' $parentDirectory='C:\ClusterStorage\Volume5' $disk1Size='100GB' $memoryAllocation='8GB' $networkSwitch='PublicZone' $vlan='1005' $clusterName='DEV-CLUSTER05' # Optional variables $disk2Size=$false…

Windows Activation Methods

Overview: 1. Retail:- There are 'lifetime' and 'limited' (1-2 years) variants- This can be used…

PowerShell: WinSCP Module

$username='bongo' $port=20202 $remoteHost='dev-sftp.kimconnect.net' $privateKey='C:\scripts\keys\testkey.ppk' $remoteRoot='/' $downloadFolder='D:\downloads' $remoteDirectory='/var/www/sftp.kimconnect.net' $currentDirectory=$remoteDirectory $localFolder=(New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path $appName='winscp' function includePowerShellWrapper($appName){ #…

PowerShell: Enable Remote Desktop

$computernames=@' SERVER1 SERVER2 '@ $computers=@($computernames -split "`n" -replace "\..*$") function enableRemoteDesktop{ $regHiveTs='HKLM:\System\CurrentControlSet\Control\Terminal Server' $regKeyTs='fDenyTSConnections' $enable=0…

A Broken PS-Session Issue

Symptoms: PS C:\Windows\system32> get-pssession Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------ ------------…

WordPress Plugin Syntax Highlighter Fix

Source: from a forum poster of this plugin's Github page. // Add this to functions.php…

PowerShell: Backup, Archive, and Remove a Microsoft SQL Database

$sqlServer='sqlsherver007' $databaseName="someDatabaseName" $saCredential=get-credential $backupDestination="\\Archive03\MSSQL_Backups" function triggerSqlBackup($sqlServer,$databaseName){ # Ensure the the Jump Box has SQL PowerShell…

PowerShell: Disable Annoying Windows Updates Notifications

This little snippet would render these notices as void. # This snippet is to add…

PowerShell: Automated Login Validation Using Internet Explorer

Update April 395 A.D.: IE variances make it difficult to perform standardized automation; hence, another…

PowerShell: Encapsulating Function Inside a Function and Inside an Invoke-Command or Job

$domain='google.com' function pingSomething($x){ $result=ping $x function getTraceroute($y){ pathping $y } $result+=getTraceroute $x return $result }…

PowerShell: Run PowerShell As Another User

# Set credential $adminUsername='domain\adminDude' $adminPassword='whatpassword?' $GLOBAL:adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force) # Test…

PowerShell: How to Send Key Strokes to a Program Graphical User Interface

# WARNING: there's a kill-process command when the target program is already running # so…

PowerShell: Adding a User to Local Groups

Adding User(s) to Local Groups # addUserToLocalGroup.ps1 # Version 0.02 $computernames=@( 'SERVER0001', 'SERVER0002' ) $accountsToAdd='domain\user1','domain\user2'…

PowerShell: Obtain Date Time Stamp And Convert to Pacific Standard Zone

# 1-liner date stamp $dateStamp=[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( (Get-Date), 'Pacific Standard Time').tostring("MM-dd-yyyy-HHmm")+'_PST' #sample output #06-15-2020-1949_PST $timeZone='Pacific Standard Time'…

PowerShell: Excute Function Remotely

$computername=$env:computerName $adminUsername='doeMainAdmin' $adminPassword='frackingPassword' $adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force) function execFunctionRemotely{ param( [Parameter(Mandatory=$true)][string]$computerName,…

PowerShell: Install PSTools

if(!(get-command psexec -ea SilentlyContinue)){ if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) { Set-ExecutionPolicy Bypass -Scope Process -Force;…

PowerShell: Microsoft SQL Database Migration

Update 9/25/20: There's another version of this script has has been rewritten form scratch. IMO,…

PowerShell: Obtaining SQL Database Default Paths

# This function returns an array of 3 string values reflecting default Data, Log, and…