Skip to content

Instantly share code, notes, and snippets.

@nascarsayan
Last active July 16, 2021 07:06
Show Gist options
  • Save nascarsayan/58427b5de44a71b279cff56b85d24096 to your computer and use it in GitHub Desktop.
Save nascarsayan/58427b5de44a71b279cff56b85d24096 to your computer and use it in GitHub Desktop.
Powershell Functions
$VMMUname = ''
$VMMPass = ''
$Sess = @{}
function Show-VMMMemory {
[alias("vmmspec")]
param()
$hostname = $Sess['vmm'].ComputerName
Write-Output "Getting memory spec details for $hostname ..."
Invoke-Command -Session $Sess['vmm'] {
$totalMem = 0;
$usedMem = 0;
Write-Output "+++ Powered on VMs`n"
$vms = Get-SCVirtualMachine -VMMServer localhost
$vms | ForEach-Object {
if ($_.MemoryAssignedMB -gt 0) {
[PSCustomObject]@{
Name = $_.Name
ID = $_.ID
StatusString = $_.StatusString
AssignedMemoryGB = ($_.MemoryAssignedMB / 1024.0)
} | ConvertTo-Json
$usedMem += $_.MemoryAssignedMB;
$totalMem += $_.MemoryAssignedMB;
}
};
Write-Output "+++ Hosts`n"
$vmHosts = Get-SCVMHost -VMMServer localhost;
$vmHosts | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
AvailableMemoryGB = ($_.AvailableMemory / 1024.0)
} | ConvertTo-Json
$totalMem += $_.AvailableMemory
};
$usedMem = ($usedMem / 1024 + 0.0);
$totalMem = ($totalMem / 1024 + 0.0);
Write-Output "`n+++ UsedMemoryGB = $usedMem"
Write-Output "+++ TotalMemoryGB = $totalMem"
}
Write-Output "+++ HostName = $hostname"
}
class Host {
[string]$FQDN
[string]$Name
[string]$Description
}
$vmmpods = @(
[Host]@{FQDN = '10.184.137.134'; Name = 'VMMNEBDEV0805'; Description = 'TotalMemoryGB: 28.64' }
[Host]@{FQDN = '10.225.95.100'; Name = 'VMMNEBDEV0050'; Description = 'TotalMemoryGB: 72.58' }
[Host]@{FQDN = '10.225.91.184'; Name = 'VMMNEBDEV0103'; Description = 'TotalMemoryGB: 40.99' }
[Host]@{FQDN = '10.184.155.2'; Name = 'VMMNEBSTD0582'; Description = 'TotalMemoryGB: 19.44' }
)
function Get-Session {
param (
[Parameter(Mandatory = $true)][string]$ComputerName,
[Parameter(Mandatory = $true)][string]$Username,
[Parameter(Mandatory = $true)][string]$Passwd
)
$DebugPreference = 'Continue';
if ($Sess[$ComputerName]) {
Write-Debug "Returning cached session to $ComputerName..."
$Sess['latest'] = $Sess[$ComputerName]
return $Sess['latest']
}
Write-Debug "Connecting to $ComputerName..."
$DebugPreference = 'SilentlyContinue';
$passSec = ConvertTo-SecureString -String $Passwd -AsPlainText -Force
$cred = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList $Username, $passSec
$so = New-PsSessionOption -SkipCACheck -SkipCNCheck
$Sess[$ComputerName] = New-PSSession -ComputerName $ComputerName -Authentication 'negotiate' -Credential $cred -SessionOption $so
$Sess['latest'] = $Sess[$ComputerName]
return $Sess['latest']
}
function Get-VMMSession {
[alias("vmmg")]
param (
[string]$ComputerName
)
if ($ComputerName -eq '') {
$vmmpods
return $null
}
$pod = $null
foreach ($vmmpod in $vmmpods) {
if (($ComputerName -eq ($vmmpod.FQDN.Split("."))[3]) -or ($ComputerName -eq $vmmpod.Name)) {
$pod = $vmmpod
}
}
if ($pod) { $ComputerName = $pod.FQDN }
$Sess['vmm'] = Get-Session $ComputerName $VMMUname $VMMPass
if ($pod) {
$Sess[($pod.FQDN.Split("."))[3]] = $Sess['vmm']
$Sess[$pod.Name] = $Sess['vmm']
}
return $Sess['vmm']
}
function Enter-VMMSession {
[alias("vmme")]
param (
[string]$ComputerName
)
if ((!$ComputerName) -and $Sess['vmm']) {
$Session = $Sess['vmm']
} else {
$Session = Get-VMMSession $ComputerName
}
if ($Session) {
Enter-PSSession $Session
}
}
function Invoke-VMMSession {
[alias("vmmi")]
param (
[string]$script,
[string]$Session
)
$script = $script -replace '(Get-SC[a-z]*)(?! -VMMServer)', '$1 -VMMServer localhost '
if (!$Session) {
$Session = $Sess['vmm']
}
$scriptBlock = [Scriptblock]::Create($script)
Invoke-Command -Session $Sess['vmm'] -ScriptBlock $scriptBlock
}
function DelVM {
Invoke-Command -Session $Sess['vmm'] -Script { $VMs = Get-SCVirtualMachine | Where-Object { $_.Name -match '^(arc|sn)-' }; $vm.length; foreach ($vm in $vms) { $null = Remove-SCVirtualmachine -VM $vm; } }
}
function DelVMT {
Invoke-Command -Session $Sess['vmm'] -Script { $VMTs = Get-SCVMTemplate | Where-Object { $_.Name -match 'Temporary' }; $vmts.length; foreach ($vmt in $vmts) { $null = Remove-SCVMTemplate -VMTemplate $vmt; } }
}
function fix_perf {
sudo net stop WinMgmt; sudo net start WinMgmt;
}
function Invoke-ScriptVMM() {
[alias("vmmscr")]
param (
[string]$ScriptPath,
$Session
)
if (!$Session) {
$Session = $Sess['vmm']
}
$Parameters = @{
Session = $Session
FilePath = $ScriptPath
}
Invoke-Command @Parameters
}
function Invoke-ScriptVMMAll() {
[alias("vmmscra")]
param (
[string]$ScriptPath
)
foreach ($vmmpod in $vmmpods) {
$FQDN = $vmmpod.FQDN
$null = Get-VMMSession $FQDN
Invoke-ScriptVMM $ScriptPath
}
}
function Reset-WslServe {
Get-Service LxssManager | Restart-Service
wsl -t Ubuntu-18.04
}
Set-PSReadlineKeyHandler -Key ctrl+d -Function DeleteCharOrExit
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
Invoke-Expression (&starship init powershell)
. $HOME/Code/ps1/functions.ps1
function Reset-WslServe {
Get-Service LxssManager | Restart-Service
wsl -t Ubuntu
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment