Category: Codes

PowerShell: Add and Remove a Registry Key

How to Add a Registry Key # Add New Registry Key $regHive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM' $keyname='OleDbTimeout' $value=600 Set-ItemProperty…

PowerShell: Set Service Startup Mode

# setServiceStartupMode.ps1 # Set auto start and restart upon failures $computernames=@( 'TESTWINDOWS001', 'TESTWINDOWS002' ) $serviceName='windows_exporter'…

PowerShell: Move Virtual Machine Storage Using VMM

# moveVmStorageUsingVmm.ps1 # Version 0.01 $vmNames=@( 'TESTVM0001', 'TESTVM0002', 'TESTVM0003' ) $storageLocations=@( 'C:\ClusterStorage\BLOB001', 'C:\ClusterStorage\BLOB002', 'C:\ClusterStorage\BLOB003' )…

PowerShell: Add Windows NTFS Permissions to File or Folder

The re-usable function: $path='C:\Windows\servicing' $accountsToAdd='Administrators' $permissions='Full' function addNtfsPermissions ($path,$accountsToAdd,$permissions){ $acl = Get-ACL $path $accessRule=New-Object System.Security.AccessControl.FileSystemAccessRule($accountsToAdd,$permissions,"Allow")…

Search for Windows computers in a certain subnet using Active Directory

# Search for Windows computers in a certain subnet using Active Directory $subnetQuery='10.10' $filterString='2016' $computers=Get-ADComputer…

PowerShell: Quick Snippet to Remove Virtual Machine Snapshots in VMM

$vmNames=@( 'MACHINE1', 'MACHINE2' ) foreach($vmName in $vmNames){ $checkpoint = Get-SCVMCheckpoint -VM $vmName if($checkpoint){$checkpoint|%{Remove-SCVMCheckpoint -VMCheckpoint $_…

PowerShell: Create Hyper-V Guest VM From Virtual Disk (VHDX)

Part 1: Creating Hyper-V Guest VM From a Virtual Disk # createHyperVGuestVmFromDisk.ps1 # Version 0.02…

PowerShell: Find Guest VMs Associated with a Certain Storage Path

# findGuestMvsByStorage.ps1 $storagePath='\\SMBSERVER009' function getAllGuestVms($clusterName){ try{ Import-Module FailoverClusters $clusterName=if($clusterName){ $clustername }else{ (get-cluster).name } $allHyperVHosts={(Get-ClusterNode -Cluster…

PowerShell: Restart a Service on All Hyper-V Hosts of a Cluster

$serviceName='vmms' $clusterName='HyperV-cluster001' function restartServiceAllClusterNodes($service='vmms',$clusterName){ function restartService($serviceName){ $waitSeconds=40 $isValidProcess=try{[bool](get-service $serviceName -EA Stop)}catch{$false} if($isValidProcess){ try{ $process=Start-Process -FilePath…

PowerShell: Find Hyper-V Host by Guest VM Name

# findVmHostByGuestName.ps1 $vmName='TESTVM' function findVmHostByGuestName($vmName){ try{ Import-Module Hyper-V Import-Module FailoverClusters $allHyperVHosts={(Get-ClusterNode | Where { $_.State…

PowerShell: Get All Hyper-V Host Spectre Patch Versions

# getAllVmSpectrePatchVersions.ps1 function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…

PowerShell: Fix All VMs CPU Compatibility Setting

# fixAllVmCpuCompatibility.ps1 # version 0.01 function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu'…

PowerShell: Search for Hyper-V Guest VM That Has Not Been Registered In Cluster

# findVmHost.ps1 $vmName='TESTVM01' function findVmHost($vmName){ try{ Import-Module Hyper-V Import-Module FailoverClusters $allHyperVHosts={(Get-ClusterNode | Where { $_.State…

How To Stop, Start, Restart a Windows Service Being Stuck in ‘Stopping’ Status

Convenient Function:(related to https://blog.kimconnect.com/powershell-kill-a-windows-service-forcefully) # forceRestartService.ps1 # version 0.0.2 $serviceName='Spooler' function forceRestartService($serviceName='Spooler'){ $processId=Get-WmiObject -Class Win32_Service…

PowerShell: Copy Active Directory User

# copyADUser.ps1 # Version 0.01 $fromUsername='mTyson' $newUserFirstname='Bruce' $newUserLastname='Lee' $newPassword='SOMEPASSWORD' $newEmailAddress='[email protected]' $setProxyAddress=$false function copyADUser{ param( $fromUsername,…

PowerShell: Quickly Add Users into Groups on a List of Computers

$newVmNames=@( 'SERVER0001', 'SERVER0002' ) $newUsers=@( 'domain\user1' ) $groupNames='Administrators','Remote Desktop Users' $newVmNames|%{ invoke-command -computername $_ {…

PowerShell: How To Disable and/or Stop Windows Service

Disable and Stop Using Native Command 'Set-Service' # To disable service as well as stopping…

CSS Snippet to Target Phones, Tablets, and Desktops Separately

@media (min-width:20em) { /* iPhones, Androids portrait 480x320 phones */ } @media (min-width:30em) { /*…

PowerShell: Technique to Pass Array Variable as Argument

Problem: The subtle difference between invoke-command -scriptblock {} -Args vs -ArgumentList is elusive enough to…

PowerShell: Add New Virtual Disk to Existing Guest VM in Hyper-V

# Adding disks (optional) $newVMNames='TestWindows2019' $extraDiskSize='200GB' if($extraDiskSize){ foreach($newVmName in $newVMNames){ STOP-VM -vmname $newVmName $diskFile=(join-path $destinationFolder…