From cdd1ea752dec602932b30b74c68f813766f5f3b1 Mon Sep 17 00:00:00 2001 From: ofilter Date: Thu, 7 Apr 2022 08:55:03 +0200 Subject: [PATCH] transitioning to scvmm --- Testing/sirius.ps1 | 188 ++++++++++++++++++++++++++------------------- 1 file changed, 110 insertions(+), 78 deletions(-) diff --git a/Testing/sirius.ps1 b/Testing/sirius.ps1 index 0d417e5..3d6fb7d 100644 --- a/Testing/sirius.ps1 +++ b/Testing/sirius.ps1 @@ -15,20 +15,19 @@ $HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" class VirtualizationServer { [int]$port=8100 - [string]$url -# [Boolean] _FindHost ($vname) { -# $type=$this.GetType().BaseType -# Write-Host "Find Host not implemented in type $type" -# return $false -# } -# [Boolean] FindHost ($vname) { -# return $this._FindHost($vname) -# } - [Hashtable]$__hostlist=@{} + [string]$url="" + # [Boolean] _FindHost ($vname) { + # $type=$this.GetType().BaseType + # Write-Host "Find Host not implemented in type $type" + # return $false + # } + # [Boolean] FindHost ($vname) { + # return $this._FindHost($vname) + # } __load_hosts(){ $type=$this.GetType().BaseType - Write-Host "Load hosts not implemented in type $type" + Write-Warning "Load hosts not implemented in type $type" } load_hosts(){ @@ -36,42 +35,43 @@ class VirtualizationServer { $this.__load_hosts() } - hidden [Array] $__host_list=@() - [Array] $host_list = $this.__host_list - + [Array] $__host_list=@() + [Array] get_host_list(){ + return $this.__host_list + } } -class SystemCenterVirtualMachineManager { +class SystemCenterVirtualMachineManager: VirtualizationServer { __load_hosts(){ - Get-SCVMMServer -ComputerName $this.url -TCPPort $this.port + Get-SCVMMServer -ComputerName $this.url $list = Get-SCVirtualMachine $this.__host_list=$list -# foreach ($element in $list){$element.Name} - Format("Loaded VM from {0}",$this.url) | Write-Debug + [string]::Format("Loaded VM from {0}",$this.url) | Write-Debug + } } class HypervisorServer: VirtualizationServer{ -# [Boolean] _FindHost ($vname) { -# $result=$false -# try -# { -# Invoke-Command -ComputerName $this.url { -# Param ($name) Get-VM -Name $name -# } -ArgumentList $vname -# $result=$? -# } -# catch -# { -# Format ("{0}: {1}",$this.url,$Error[0]) | Write-Debug -# } -# -# return $result -# } + # [Boolean] _FindHost ($vname) { + # $result=$false + # try + # { + # Invoke-Command -ComputerName $this.url { + # Param ($name) Get-VM -Name $name + # } -ArgumentList $vname + # $result=$? + # } + # catch + # { + # Format ("{0}: {1}",$this.url,$Error[0]) | Write-Debug + # } + # + # return $result + # } } class VMWareServer: VirtualizationServer{} class VirtualizerManager{ [Array]$__connected_servers=@() - [Hashtable]$__found_vh=@{} + [Hashtable]$__found_host_dict=@{} [Boolean] __log_in($url){ $status_message="" $result=$false @@ -88,7 +88,7 @@ class VirtualizerManager{ } return $result } - [Hashtable] $found_vh = $this.__found_vh + hidden [Hashtable] get_found_host_dict() {return $this.__found_host_dict} append_session($type,$url) { @@ -106,60 +106,78 @@ class VirtualizerManager{ } load_hosts() { - $this.__found_vh=@{} + $this.__found_host_dict=@{} for ($num = 0 ; $num -le $this.__connected_servers.Length -1 ; $num++){ - $server = $this.__connected_servers[$num] - $server.load_hosts() - foreach ($item in $server.host_list) - { - $this.append_vhost - } + $server = $this.__connected_servers[$num] + $server.load_hosts() + foreach ($vhost in $server.get_host_list()) + { +# $item.name | Write-Host + $this.append_vhost($vhost,$server.url) + } } } __append_vhost($vhost,$source_url){ - if ($this.__host_list[$vhost.Name] -isnot [Hashtable]){ - $this.__host_list[$vhost.Name]=@{} + if ($this.__found_host_dict[$vhost.Name] -isnot [Hashtable]){ + $this.__found_host_dict[$vhost.Name]=@{} } - $this.__host_list[$vhost.Name.ToLower()][$source_url]=$vhost + $this.__found_host_dict[$vhost.Name.ToLower()][$source_url]=$vhost } append_vhost($vhost,$source_url){ $this.__append_vhost($vhost,$source_url) } - find_host($vname){ - $hostlist=$vname - if ($hostlist -is [Array]){ - $hostlist=@($hostlist) + find_hosts($vname){ + $hosts_to_find=$vname + if ($hosts_to_find -isnot [Array]){ + $hosts_to_find=@("$hosts_to_find") } # Find hosts - foreach ($vhostname in $hostlist) { - $item_from_list=$this.found_vh[$vhostname.ToString()] - [String]::Format("`t{0}:",$vhostname.name) | Write-Host - if (.Length > 0) + foreach ($hostname in $hosts_to_find) { + $lhostname=$hostname.ToLower() + $entry_from_list=$this.__found_host_dict[$lhostname] + [String]::Format("`t{0}:",$hostname) | Write-Host +# $item_from_list |Write-Host +# foreach ($a in $item_from_list) {Write-Host $a} +# $this.found_host_dict[$vhostname.ToString()].Lenght | Write-Host +# +# $item_from_list.Length | Write-Host + $entry_from_list.Length | Write-Host + Write-Host "IF" + if ($entry_from_list.Length > 0) { - for ($num = 0 ; $num -le $item_from_list.Length -1 ; $num++){ - $server = $item_from_list[$num] - [String]::Format("`t{0}:",$server.url) | Write-Host + Write-Host "Inner IF" + ## Not being executed? + Write-Host "IF" + Write-Host "!!" + foreach ($server_location in $entry_from_list) { + { + $server_location | Write-Host + $vm = $this.__found_host_dict[$lhostname][$server_location] + #$vm | Write-Host + [String]::Format("{0}:",$server_location) | Write-Host + } + } + else { + [String]::Format("---- Couldn't be found") | Write-Host } } - else { - [String]::Format("`t---- Couldn't be found") | Write-Host - } } + } -# find_host($vname){ -# $hostlist=$vname -# if ($hostlist -is [Array]){ -# $hostlist=@($hostlist) -# } -# foreach ($_vname in $hostlist) -# { -# for ($num = 0 ; $num -le $this.__connected_servers.Length -1 ; $num++){ -# $server= $this.__connected_servers[$num] -# $result = $server.FindHost($_vname) -# [String]::Format(">> {0} found in {1}? {2}", $_vname, $server.url, $result) | Write-Host -# } -# } -# } + # find_host($vname){ + # $hostlist=$vname + # if ($hostlist -is [Array]){ + # $hostlist=@($hostlist) + # } + # foreach ($_vname in $hostlist) + # { + # for ($num = 0 ; $num -le $this.__connected_servers.Length -1 ; $num++){ + # $server= $this.__connected_servers[$num] + # $result = $server.FindHost($_vname) + # [String]::Format(">> {0} found in {1}? {2}", $_vname, $server.url, $result) | Write-Host + # } + # } + # } } function Get-Hosts-From-File($filepath) @@ -170,7 +188,7 @@ function Get-Hosts-From-File($filepath) $host_array=@() foreach ($hostname in $splitted_file_content) { - if ($hostname) + if ($hostname) { $host_array+=$hostname } @@ -198,10 +216,24 @@ function load_scvmm($manager) { function Main{ $manager = [VirtualizerManager]::new() load_scvmm($manager) - $manager.__connected_servers -# $vmhosts = Get-Hosts-From-File($HOSTS_FILE) -# $manager.find_host($vmhosts) + $manager.load_hosts() + $vmhosts = Get-Hosts-From-File($HOSTS_FILE) + +# $manager.get_found_host_dict() +# foreach ($hostname in $manager.get_found_host_dict()){ +# Write-Output $hostname.Name +# } + $manager.__found_host_dict.Lenght + $manager.find_hosts($vmhosts) } +function Test_VCSMM{ + $manager = [VirtualizerManager]::new() + load_scvmm($manager) + [VirtualizationServer]$x=$manager.__connected_servers[0] + $x.load_hosts() + $x.get_host_list() +} +#Test_VCSMM Main