Category: Codes

PowerShell: Overcome Issues with Error 13932 in SCVMM When Refreshing Virtual Machines

Dealing with Clusters # refreshCluster.ps1 # Function to refresh a cluster in VMM in anticipation…

PowerShell: Try Catch Technique to Obtain Error Type

# Test try{Read-SCVirtualMachine $vmName -EA Stop}catch{$errorMessage=$error[0].Exception.GetType().FullNamewrite-host $errorMessage} # Get error type Microsoft.VirtualManager.Utils.CarmineException # Retry catch…

WordPress: Add Search Box Into Header

Navigate to Appearance > Theme Editor > select header.php > search for this section: <div…

PowerShell: Initiate Tests on Certain Ports

This appears to be a duplicate of another post. function initTestPort($portNumber=5985,$maxTests=3){ function getIndexDifference { param(…

PowerShell: How To Test A Server Ephemeral Port

# Setup a listening port on server # This session will automatically terminates after a…

PowerShell: Compare File Counts of 2 Directories

$folder1='C:\Temp' $folder2='C:\Temp2' $username=$null $password=$null function compareFileCounts{ param($folder1,$folder2,$mountAsUser,$mountAsPassword) function mountPathAsDrive($path,$driveLetter,$mountAsUser,$mountAsPassword){ function convertPathToUnc($path,$computername=$env:computername,$credentials=$null){ $pathIsUnc=$path -match '^\\\\' $pathIsReachable=if($credentials){test-path…

PowerShell: Query Google Account Using GAM

$emailAddress='[email protected]' $field='accounts:last_login_time' function getGamUser{ param( $emailAddress, $field ) $result=try{gam report users user $emailAddress}catch{} if($result){ $headers=$result[0]…

PowerShell: Disable Windows Hello

function disableWindowsHello{ $regHive='REGISTRY::HKLM\SOFTWARE\Policies\Microsoft\PassportForWork' $refreshEnv=$false if (!(Test-Path $regHive)){ Write-Host "Creating registry path $regHive" New-Item -Path $regHive…

Quick Snippet to Copy NTFS Permissions Between SMB Shares

The experimental script below will sync permissions of a folder toward another.WARNING: if sub-folders at…

How To Copy Folder in Legacy Windows with Long File Names?

$sourceDirectory='D:\SMBSHARE\LONGPATH' $destinationDirectory='\\FILESERVER\LONGPATH' function copyFolderWithLongNames($sourceDirectory,$destinationDirectory){ $sourceParent=split-path $sourceDirectory -parent $sourceChild=split-path $sourceDirectory -leaf $destinationParent=split-path $destinationDirectory -parent $destinationChild=split-path $destinationDirectory…

PowerShell: Expand Disk Volume to Maximum Size

# expandDisk.ps1 # version 0.02 $driveLetters=(Get-wmiobject win32_volume -filter 'DriveType=3 AND DriveLetter IS NOT NULL').DriveLetter function…

PowerShell: How to Change Active Directory Username

$oldUsername='testUser' $newUsername='tUser' function changeUsername{ param( $oldUsername, $newUsername ) $domainName=$env:USERDNSDOMAIN $newUserPrincipleName="$newUsername@$domainName" try{ Set-ADUser $oldUsername -SamAccountName $newUsername…

PowerShell: Create, Edit User Accounts Basing on CSV File

# Create New Users: defined as names in CSV that does not exist in Existing…

PowerShell: Update Accounts Basing on CSV File

# adAccountsCsvUpdate.ps1 $originalCsvFile='C:\Users.csv' $newCsvFile='C:\Users-finalized.csv' $newEmailSuffix='@kimconnect.net' $newOu='CN=Users,DC=kimconnect,DC=com' function adAccountsCsvUpdate{ param( $originalCsvFile, $newCsvFile, $newEmailSuffix, $newOu ) function…

PowerShell: Export Event Logs by a Certain Date Range

# User defined variables $daysLimit=7 $startdate=(get-date).adddays(-$daysLimit) $computername=$env:computername $logName='Application' $filterTypes='Error','Warning' $logPath="c:\temp\eventlogs-from-$($startdate.tostring('yyyy-MM-dd'))-to-$((get-date).tostring('yyyy-MM-dd')).csv" # Get the event logs…

PowerShell: Backup Dynamics CRM Organization Database

# backupCrmOrgDatabase.ps1 # This script is to be invoked in the context of the Administrator…

PowerShell: Remove or Disable Windows Defender

$username='domain\serviceAccount' $password='PasswordHere' $encryptedPassword=ConvertTo-SecureString $password -AsPlainText -Force $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$encryptedPassword; $computerNames=@( 'SERVER01',…

PowerShell: Windows Session Memory Usage Watcher

# windowsSessionWatcher.ps1 $serverNames=@( 'IRV-RDS01.domain1.net', 'LAX-RDS02.domain2.com ) $thresholdMemoryUsagePercent=30 $thresholdMemoryGb=20 $domainCreds=@( @{domain='domain1.ad';username='domain1\sysadmin';password=$env:pass1} @{domain='domain2.com';username='domain2\sysadmin';password=$env:pass2} ) # Email relay…

PowerShell: Checking Duplicating Identifiers Among ADFS Relying Party Trusts

function getDuplicatingIfd{ write-host "Checking each relying party trust for any duplicates of identifiers..." $trusts=Get-AdfsRelyingPartyTrust $allTrustNames=$trusts.Name…

PowerShell: Connecting via WinRM with a Timeout

$credentials=get-credential $sessionTimeout=New-PSSessionOption -OpenTimeout 120000 # 2 minutes $sessionIncludePort=New-PSSessionOption -IncludePortInSPN -OpenTimeout 120000 $session=if($credentials){ try{ New-PSSession -ComputerName…