From 7bda078c1d06ee3020a180745995aaffb6715c46 Mon Sep 17 00:00:00 2001 From: ofilter Date: Sun, 22 May 2022 14:51:20 +0200 Subject: [PATCH] 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 }