master #6
51
Notes.md
51
Notes.md
@ -4,19 +4,19 @@
|
||||
## Data storing
|
||||
|
||||
### Variable
|
||||
```ps
|
||||
```powershell
|
||||
$x="Text"
|
||||
```
|
||||
|
||||
#### Invoking / Data types
|
||||
|
||||
##### String
|
||||
```ps
|
||||
```powershell
|
||||
[string]$x="Text"
|
||||
```
|
||||
|
||||
##### Int
|
||||
```ps
|
||||
```powershell
|
||||
PS C:\Windows\system32> [int]$x=1
|
||||
PS C:\Windows\system32> [int]$x="1"
|
||||
PS C:\Windows\system32> [int]$x="1a"
|
||||
@ -34,7 +34,7 @@ At line:1 char:1
|
||||
|
||||
### Array
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
$arry = @("a", "b", "c")
|
||||
```
|
||||
|
||||
@ -44,13 +44,13 @@ $arry = @("a", "b", "c")
|
||||
|
||||
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ee692803(v=technet.10)?redirectedfrom=MSDN
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
[ordered]@{ <name> = <value>; [<name> = <value> ] ...}
|
||||
```
|
||||
|
||||
(By default sorts by values, I believe ordered means the array trust you as the elements are ordered.)
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
$X = [ordered]@{A=1;C=2;B=3}
|
||||
```
|
||||
|
||||
@ -59,19 +59,19 @@ $X = [ordered]@{A=1;C=2;B=3}
|
||||
|
||||
#### *Normal* Array
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
@("a", "b", "c")[0]
|
||||
>> a
|
||||
```
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
@("a", "b", "c")[-1 ]
|
||||
>> c
|
||||
```
|
||||
|
||||
#### Hash array
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
@{A=1;C=3;B=2}["A"]
|
||||
>> 1
|
||||
```
|
||||
@ -95,25 +95,25 @@ Write-Host
|
||||
```
|
||||
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Write-Host "text"
|
||||
```
|
||||
|
||||
#### Write-Output
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Write-Output "text"
|
||||
```
|
||||
|
||||
## Filtering
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Write-Output "text" | Select-String -pattern $filters -notMatch
|
||||
```
|
||||
|
||||
### Select-Object
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Select-Object HostName
|
||||
```
|
||||
|
||||
@ -121,13 +121,13 @@ Select-Object HostName
|
||||
|
||||
### Read file
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Get-Content $path
|
||||
```
|
||||
|
||||
### write to file
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Write-Host $text | Out-File $path
|
||||
```
|
||||
|
||||
@ -135,7 +135,7 @@ Write-Host $text | Out-File $path
|
||||
|
||||
### Check if last command executed correctly
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
# Do something
|
||||
echo $?
|
||||
```
|
||||
@ -150,14 +150,14 @@ echo $?
|
||||
|
||||
## Management
|
||||
### Access remote PS server
|
||||
```ps
|
||||
```powershell
|
||||
Enter-PSSession –ComputerName $hostname [-Credential username]
|
||||
```
|
||||
> If username gets left behind, the current user will be used
|
||||
### Send command remotely
|
||||
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
Invoke-Command -ComputerName hostname1, hostname2 -ScroptBlock {$COMMAND}
|
||||
```
|
||||
|
||||
@ -189,12 +189,12 @@ https://xainey.github.io/2016/powershell-classes-and-concepts/#introduction
|
||||
|
||||
### Create new host
|
||||
|
||||
```ps
|
||||
```powershell
|
||||
New-VM -Name "HYPER" -MemoryStartupBytes 512MB
|
||||
```
|
||||
|
||||
### List current VM
|
||||
```ps
|
||||
```powershell
|
||||
Get-VMHost
|
||||
[[-ComputerName] <String[]>]
|
||||
[[-Credential] <PSCredential[]>]
|
||||
@ -202,6 +202,15 @@ Get-VMHost
|
||||
```
|
||||
#### Example
|
||||
##### Find VM by name
|
||||
```ps
|
||||
```powershell
|
||||
Get-VM -ComputerName Server1
|
||||
```
|
||||
|
||||
|
||||
# VMWARE
|
||||
|
||||
## Connect-VIServer
|
||||
|
||||
```powershell
|
||||
Connect-VIServer
|
||||
```
|
@ -31,6 +31,7 @@ class VirtualizationServer {
|
||||
}
|
||||
|
||||
load_hosts(){
|
||||
[String]::Format("Loading VM from: {0}",$this.url) | Write-Debug
|
||||
$this.__host_list=@()
|
||||
$this.__load_hosts()
|
||||
}
|
||||
@ -102,7 +103,7 @@ class VirtualizerManager{
|
||||
[String]::Format("{0}",$Error[0]) | Write-Debug
|
||||
}
|
||||
finally {
|
||||
$message = [string]::Format(">>`tStatus {0}",@("NotOK","OK"))
|
||||
$message = [string]::Format(">>`tStatus {0}",@("NotOK","OK")[$result])
|
||||
Write-Host $message
|
||||
}
|
||||
return $result
|
||||
|
285
Testing/with_middleware.ps1
Normal file
285
Testing/with_middleware.ps1
Normal file
@ -0,0 +1,285 @@
|
||||
# Author: Oriol Filter
|
||||
# Date: 08/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 VirtualVM{
|
||||
[VirtualizationServer]$__ServerObject
|
||||
[Object]$__VMObject
|
||||
[VirtualizationServer]Get_ServerObject(){
|
||||
return $this.__ServerObject
|
||||
}
|
||||
Set_ServerObject([VirtualizationServer] $Server){
|
||||
[VirtualizationServer] $this.__ServerObject=$Server
|
||||
}
|
||||
Set_VMObject([Object] $object){
|
||||
$this.__VMObject=$object
|
||||
}
|
||||
[Object]Get_VMObject(){
|
||||
return $this.__VMObject.PowerState
|
||||
}
|
||||
|
||||
[String]Get_Name(){
|
||||
return $this.__ServerObject.Name
|
||||
}
|
||||
[bool]Get_Is_Running(){
|
||||
return @{PoweredOn=$true;PoweredOff=$false}[$this.__VMObject.PowerState]
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
class SCVMM_VM:VirtualVM{
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
# }
|
||||
|
||||
__load_hosts(){
|
||||
$type=$this.GetType().BaseType
|
||||
Write-Warning "Load hosts not implemented in type $type"
|
||||
}
|
||||
|
||||
load_hosts(){
|
||||
[String]::Format("Loading VM from: {0}",$this.url) | Write-Debug
|
||||
$this.__host_list=@()
|
||||
$this.__load_hosts()
|
||||
}
|
||||
|
||||
[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(){
|
||||
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){
|
||||
$result=Invoke-Command -ComputerName $this.url { $true }
|
||||
$status_message = "OK"
|
||||
return $result
|
||||
}
|
||||
}
|
||||
class HypervisorServer: VirtualizationServer{
|
||||
[Boolean] __log_in($url){
|
||||
$result=Invoke-Command -ComputerName $this.url { $true }
|
||||
$status_message = "OK"
|
||||
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{
|
||||
|
||||
}
|
||||
|
||||
class VirtualizerMiddleware{
|
||||
[Array]$__connected_servers=@()
|
||||
[Hashtable]$__found_host_dict=@{}
|
||||
[Boolean] __log_in($url){
|
||||
[String]::Format("-- {0}:",$url) | Write-Host
|
||||
$status_message=""
|
||||
$result=$false
|
||||
try {
|
||||
$result=Invoke-Command -ComputerName $url { $true }
|
||||
}
|
||||
catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
|
||||
[String]::Format("Failed to log in to URL: {0}",$url) | Write-Warning
|
||||
[String]::Format("{0}",$Error[0]) | Write-Debug
|
||||
}
|
||||
finally {
|
||||
$message = [string]::Format(">>`tStatus {0}",@("NotOK","OK")[$result])
|
||||
Write-Host $message
|
||||
}
|
||||
return $result
|
||||
}
|
||||
hidden [Hashtable] get_found_host_dict() {return $this.__found_host_dict}
|
||||
|
||||
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_host_dict=@{}
|
||||
for ($num = 0 ; $num -le $this.__connected_servers.Length -1 ; $num++){
|
||||
$server = $this.__connected_servers[$num]
|
||||
$server.load_hosts()
|
||||
foreach ($vhost in $server.get_host_list())
|
||||
{
|
||||
$this.append_vhost($vhost,$server.url)
|
||||
}
|
||||
}
|
||||
}
|
||||
__append_vhost($vhost,$source_url){
|
||||
if ($this.__found_host_dict[$vhost.Name] -isnot [Hashtable]){
|
||||
$this.__found_host_dict[$vhost.Name]=@{}
|
||||
}
|
||||
$this.__found_host_dict[$vhost.Name][$source_url]=$vhost
|
||||
}
|
||||
append_vhost($vhost,$source_url){
|
||||
$this.__append_vhost($vhost,$source_url)
|
||||
}
|
||||
find_hosts($vname){
|
||||
$hosts_to_find=$vname
|
||||
if ($hosts_to_find -isnot [Array]){
|
||||
$hosts_to_find=@("$hosts_to_find")
|
||||
}
|
||||
foreach ($hostname in $hosts_to_find) {
|
||||
$entry_from_list=$this.__found_host_dict[$hostname]
|
||||
[String]::Format("`t{0}:",$hostname) | Write-Host
|
||||
if ($entry_from_list.Length -gt 0)
|
||||
{
|
||||
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"
|
||||
}
|
||||
else {
|
||||
[String]::Format("`----------- Couldn't be found`n") | 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_urls = Get-Hosts-From-File($HYPERV_FILE)
|
||||
foreach ($url in $hyperv_urls) {
|
||||
$manager.append_session([HypervisorServer], $url)
|
||||
}
|
||||
}
|
||||
|
||||
function load_scvmm($manager) {
|
||||
$hyperv_urls = Get-Hosts-From-File($SCVMM_FILE)
|
||||
foreach ($url in $hyperv_urls) {
|
||||
$manager.append_session([SystemCenterVirtualMachineManager], $url)
|
||||
}
|
||||
}
|
||||
|
||||
function Main{
|
||||
$manager = [VirtualizerMiddleware]::new()
|
||||
load_scvmm($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]
|
||||
}
|
||||
|
||||
Main
|
||||
|
||||
# if file is empty -> Debug+warn + skipping message (returns empty array)
|
||||
# add vmware support
|
||||
# in scvmm check if vname stored are actually the whole object or just the name
|
||||
# add middleware
|
||||
# do menus
|
||||
# store data into custom classes, that way one with custom methods can access the information one way or another, but it's there
|
||||
# add a menu()
|
||||
# get_location
|
||||
# get_status
|
||||
# list_snaps
|
||||
# Replace virtual host terminology with virtual machine
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user