From f2aa5f11dd05c8f38604e46da800545ce5ce7529 Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Fri, 8 Apr 2022 03:29:56 +0200 Subject: [PATCH 01/13] Notes update --- placeholder.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/placeholder.md b/placeholder.md index ceae2bc..7166491 100644 --- a/placeholder.md +++ b/placeholder.md @@ -56,4 +56,38 @@ Get-Command -Module hyper-v | Out-GridView ##### Niputis -https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 \ No newline at end of file +https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 + +# At the authentication side. +was considering something like + + +## Background login +for (object in auth_list){ + if (can_do_backround_login){ + try{ + object.login_ended=False + check_login() # Emphasis on sending it to the background + } + catch $error { + finally.login_ended=True + } + } +} + +## Front login +for (object in auth_list){ + try{ + object.login_ended=False + check_login() # Emphasis on sending it to the background + } + catch $error { + finally.login_ended=True + } +} + + + +While (there is something that still has login_ended=False){ + wait +} \ No newline at end of file -- 2.47.2 From 799adece03a4d9560131ed0ebbfce69318d97b6b Mon Sep 17 00:00:00 2001 From: ofilter Date: Sat, 9 Apr 2022 04:23:09 +0200 Subject: [PATCH 02/13] Added VM object --- Notes.md | 11 +++- Testing/with_middleware.ps1 | 123 ++++++++++++++++++++++++++++-------- placeholder.md | 36 +---------- 3 files changed, 109 insertions(+), 61 deletions(-) diff --git a/Notes.md b/Notes.md index 50de052..24cc975 100644 --- a/Notes.md +++ b/Notes.md @@ -213,4 +213,13 @@ Get-VM -ComputerName Server1 ```powershell Connect-VIServer -``` \ No newline at end of file +``` + + +## Snapshot + +https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/categories/snapshot/ + +Find hosts, iterate with domains? + +$Hostname+.domain.dom \ No newline at end of file diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index c0565cb..d237077 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -1,5 +1,5 @@ # Author: Oriol Filter -# Date: 08/04/2022 +# Date: 09/04/2022 $DebugPreference = "Continue" @@ -12,39 +12,90 @@ $HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" ## Add Vhost objects for each type of server -class VirtualVM{ +class VirtualMachine +{ [VirtualizationServer]$__ServerObject [Object]$__VMObject - [VirtualizationServer]Get_ServerObject(){ + [VirtualizationServer]Get_ServerObject() + { return $this.__ServerObject } - Set_ServerObject([VirtualizationServer] $Server){ + + Set_ServerObject([VirtualizationServer] $Server) + { [VirtualizationServer] $this.__ServerObject=$Server } - Set_VMObject([Object] $object){ + + Set_VMObject([Object] $object) + { $this.__VMObject=$object } - [Object]Get_VMObject(){ + + [Object]Get_VMObject() + { return $this.__VMObject.PowerState } - [String]Get_Name(){ + [String]Get_Name() + { return $this.__ServerObject.Name } - [bool]Get_Is_Running(){ - return @{PoweredOn=$true;PoweredOff=$false}[$this.__VMObject.PowerState] + + [bool]__Get_Is_Running() + { + $type=$this.GetType().BaseType + Write-Warning "Get_Is_Running not implemented in type $type" + return $false + } + [bool]Get_Is_Running() + { + return $this.__Get_Is_Running() } + [Object]__Get_Snapshots() + { + $type=$this.GetType().BaseType + Write-Warning "Get_Snapshots not implemented in type $type" + return $null + } + [Object]Get_Snapshots() + { + return $this.__Get_Snapshots + } } -class SCVMM_VM:VirtualVM{ - +class VM_SCVMM:VirtualMachine +{ + [bool]__Get_Name() + { + return $this.__ServerObject.Name + } + [bool]__Get_Is_Running(){ + return @{"PoweredOn"=$true;"PoweredOff"=$false}[$this.__VMObject.VirtualMachineState] + } } -class VirtualizationServer { - [int]$port=8100 +class VM_VMWare:VirtualMachine +{ + [bool] + __Get_Name() + { + return $this.__ServerObject.Name + } + [bool] + __Get_Is_Running() + { + return @{ "PoweredOn" = $true; "PoweredOff" = $false }[$this.__VMObject.PowerState] + } +} + +class VirtualizationServer +{ + [VirtualMachine]$__vm_obj=[VirtualMachine] + [int]$port="" [string]$url="" + [Array] $__host_list=@() # [Boolean] _FindHost ($vname) { # $type=$this.GetType().BaseType # Write-Host "Find Host not implemented in type $type" @@ -53,6 +104,12 @@ class VirtualizationServer { # [Boolean] FindHost ($vname) { # return $this._FindHost($vname) # } + [VirtualMachine]__create_vm([Object]$item){ + [VirtualMachine]$new_vm=$this.__vm_obj::new() + $new_vm.__ServerObject=$this + $new_vm.__VMObject=$item + return $new_vm + } __load_hosts(){ $type=$this.GetType().BaseType @@ -60,41 +117,48 @@ class VirtualizationServer { } load_hosts(){ - [String]::Format("Loading VM from: {0}",$this.url) | Write-Debug $this.__host_list=@() + [String]::Format("Loading VM from: {0}",$this.url) | Write-Debug $this.__load_hosts() + [String]::Format("Loaded {1} VM from: {0}",$this.url,$this.__host_list.Length) | Write-Debug } - [Array] $__host_list=@() [Array] get_host_list(){ return $this.__host_list } + [Boolean] __check_login(){ $type=$this.GetType().BaseType Write-Warning "Check_login not implemented in type $type" return $false } + [Boolean] check_login(){ Invoke-Command -ComputerName $this.url { $true } return $this.check_login() } + } -class SystemCenterVirtualMachineManager: VirtualizationServer { - __load_hosts(){ +class SystemCenterVirtualMachineManager: VirtualizationServer +{ + __load_hosts() + { Get-SCVMMServer -ComputerName $this.url $list = Get-SCVirtualMachine $this.__host_list=$list - # [string]::Format("Loaded {0} VM from {1}",$list.Lenght,$this.url) | Write-Debug } - [Boolean] __check_login($url){ + [Boolean] __check_login($url) + { $result=Invoke-Command -ComputerName $this.url { $true } $status_message = "OK" return $result } } -class HypervisorServer: VirtualizationServer{ - [Boolean] __log_in($url){ +class HypervisorServer: VirtualizationServer +{ + [Boolean] __log_in($url) + { $result=Invoke-Command -ComputerName $this.url { $true } $status_message = "OK" return $result @@ -116,13 +180,23 @@ class HypervisorServer: VirtualizationServer{ # return $result # } } -class VMWareServer: VirtualizationServer{} +class VMWareServer: VirtualizationServer +{ + __load_hosts() + { + Connect-VIServer -Server $this.url + $list = Get-SCVirtualMachine + $this.__host_list=$list + } +} -class VirtualizerManager{ +class VirtualizerManager +{ } -class VirtualizerMiddleware{ +class VirtualizerMiddleware +{ [Array]$__connected_servers=@() [Hashtable]$__found_host_dict=@{} [Boolean] __log_in($url){ @@ -192,7 +266,6 @@ class VirtualizerMiddleware{ foreach ($entry_from_dict in $entry_from_list.GetEnumerator()) { $server_location = $entry_from_dict.Name - $vm = $entry_from_dict.Value [String]::Format("`t`tFound in {0}", $server_location) | Write-Host } Write-Host "`-----------`n" diff --git a/placeholder.md b/placeholder.md index 7166491..ceae2bc 100644 --- a/placeholder.md +++ b/placeholder.md @@ -56,38 +56,4 @@ Get-Command -Module hyper-v | Out-GridView ##### Niputis -https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 - -# At the authentication side. -was considering something like - - -## Background login -for (object in auth_list){ - if (can_do_backround_login){ - try{ - object.login_ended=False - check_login() # Emphasis on sending it to the background - } - catch $error { - finally.login_ended=True - } - } -} - -## Front login -for (object in auth_list){ - try{ - object.login_ended=False - check_login() # Emphasis on sending it to the background - } - catch $error { - finally.login_ended=True - } -} - - - -While (there is something that still has login_ended=False){ - wait -} \ No newline at end of file +https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 \ No newline at end of file -- 2.47.2 From f8f1cb9555ea05c70b80a5638085ad1ca18086a1 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sat, 9 Apr 2022 04:30:46 +0200 Subject: [PATCH 03/13] added example of Connect-VIServer with https protocol --- placeholder.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/placeholder.md b/placeholder.md index ceae2bc..92688f1 100644 --- a/placeholder.md +++ b/placeholder.md @@ -56,4 +56,7 @@ Get-Command -Module hyper-v | Out-GridView ##### Niputis -https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 \ No newline at end of file +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 -- 2.47.2 From 4acbcc7feab2b12c6c684deb37cd14246ff8f4d7 Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Sat, 9 Apr 2022 14:17:11 +0200 Subject: [PATCH 04/13] . --- Testing/with_middleware.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index d237077..88d1a92 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -352,7 +352,7 @@ Main # get_status # list_snaps # Replace virtual host terminology with virtual machine - +# add to middleware, load hyperv_from_file($path) -- 2.47.2 From bc6a9395660ba385302f04d27c697c7baf5e3ee5 Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Sun, 10 Apr 2022 03:17:55 +0200 Subject: [PATCH 05/13] Added some functions to generyc --- Notes.md | 4 +- Testing/with_middleware.ps1 | 90 +++++++++++++++++++++++++++---------- placeholder.md | 29 +++++++++++- 3 files changed, 96 insertions(+), 27 deletions(-) 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 -- 2.47.2 From 4951c160b9fe33ee5b11b54d971834405bbfc675 Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Sun, 10 Apr 2022 03:19:24 +0200 Subject: [PATCH 06/13] Added get_snapshot functions to vms, couple notes to the todo list at the bottom of the file. --- Testing/with_middleware.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index c75c17e..b2c41ac 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -105,9 +105,9 @@ class VM_SCVMM:_Generic_VirtualMachine { return @{"PoweredOn"=$true;"PoweredOff"=$false}[$this.__VMObject.VirtualMachineState] } - # __Load_Snapshots(){ - # Get-SCVMCheckpoint -VM $this.Get_Name() # -VMMServer $this.__ServerObject.url - # } + __Load_Snapshots(){ + Get-SCVMCheckpoint -VM $this.Get_Name() # -VMMServer $this.__ServerObject.url + } #Get-SCVMCheckpoint } @@ -124,9 +124,9 @@ class VM_vSphere:_Generic_VirtualMachine { return @{ "PoweredOn" = $true; "PoweredOff" = $false }[$this.__VMObject.PowerState] } -# __Load_Snapshots(){ -# Get-Snapshot -VM $this.Get_Name() # -VIServer $this.__ServerObject.url -# } + __Load_Snapshots(){ + Get-Snapshot -VM $this.Get_Name() # -VIServer $this.__ServerObject.url + } } class VirtualizationServer -- 2.47.2 From f5973fa295d9af902ef3749da9b5ad693989b51b Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Sat, 16 Apr 2022 19:32:08 +0200 Subject: [PATCH 07/13] changed date. --- Testing/with_middleware.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index b2c41ac..f5f3de4 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -1,5 +1,5 @@ # Author: Oriol Filter -# Date: 09/04/2022 +# Date: 10/04/2022 $DebugPreference = "Continue" -- 2.47.2 From a31ceed069c878e1852785ed3704c72d91c032b7 Mon Sep 17 00:00:00 2001 From: ofilter Date: Fri, 13 May 2022 15:24:22 +0200 Subject: [PATCH 08/13] Listing vmware returns all the server if found. --- Testing/with_middleware.ps1 | 160 ++++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 69 deletions(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index f5f3de4..a7ab788 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -2,42 +2,49 @@ # Date: 10/04/2022 +## Vars + $DebugPreference = "Continue" $ErrorActionPreference = "Stop" $SCRIPT_PATH=$PSScriptRoot -$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list.txt" +#$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list.txt" $SCVMM_FILE = "$SCRIPT_PATH/hyperv_list.txt" +$VSPHERE_FILE = "$SCRIPT_PATH/vsphere_list.txt" $HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" -## Objects + + +### Disable Participation messages +#Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false +#Set-PowerCLIConfiguration -Scope User -InvalidCertificateAction Ignore -Confirm:$false +#Set-PowerCLIConfiguration -Scope User -DefaultVIServerMode Multiple -Confirm:$false + + + ## Add Vhost objects for each type of server class _Generic_VirtualMachine { - [VirtualizationServer]$__ServerObject - [Array[Object]]$__Snapshots=@() + [Object]$__ServerObject + [Array]$__Snapshots=@() [Object]$__VMObject - [VirtualizationServer]Get_ServerObject() + [Object]Get_ServerObject() { return $this.__ServerObject } - - Set_ServerObject([VirtualizationServer] $Server) + Set_ServerObject([Object] $Server) { - [VirtualizationServer] $this.__ServerObject=$Server + [Object] $this.__ServerObject=$Server } - Set_VMObject([Object] $object) { $this.__VMObject=$object } - [Object]Get_VMObject() { return $this.__VMObject.PowerState } - - [bool]__Get_Is_Running() + [bool]__Get_Name() { $type=$this.GetType().BaseType Write-Warning "Get_Name not implemented in type $type" @@ -47,7 +54,6 @@ class _Generic_VirtualMachine { return $this.__Get_Name() } - [bool]__Get_Is_Running() { $type=$this.GetType().BaseType @@ -58,28 +64,24 @@ class _Generic_VirtualMachine { return $this.__Get_Is_Running() } - - [Array[Object]]__Get_All_Snapshots() + [Array]__Get_All_Snapshots() { $type=$this.GetType().BaseType Write-Warning "Get_All_Snapshots not implemented in type $type" return $null } - [Array[Object]]Get_All_Snapshots() + [Array]Get_All_Snapshots() { return $this.__Get_Snapshots } - - [Array[Object]]__Load_Snapshots(){ + __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){ @@ -91,8 +93,6 @@ class _Generic_VirtualMachine { return $this.Get_Last_Snapshot() } - - } class VM_SCVMM:_Generic_VirtualMachine @@ -105,9 +105,9 @@ class VM_SCVMM:_Generic_VirtualMachine { return @{"PoweredOn"=$true;"PoweredOff"=$false}[$this.__VMObject.VirtualMachineState] } - __Load_Snapshots(){ - Get-SCVMCheckpoint -VM $this.Get_Name() # -VMMServer $this.__ServerObject.url - } + __Load_Snapshots(){ + Get-SCVMCheckpoint -VM $this.Get_Name() # -VMMServer $this.__ServerObject.url + } #Get-SCVMCheckpoint } @@ -131,7 +131,8 @@ class VM_vSphere:_Generic_VirtualMachine class VirtualizationServer { - [_Generic_VirtualMachine]$__vm_obj=[_Generic_VirtualMachine] + # The fuck does all this garbage +# [_Generic_VirtualMachine]$__vm_obj=[_Generic_VirtualMachine] [int]$port="" [string]$url="" [Array] $__host_list=@() @@ -143,12 +144,12 @@ class VirtualizationServer # [Boolean] FindHost ($vname) { # return $this._FindHost($vname) # } - [_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 - } +# [_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 +# } __load_hosts(){ $type=$this.GetType().BaseType @@ -173,33 +174,25 @@ class VirtualizationServer } [Boolean] check_login(){ - Invoke-Command -ComputerName $this.url { $true } - return $this.check_login() - } - -} - -class SystemCenterVirtualMachineManager: VirtualizationServer -{ - __load_hosts() - { - Get-SCVMMServer -ComputerName $this.url - $list = Get-SCVirtualMachine #-VMMServer $this.url - $this.__host_list=$list - } - [Boolean] __check_login($url) - { - $result=Invoke-Command -ComputerName $this.url { $true } + [String]::Format("Attempting to login into the server {0}",$this.url) | Write-Debug +# Invoke-Command -ComputerName $this.url { $true } + $result=$this.__check_login() return $result } + } + + + class HypervisorServer: VirtualizationServer { - [Boolean] __log_in($url) + [Boolean] __check_login() { $result=Invoke-Command -ComputerName $this.url { $true } return $result } + # Lacks get VM + # # [Boolean] _FindHost ($vname) { # $result=$false # try @@ -217,16 +210,35 @@ class HypervisorServer: VirtualizationServer # return $result # } } + +class SystemCenterVirtualMachineManager: HypervisorServer +{ + __load_hosts() + { +# Get-SCVMMServer -ComputerName $this.url + $list = Get-VMGet-SCVirtualMachine -VMMServer $this.url + $this.__host_list=$list + } +} class vSphereServer: VirtualizationServer { __load_hosts() { Connect-VIServer -Server $this.url - $list = Get-SCVirtualMachine + $list = VMware.VimAutomation.Core\Get-VM $this.__host_list=$list } -} + [Boolean] __check_login() + { + Connect-VIServer -Server $this.url + $result=$? +# Disconnect-VIServer -Server $this.url + return $result + } + +} +# This remains unused?? class VirtualizerManager { load_hyperv_from_file(){} @@ -238,18 +250,20 @@ class VirtualizerAssistant { [Array]$__connected_servers=@() [Hashtable]$__found_host_dict=@{} - [Boolean] __log_in($url){ - [String]::Format("-- {0}:",$url) | Write-Host + [Boolean] __log_in($server){ + [String]::Format("-- {0}:",$server.url) | Write-Host $result=$false try { - $result=Invoke-Command -ComputerName $url { $true } + $result=$server.check_login() +# $ +# $result=Invoke-Command -ComputerName $server.url { $true } } catch [System.Management.Automation.Remoting.PSRemotingTransportException] { - [String]::Format("Failed to log in to URL: {0}",$url) | Write-Warning + [String]::Format("Failed to log in to URL: {0}",$server.url) | Write-Warning [String]::Format("{0}",$Error[0]) | Write-Debug } finally { - $message = [string]::Format(">>`tStatus {0}",@("NotOK","OK")[$result]) + $message = [string]::Format(">>`tStatus {0}`n",@("NotOK","OK")[$result]) Write-Host $message } return $result @@ -258,12 +272,12 @@ class VirtualizerAssistant append_session($type,$url) { - $type.GetType().IsValueType - $result = $this.__log_in($url) +# $type.GetType().IsValueType + $server=$type::new() + $server.url=$url + $result = $this.__log_in($server) if ($result) { - $server=$type::new() - $server.url=$url $this.__connected_servers+=$server } } @@ -355,25 +369,33 @@ function load_hyperv($manager) { } } -function load_scvmm($manager) { +function load_scvmm([VirtualizerAssistant] $manager) { $hyperv_urls = Get-Hosts-From-File($SCVMM_FILE) foreach ($url in $hyperv_urls) { $manager.append_session([SystemCenterVirtualMachineManager], $url) } } +function load_vsphere($manager) { + $vsphere_urls = Get-Hosts-From-File($VSPHERE_FILE) + foreach ($url in $vsphere_urls) { + $manager.append_session([vSphereServer], $url) + } +} + +function Test_VM{ + $_____url=$_____url + $manager = [VirtualizerAssistant]::new() + $manager.append_session([SystemCenterVirtualMachineManager], $_____url) + $manager.__connected_servers[0].get_host_list()[0] +} function Main{ $manager = [VirtualizerAssistant]::new() - load_scvmm($manager) + load_vsphere($manager) $manager.load_hosts() $vmhosts = Get-Hosts-From-File($HOSTS_FILE) $manager.find_hosts($vmhosts) -} -function Test_VM{ - $_____url=$_____url - $manager = [VirtualizerMiddleware]::new() - $manager.append_session([SystemCenterVirtualMachineManager], $_____url) - $manager.__connected_servers[0].get_host_list()[0] +# $manager.__found_host_dict } Main -- 2.47.2 From 0d37142c99115917dcd5993c6baa2d34dfcd6c5e Mon Sep 17 00:00:00 2001 From: ofilter Date: Sat, 14 May 2022 14:48:06 +0200 Subject: [PATCH 09/13] Menu ongoing --- Testing/with_middleware.ps1 | 161 ++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 51 deletions(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index a7ab788..a3b3420 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -6,12 +6,6 @@ $DebugPreference = "Continue" $ErrorActionPreference = "Stop" -$SCRIPT_PATH=$PSScriptRoot -#$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list.txt" -$SCVMM_FILE = "$SCRIPT_PATH/hyperv_list.txt" -$VSPHERE_FILE = "$SCRIPT_PATH/vsphere_list.txt" -$HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" - ### Disable Participation messages #Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false @@ -225,12 +219,11 @@ class vSphereServer: VirtualizationServer __load_hosts() { Connect-VIServer -Server $this.url - $list = VMware.VimAutomation.Core\Get-VM + $list = VMware.VimAutomation.Core\Get-VM -Location $this.url $this.__host_list=$list } [Boolean] __check_login() { - Connect-VIServer -Server $this.url $result=$? # Disconnect-VIServer -Server $this.url @@ -239,12 +232,12 @@ class vSphereServer: VirtualizationServer } # This remains unused?? -class VirtualizerManager -{ - load_hyperv_from_file(){} - load_scvmm_from_file(){} - load_vcenter_from_file(){} -} +#class VirtualizerManager +#{ +# load_hyperv_from_file(){} +# load_scvmm_from_file(){} +# load_vcenter_from_file(){} +#} class VirtualizerAssistant { @@ -343,54 +336,120 @@ class VirtualizerAssistant # } } -function Get-Hosts-From-File($filepath) -{ - ## In a future use a yaml as a .conf (with the hyperv and vmware listed) - $file_content = Get-Content "$filepath" - $splitted_file_content = $file_content.Split() - $host_array=@() - foreach ($hostname in $splitted_file_content) - { - if ($hostname) - { - $host_array+=$hostname + +#Used to store the config +class Config{ + $_SCRIPT_PATH=$PSScriptRoot + $_HYPERV_FILE = [String]::Format("{0}/hyperv_list.txt",$this.SCRIPT_PATH) + $_SCVMM_FILE = [String]::Format("{0}/scvmm_list.txt",$this.SCRIPT_PATH) + $_VSPHERE_FILE = [String]::Format("{0}/vsphere_list.txt",$this.SCRIPT_PATH) + $_HOSTS_FILE = [String]::Format("{0}/hosts.txt",$this.SCRIPT_PATH) + Load_Hyperv($manager) { + $hyperv_urls = Get-Hosts-From-File($this._HYPERV_FILE) + foreach ($url in $hyperv_urls) { + $manager.append_session([HypervisorServer], $url) } } - return $host_array + + Load_Scvmm([VirtualizerAssistant] $manager) { + $hyperv_urls = Get-Hosts-From-File($this._SCVMM_FILE) + foreach ($url in $hyperv_urls) { + $manager.append_session([SystemCenterVirtualMachineManager], $url) + } + } + Load_Vsphere($manager) { + $vsphere_urls = Get-Hosts-From-File($this._VSPHERE_FILE) + foreach ($url in $vsphere_urls) { + $manager.append_session([vSphereServer], $url) + } + } + + [String[]]GetHostsFromFile($filepath) + { + ## In a future use a yaml as a .conf (with the hyperv and vmware listed) + $file_content = Get-Content "$filepath" + $splitted_file_content = $file_content.Split() + $host_array=@() + foreach ($hostname in $splitted_file_content) + { + if ($hostname) + { + $host_array+=$hostname + } + } + return $host_array + } + + +# Load_All_Servers($x){ +# $this.G +# } +# Load_All{ +# +# } +} + + +class Menu{ + + $exit=$false + [VirtualizerAssistant]$Assistant + [Config]$Config + + print_menu(){ + Write-Host "Select an option an option:" + Write-Host @" + `t- 0 Attempt to log in into the servers (authenticates with the current session) + `t- 1 Load VMs + `t- 2 Find Hosts + `t- 3 Disconnect from all servers and reload server list content + `t- 4 Reload VM file list content + `t- -1 Exit +"@ + } + + select_menu(){ + $uinput=Read-Host "Please enter your option" + switch ($uinput) { + -1 { + $this.Quit() + } + 0 { + + } + Default { + Write-Host "Error, try again" + } + } + } + + Menu(){ + while (!$this.exit){ + $this.print_menu() + $this.select_menu() + } + } + Quit(){ + $this.exit=$true + } + } # Advanced params? # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2 -function load_hyperv($manager) { - $hyperv_urls = Get-Hosts-From-File($HYPERV_FILE) - foreach ($url in $hyperv_urls) { - $manager.append_session([HypervisorServer], $url) - } -} -function load_scvmm([VirtualizerAssistant] $manager) { - $hyperv_urls = Get-Hosts-From-File($SCVMM_FILE) - foreach ($url in $hyperv_urls) { - $manager.append_session([SystemCenterVirtualMachineManager], $url) - } -} -function load_vsphere($manager) { - $vsphere_urls = Get-Hosts-From-File($VSPHERE_FILE) - foreach ($url in $vsphere_urls) { - $manager.append_session([vSphereServer], $url) - } -} +# +#function Test_VM{ +# $_____url=$_____url +# $manager = [VirtualizerAssistant]::new() +# $manager.append_session([SystemCenterVirtualMachineManager], $_____url) +# $manager.__connected_servers[0].get_host_list()[0] +#} + -function Test_VM{ - $_____url=$_____url - $manager = [VirtualizerAssistant]::new() - $manager.append_session([SystemCenterVirtualMachineManager], $_____url) - $manager.__connected_servers[0].get_host_list()[0] -} function Main{ - $manager = [VirtualizerAssistant]::new() load_vsphere($manager) $manager.load_hosts() $vmhosts = Get-Hosts-From-File($HOSTS_FILE) -- 2.47.2 From 0fbb9205e891f2c0997b52765e610b682158f9c8 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sat, 14 May 2022 15:13:21 +0200 Subject: [PATCH 10/13] Menu ongoing --- Testing/with_middleware.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index a3b3420..28ba674 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -353,9 +353,10 @@ class Config{ Load_Scvmm([VirtualizerAssistant] $manager) { $hyperv_urls = Get-Hosts-From-File($this._SCVMM_FILE) - foreach ($url in $hyperv_urls) { - $manager.append_session([SystemCenterVirtualMachineManager], $url) - } +# return @{[SystemCenterVirtualMachineManager]:3} +# foreach ($url in $hyperv_urls) { +# $manager.append_session([SystemCenterVirtualMachineManager], $url) +# } } Load_Vsphere($manager) { $vsphere_urls = Get-Hosts-From-File($this._VSPHERE_FILE) -- 2.47.2 From f2c90f208446d4420143ee99de8f6e0ccf434584 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sat, 14 May 2022 15:13:37 +0200 Subject: [PATCH 11/13] Menu ongoing --- Testing/with_middleware.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index 28ba674..ac508a8 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -353,7 +353,7 @@ class Config{ Load_Scvmm([VirtualizerAssistant] $manager) { $hyperv_urls = Get-Hosts-From-File($this._SCVMM_FILE) -# return @{[SystemCenterVirtualMachineManager]:3} + return @{[SystemCenterVirtualMachineManager]:3} # foreach ($url in $hyperv_urls) { # $manager.append_session([SystemCenterVirtualMachineManager], $url) # } -- 2.47.2 From 7bda078c1d06ee3020a180745995aaffb6715c46 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sun, 22 May 2022 14:51:20 +0200 Subject: [PATCH 12/13] Menu working --- Testing/with_middleware.ps1 | 127 ++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 49 deletions(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index ac508a8..60da14a 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -176,8 +176,6 @@ class VirtualizationServer } - - class HypervisorServer: VirtualizationServer { [Boolean] __check_login() @@ -210,16 +208,18 @@ class SystemCenterVirtualMachineManager: HypervisorServer __load_hosts() { # Get-SCVMMServer -ComputerName $this.url - $list = Get-VMGet-SCVirtualMachine -VMMServer $this.url + $list = Get-SCVirtualMachine -VMMServer $this.url $this.__host_list=$list } } + class vSphereServer: VirtualizationServer { __load_hosts() { - Connect-VIServer -Server $this.url - $list = VMware.VimAutomation.Core\Get-VM -Location $this.url +# Connect-VIServer -Server $this.url + $list = VMware.VimAutomation.Core\Get-VM -Server $this.url +# Hyper-V\Get-VM $this.__host_list=$list } [Boolean] __check_login() @@ -231,13 +231,6 @@ class vSphereServer: VirtualizationServer } } -# This remains unused?? -#class VirtualizerManager -#{ -# load_hyperv_from_file(){} -# load_scvmm_from_file(){} -# load_vcenter_from_file(){} -#} class VirtualizerAssistant { @@ -279,6 +272,7 @@ class VirtualizerAssistant } load_hosts() { + # Downloads all the VM information from the Connected Servers $this.__found_host_dict=@{} for ($num = 0 ; $num -le $this.__connected_servers.Length -1 ; $num++){ $server = $this.__connected_servers[$num] @@ -337,37 +331,47 @@ class VirtualizerAssistant } -#Used to store the config +#Used to store the config and given an assisant object attempts to log in class Config{ - $_SCRIPT_PATH=$PSScriptRoot - $_HYPERV_FILE = [String]::Format("{0}/hyperv_list.txt",$this.SCRIPT_PATH) - $_SCVMM_FILE = [String]::Format("{0}/scvmm_list.txt",$this.SCRIPT_PATH) - $_VSPHERE_FILE = [String]::Format("{0}/vsphere_list.txt",$this.SCRIPT_PATH) - $_HOSTS_FILE = [String]::Format("{0}/hosts.txt",$this.SCRIPT_PATH) - Load_Hyperv($manager) { - $hyperv_urls = Get-Hosts-From-File($this._HYPERV_FILE) + $_SCRIPT_PATH = "" + $_HYPERV_FILE = "" + $_SCVMM_FILE = "" + $_VSPHERE_FILE = "" + $_HOSTS_FILE = "" + Config(){ + $this._SCRIPT_PATH = $PSScriptRoot + $this._HYPERV_FILE = [String]::Format("{0}/hyperv_list.txt",$this._SCRIPT_PATH) + $this._SCVMM_FILE = [String]::Format("{0}/scvmm_list.txt",$this._SCRIPT_PATH) + $this._VSPHERE_FILE = [String]::Format("{0}/vsphere_list.txt",$this._SCRIPT_PATH) + $this._HOSTS_FILE = [String]::Format("{0}/hosts.txt",$this._SCRIPT_PATH) + } + Load_Hyperv([VirtualizerAssistant] $assistant) { + $hyperv_urls = $this._Get_Hosts_From_File($this._HYPERV_FILE) foreach ($url in $hyperv_urls) { - $manager.append_session([HypervisorServer], $url) + $assistant.append_session([HypervisorServer], $url) } } - Load_Scvmm([VirtualizerAssistant] $manager) { - $hyperv_urls = Get-Hosts-From-File($this._SCVMM_FILE) - return @{[SystemCenterVirtualMachineManager]:3} -# foreach ($url in $hyperv_urls) { -# $manager.append_session([SystemCenterVirtualMachineManager], $url) -# } + Load_Scvmm([VirtualizerAssistant] $assistant) { + $hyperv_urls = $this._Get_Hosts_From_File($this._SCVMM_FILE) + foreach ($url in $hyperv_urls) { + $assistant.append_session([SystemCenterVirtualMachineManager], $url) + } } - Load_Vsphere($manager) { - $vsphere_urls = Get-Hosts-From-File($this._VSPHERE_FILE) + Load_Vsphere([VirtualizerAssistant] $assistant) { + $vsphere_urls = $this._Get_Hosts_From_File($this._VSPHERE_FILE) foreach ($url in $vsphere_urls) { - $manager.append_session([vSphereServer], $url) + $assistant.append_session([vSphereServer], $url) } } - [String[]]GetHostsFromFile($filepath) + [String[]]_Get_Hosts_From_File($filepath) { - ## In a future use a yaml as a .conf (with the hyperv and vmware listed) + if (-not(Test-Path -Path $filepath -PathType Leaf)) { + [String]::Format("File {0} doesn't exist, skipping ...",$filepath) | Write-Host + return @() + } + ## In a future use a yaml as a .conf *or a json* (with the hyperv and vmware listed) $file_content = Get-Content "$filepath" $splitted_file_content = $file_content.Split() $host_array=@() @@ -380,14 +384,16 @@ class Config{ } return $host_array } + [String[]]Get_Hosts_To_Find(){ + return $this._Get_Hosts_From_File($this._HOSTS_FILE) + } -# Load_All_Servers($x){ -# $this.G -# } -# Load_All{ -# -# } + Load_All_Servers([VirtualizerAssistant] $assistant){ + $this.Load_Hyperv($assistant) + $this.Load_Scvmm($assistant) + $this.Load_Vsphere($assistant) + } } @@ -397,27 +403,46 @@ class Menu{ [VirtualizerAssistant]$Assistant [Config]$Config - print_menu(){ + _print_menu(){ Write-Host "Select an option an option:" Write-Host @" `t- 0 Attempt to log in into the servers (authenticates with the current session) `t- 1 Load VMs `t- 2 Find Hosts - `t- 3 Disconnect from all servers and reload server list content - `t- 4 Reload VM file list content +# `t- 3 Disconnect from all servers and reload server list content (??) +# `t- 4 Reload VM file list content (??) `t- -1 Exit "@ } + Log_In(){ + $this.Config.Load_All_Servers($this.Assistant) + } + Load_VM(){ + $this.Assistant.load_hosts() + } + Find_Hosts(){ + $this.Assistant.find_hosts($this.Config.Get_Hosts_To_Find()) + } - select_menu(){ + + _select_menu(){ $uinput=Read-Host "Please enter your option" switch ($uinput) { -1 { $this.Quit() } 0 { - + $this.Log_In() } + 1 { + $this.Load_VM() + } + 2 { + $this.Find_Hosts() + } +# 3 { +# $this.Disconnect() +# } Default { Write-Host "Error, try again" } @@ -425,12 +450,15 @@ class Menu{ } Menu(){ + $this.Assistant=[VirtualizerAssistant]::new() + $this.Config=[Config]::new() while (!$this.exit){ - $this.print_menu() - $this.select_menu() + $this._print_menu() + $this._select_menu() } } Quit(){ + Write-Host " -> Bye!" $this.exit=$true } @@ -451,10 +479,11 @@ class Menu{ function Main{ - load_vsphere($manager) - $manager.load_hosts() - $vmhosts = Get-Hosts-From-File($HOSTS_FILE) - $manager.find_hosts($vmhosts) + $menu = [Menu]::new() +# load_vsphere($manager) +# $manager.load_hosts() +# $vmhosts = Get-Hosts-From-File($HOSTS_FILE) +# $manager.find_hosts($vmhosts) # $manager.__found_host_dict } -- 2.47.2 From ca53cedf8889c44c27b134827d9025eab8dbcba7 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sun, 22 May 2022 14:52:53 +0200 Subject: [PATCH 13/13] Fixed date --- Testing/with_middleware.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/with_middleware.ps1 b/Testing/with_middleware.ps1 index 60da14a..e45dadd 100644 --- a/Testing/with_middleware.ps1 +++ b/Testing/with_middleware.ps1 @@ -1,5 +1,5 @@ # Author: Oriol Filter -# Date: 10/04/2022 +# Date: 22/05/2022 ## Vars -- 2.47.2