Listing vmware returns all the server if found.

This commit is contained in:
ofilter 2022-05-13 15:24:22 +02:00
parent f5973fa295
commit a31ceed069

View File

@ -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