Category: Codes

PowerShell: Setting Windows Firewall Rules

# Set variables for HTTP$protocolName="HTTP"$protocol="TCP"$portNumbers='80','443'$direction="Inbound"$scopes='Domain', 'Private'# Add Firewall RuleNew-NetFirewallRule -DisplayName "$protocolName-$direction" -Profile @($scopes) -Direction $direction…

PowerShell: Check VLAN of Windows Machine

Le Kommand: Get-NetAdapter|select Name,VlanID Sample Outputs PS C:\Windows\system32> Get-NetAdapter|select Name,VlanIDName VlanID---- ------Ethernet 1 0 PS…

PowerShell: Set ACL

# setAcl-v0.01.ps1# # What this script does:# 1. Set permissions on a set of "destination…

PowerShell: Moving Virtual Machines & Expanding Disk Volumes in Hyper-V & Microsoft Failover Clusters

Sample VM Migation Plan (time window = 3 hours): Pre-emptively resolve disks merging errors prior…

PowerShell: Remove Nic Teaming

# Check NIC TeamingPS C:\Windows\system32> get-NetLbfoTeamName : ISCSIMembers : {iSCSI-B, iSCSI-A}TeamNics : ISCSITeamingMode : SwitchIndependentLoadBalancingAlgorithm…

Linux: MySql Docker Container – Export database

# Set VariablessqlContainer="mysql-server"userName=bruceleepassword=chucknorrisdatabaseName=kimconnect# Run mysql inside containerdocker exec -it  $sqlContainer mysql -u$userName --password='$password'show databases;# Execute mysqldumpdocker exec $sqlContainer /usr/bin/mysqldump…

PowerShell: Copying Only Files From a Folder

Function (snippet): function copyOnlyFiles{ param( [Parameter(Mandatory=$true)][string]$sourceFolder, [Parameter(Mandatory=$true)][string]$destinationFolder ) $totalSizeBytes=0; # Get-ChildItem "C:\Temp"|?{!$_.PSIsContainer}|%{robocopy "C:\Temp" "C:\TempCopy" $_}…

PowerShell: Install Visual Studio 2019 Community Version

# Install Chocolateyif (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}#…

PowerShell: Get Size On Disk and Discover Largest Files in a Given Directory

# getSizeOnDisk-v0.01.ps1# Set variables$parentDirectory="C:\"$depth=4$topX=10$excludeDirectories="C:\Windows","C:\Program Files","C:\Program Files (x86)"$clusterSizeOfNetworkShare=8192# sanitateif($depth -le 1){$depth=1}################################## Excuting Program as an Administrator…

Setting Up Python on a Windows Machine

# Install Visual C++ 14choco install vcredist2017 -y# Install Python 3.8choco install python --version=3.8 -y#…

PowerShell: Quick 1-Liner to Check Status of URL

The kommand: Invoke-WebRequest "https://blog.kimconnect.com" -MaximumRedirection 0 -ErrorAction SilentlyContinue | Select-Object StatusCode,StatusDescription Sample Result: StatusCode StatusDescription----------…

PowerShell: Generate a CSV Report of O365 Exchange Online Mailboxes

# Office 365 Global Admin Credential$username="[email protected]"$password=ConvertTo-securestring "PASSWORD" -AsPlainText -Force$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$passwordfunction…

PowerShell: Connect to Office 365 Exchange Online

function disconnectExchangeOnline{ <# manually check sessions PS C:\Windows\system32> Get-PSSession Id Name ComputerName ComputerType State ConfigurationName…

PowerShell: Get Executable Version and File Location

# Quick method to obtain computernames of all nodes in a cluster and adjoin result…

PowerShell: Add System Backup Privileges

function addSystemPrivilege{ param( [String[]]$privileges=@("SeBackupPrivilege","SeRestorePrivilege") ) function includeSystemPrivileges{ $win32api = @' using System; using System.Runtime.InteropServices; namespace…

PowerShell: Deleting a Single File Safely

# Name your file$item="\\FILESHERVER007\someweird folder names with long paths\ fmmkklghhbb-yj-yuyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy p-hphlfiles\whatup.exe - Shortcut.lnk" #item could…

PowerShell: Increase Default Windows 260-Character Paths Limit

Problem: Error when trying to rename files manually:'The source files name(s) are larger than is…

PowerShell: How to Properly Delete a Msol User Account

# Set the user ObjectID attribute variable$msolUser="6f2fcfcd-...."# Move user account to Recycle BinRemove-MsolUser -ObjectId $msolUser#…

PowerShell: Play with Time

1. Constructor: $timer=[System.Diagnostics.Stopwatch]::StartNew() 2. Accessing a property $totalSeconds=$timer.Elapsed.TotalSeconds 3. Output Time (Usage): $hours=[math]::round($totalSeconds/3600,2)write-host "It has…

PowerShell: Accessing the Reflection Assembly Class to Retrieve User Context

Step 1: Accessing Reflection Assembly namespace to call method Load with Windows Account Management as…