From 4edadde27dcdd18e502a8111c30327be4e7cfef2 Mon Sep 17 00:00:00 2001 From: ofilter Date: Thu, 7 Apr 2022 06:15:57 +0200 Subject: [PATCH] transitioning to scvmm --- Notes.md | 5 +- Testing/sirius.ps1 | 210 +++++++++++++++++++++++++++++++++++++++++++++ placeholder.md | 32 ++++++- 3 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 Testing/sirius.ps1 diff --git a/Notes.md b/Notes.md index 7936685..b5307ea 100644 --- a/Notes.md +++ b/Notes.md @@ -170,14 +170,15 @@ https://adamtheautomator.com/hyper-v-powershell/ ### Functions +[//]: # (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2) + ### Classes https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-7.2 https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-19/64 -[//]: # (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2) - +https://xainey.github.io/2016/powershell-classes-and-concepts/#introduction # Hypver-V specific diff --git a/Testing/sirius.ps1 b/Testing/sirius.ps1 new file mode 100644 index 0000000..2b93d59 --- /dev/null +++ b/Testing/sirius.ps1 @@ -0,0 +1,210 @@ +# Author: Oriol Filter +# Date: 06/04/2022 + + +$DebugPreference = "Continue" +$ErrorActionPreference = "Stop" +$SCRIPT_PATH=$PSScriptRoot +$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list.txt" +$SCVMM_FILE = "$SCRIPT_PATH/hyperv_list.txt" +$HOSTS_FILE = "$SCRIPT_PATH/hosts.txt" +## Objects + + +## Add Vhost objects for each type of server + +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=@{} + + __load_hosts(){ + $type=$this.GetType().BaseType + Write-Host "Load hosts not implemented in type $type" + } + + load_hosts(){ + $this.__host_list=@() + $this.__load_hosts() + } + + [Array] $__host_list=@() + [Array] $host_list{ + return $this.__host_list + } + +} +class SystemCenterVirtualMachineManager { + __load_hosts(){ + Get-SCVMMServer -ComputerName $this.url -TCPPort $this.port + $list = Get-SCVirtualMachine + $this.__host_list=$list +# foreach ($element in $list){$element.Name} + 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 +# } +} +class VMWareServer: VirtualizationServer{} + +class VirtualizerManager{ + [Array]$__connected_servers=@() + [Hashtable]$__found_vh=@{} + [Boolean] __log_in($url){ + $status_message="" + $result=$false + try { + $result=Invoke-Command -ComputerName $url { $true } + $status_message = "OK" + } + catch [System.Management.Automation.Remoting.PSRemotingTransportException] { + $status_message = Format("Error '{0}'",$_.Exception.GetType().fullname) + } + finally { + $message = [string]::Format(">> [{0}] {1}",$url,$status_message) + Write-Debug $message + } + return $result + } + [Hashtable] $found_vh{ + return $this.__found_vh + } + append_session($type,$url) + { + $type.GetType().IsValueType + $result = $this.__log_in($url) + if ($result) + { + $server=$type::new() + $server.url=$url + $this.__connected_servers+=$server + } + } + [Array] connected_servers() { + return $this.__connected_servers + } + + load_hosts() { + $this.__found_vh=@{} + 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 + } + } + } + __append_vhost($vhost,$source_url){ + if ($this.__host_list[$vhost.Name] -isnot [system.Hashtable]){ + $this.__host_list[$vhost.Name]=@{} + } + $this.__host_list[$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 + foreach ($vhostname in $hostlist) { + $item_from_list=$this.found_vh[$vhostname.ToString()] + [String]::Format("`t{0}:",$vhostname.name) | Write-Host + if (.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 + } + } + else { + [String]::Format("`t---- Couldn't be found ",$server.url) | 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) +{ + ## 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 +} + + +# 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_url = Get-Hosts-From-File($HYPERV_FILE) + foreach ($url in $hyperv_url) { + $manager.append_session([HypervisorServer], $url) + } +} + +function load_scvmm($manager) { + $hyperv_url = Get-Hosts-From-File($SCVMM_FILE) + foreach ($url in $hyperv_url) { + $manager.append_session([SystemCenterVirtualMachineManager], $url) + } +} + +function Main{ + $manager = [VirtualizerManager]::new() + load_scvmm($manager) + $manager.__connected_servers +# $vmhosts = Get-Hosts-From-File($HOSTS_FILE) +# $manager.find_host($vmhosts) +} + + +Main diff --git a/placeholder.md b/placeholder.md index 64ae247..ceae2bc 100644 --- a/placeholder.md +++ b/placeholder.md @@ -21,9 +21,39 @@ At C:\Users\Mamoncete\Desktop\test.ps1:93 char:17 El for fa el lio? +```sh You cannot call a method on a null-valued expression. At C:\Users\Mamoncete\Desktop\test.ps1:50 char:8 + $this._connected_servers[$num].FindHost("Test") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException - + FullyQualifiedErrorId : InvokeMethodOnNull \ No newline at end of file + + FullyQualifiedErrorId : InvokeMethodOnNull +``` + +====================================== +```sh +Get-SCVMMServer -ComputerName "VMMServer01.Contoso.com" -TCPPort 8100 +``` +https://docs.microsoft.com/en-us/powershell/module/virtualmachinemanager/get-scvmmserver?view=systemcenter-ps-2022 + +``` +Get-SCVMMServer -ComputerName "VMMServer01.Contoso.com" -TCPPort 8100 + +$list = Get-SCVirtualMachine + +$list is an array now + +foreach ($element in $list){$element.Name} +``` + +### GOD +``` +Get-Command -Module hyper-v | Out-GridView +``` + + +##### https://quick-adviser.com/where-can-i-find-scvmm-server/ + +##### Niputis + +https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40 \ No newline at end of file