diff --git a/Notes.md b/Notes.md index 24cc975..d028c42 100644 --- a/Notes.md +++ b/Notes.md @@ -222,4 +222,6 @@ https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/ Find hosts, iterate with domains? -$Hostname+.domain.dom \ No newline at end of file +$Hostname+.domain.dom + +https://developer.vmware.com/docs/powercli/latest/vmware.vimautomation.core/commands/get-snapshot/#Default diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index 88d1a92..c75c17e 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -12,9 +12,10 @@ $HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" ## Add Vhost objects for each type of server -class VirtualMachine +class _Generic_VirtualMachine { [VirtualizationServer]$__ServerObject + [Array[Object]]$__Snapshots=@() [Object]$__VMObject [VirtualizationServer]Get_ServerObject() { @@ -36,9 +37,15 @@ class VirtualMachine return $this.__VMObject.PowerState } + [bool]__Get_Is_Running() + { + $type=$this.GetType().BaseType + Write-Warning "Get_Name not implemented in type $type" + return $false + } [String]Get_Name() { - return $this.__ServerObject.Name + return $this.__Get_Name() } [bool]__Get_Is_Running() @@ -52,33 +59,62 @@ class VirtualMachine return $this.__Get_Is_Running() } - [Object]__Get_Snapshots() + [Array[Object]]__Get_All_Snapshots() { $type=$this.GetType().BaseType - Write-Warning "Get_Snapshots not implemented in type $type" + Write-Warning "Get_All_Snapshots not implemented in type $type" return $null } - [Object]Get_Snapshots() + [Array[Object]]Get_All_Snapshots() { return $this.__Get_Snapshots } + [Array[Object]]__Load_Snapshots(){ + $type=$this.GetType().BaseType + Write-Warning "Load_Snapshots not implemented in type $type" + return $null + } + Load_Snapshots(){ + $snapshots=$this.__Load_Snapshots() + if ($snapshots){$this.__Snapshots=$snapshots} + [String]::Format("Loaded {1} Snapshots from the VM: '{0}' at {2}",$this.Get_Name(),$this.__Snapshots.Length,$this.url) | Write-Debug + + } + [Object]__Get_Last_Snapshot(){ + if ($this.__Snapshots.Lenght -lt 1){ + Write-Warning "No Snapshots where found, did they where loaded?" + } + return $this.__Snapshots[0] + } + [Object]Get_Last_Snapshot() + { + return $this.Get_Last_Snapshot() + } + + } -class VM_SCVMM:VirtualMachine +class VM_SCVMM:_Generic_VirtualMachine { - [bool]__Get_Name() + [String]__Get_Name() { return $this.__ServerObject.Name } - [bool]__Get_Is_Running(){ + [bool]__Get_Is_Running() + { return @{"PoweredOn"=$true;"PoweredOff"=$false}[$this.__VMObject.VirtualMachineState] } + # __Load_Snapshots(){ + # Get-SCVMCheckpoint -VM $this.Get_Name() # -VMMServer $this.__ServerObject.url + # } + + #Get-SCVMCheckpoint } -class VM_VMWare:VirtualMachine +class VM_vSphere:_Generic_VirtualMachine { - [bool] + [String] __Get_Name() { return $this.__ServerObject.Name @@ -88,11 +124,14 @@ class VM_VMWare:VirtualMachine { return @{ "PoweredOn" = $true; "PoweredOff" = $false }[$this.__VMObject.PowerState] } +# __Load_Snapshots(){ +# Get-Snapshot -VM $this.Get_Name() # -VIServer $this.__ServerObject.url +# } } class VirtualizationServer { - [VirtualMachine]$__vm_obj=[VirtualMachine] + [_Generic_VirtualMachine]$__vm_obj=[_Generic_VirtualMachine] [int]$port="" [string]$url="" [Array] $__host_list=@() @@ -104,8 +143,8 @@ class VirtualizationServer # [Boolean] FindHost ($vname) { # return $this._FindHost($vname) # } - [VirtualMachine]__create_vm([Object]$item){ - [VirtualMachine]$new_vm=$this.__vm_obj::new() + [_Generic_VirtualMachine]__create_vm([Object]$item){ + [_Generic_VirtualMachine]$new_vm=$this.__vm_obj::new() $new_vm.__ServerObject=$this $new_vm.__VMObject=$item return $new_vm @@ -145,13 +184,12 @@ class SystemCenterVirtualMachineManager: VirtualizationServer __load_hosts() { Get-SCVMMServer -ComputerName $this.url - $list = Get-SCVirtualMachine + $list = Get-SCVirtualMachine #-VMMServer $this.url $this.__host_list=$list } [Boolean] __check_login($url) { $result=Invoke-Command -ComputerName $this.url { $true } - $status_message = "OK" return $result } } @@ -160,7 +198,6 @@ class HypervisorServer: VirtualizationServer [Boolean] __log_in($url) { $result=Invoke-Command -ComputerName $this.url { $true } - $status_message = "OK" return $result } # [Boolean] _FindHost ($vname) { @@ -180,7 +217,7 @@ class HypervisorServer: VirtualizationServer # return $result # } } -class VMWareServer: VirtualizationServer +class vSphereServer: VirtualizationServer { __load_hosts() { @@ -192,16 +229,17 @@ class VMWareServer: VirtualizationServer class VirtualizerManager { - + load_hyperv_from_file(){} + load_scvmm_from_file(){} + load_vcenter_from_file(){} } -class VirtualizerMiddleware +class VirtualizerAssistant { [Array]$__connected_servers=@() [Hashtable]$__found_host_dict=@{} [Boolean] __log_in($url){ [String]::Format("-- {0}:",$url) | Write-Host - $status_message="" $result=$false try { $result=Invoke-Command -ComputerName $url { $true } @@ -274,7 +312,6 @@ class VirtualizerMiddleware [String]::Format("`----------- Couldn't be found`n") | Write-Host } } - } # find_host($vname){ # $hostlist=$vname @@ -326,7 +363,7 @@ function load_scvmm($manager) { } function Main{ - $manager = [VirtualizerMiddleware]::new() + $manager = [VirtualizerAssistant]::new() load_scvmm($manager) $manager.load_hosts() $vmhosts = Get-Hosts-From-File($HOSTS_FILE) @@ -353,6 +390,11 @@ Main # list_snaps # Replace virtual host terminology with virtual machine # add to middleware, load hyperv_from_file($path) - - +# mayb load all the snapshots into the object, but unsure how it would affect the performance, afterwards can filter by name +# Check login into svcmm specifying the server (this will allow async) +# Check login vmware +# Check get VM from vcenter +# Mayb some background/Async process. +# Mayb a function/Class to load the arguments and create some conf +# Sort Snaps by date (newest to oldes) diff --git a/placeholder.md b/placeholder.md index 92688f1..77a2b5a 100644 --- a/placeholder.md +++ b/placeholder.md @@ -32,7 +32,7 @@ At C:\Users\Mamoncete\Desktop\test.ps1:50 char:8 ====================================== ```sh -Get-SCVMMServer -ComputerName "VMMServer01.Contoso.com" -TCPPort 8100 +Get-SCVMMServer -ComputerName "VMMServer01.Cosco.com" -TCPPort 8100 ``` https://docs.microsoft.com/en-us/powershell/module/virtualmachinemanager/get-scvmmserver?view=systemcenter-ps-2022 @@ -59,4 +59,29 @@ Get-Command -Module hyper-v | Out-GridView https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 ```powershell Connect-VIServer -Server 10.23.112.235 -Protocol https -User admin -Password pass -``` \ No newline at end of file +``` + +https://thesysadminchannel.com/get-all-vmware-snapshots-using-powercli-module/ +```powershell +Get-VM | Get-Snapshot | select VM, Name, Created +``` + +```powershell +format-list +``` + + +# Async/background + +https://adamtheautomator.com/powershell-async/ + +Mayb do a list of background processes, when it finishes/breaks or something removes itself? +Mayb a list of pids, a loop that checks if the pid still running, if it is running it waits, if it isnt it get removed, resumes when all ended? + + +## "Best practices" +https://adamtheautomator.com/powershell-best-practices/ + +# Sort-Object + +https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/sort-object?view=powershell-7.2 \ No newline at end of file