Added VM object

This commit is contained in:
ofilter 2022-04-09 04:23:09 +02:00
parent 98025e135d
commit 799adece03
3 changed files with 109 additions and 61 deletions

View File

@ -213,4 +213,13 @@ Get-VM -ComputerName Server1
```powershell
Connect-VIServer
```
```
## Snapshot
https://developer.vmware.com/docs/powercli/latest/products/vmwarevsphereandvsan/categories/snapshot/
Find hosts, iterate with domains?
$Hostname+.domain.dom

View File

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

View File

@ -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
}
https://livebook.manning.com/book/windows-powershell-in-action-third-edition/chapter-2/40