Big progress.
This commit is contained in:
parent
1a72313e20
commit
d01aedb7c3
2
Notes.md
2
Notes.md
@ -174,6 +174,8 @@ https://adamtheautomator.com/hyper-v-powershell/
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
3
Testing/hosts.txt
Normal file
3
Testing/hosts.txt
Normal file
@ -0,0 +1,3 @@
|
||||
test1
|
||||
test5
|
||||
rest1
|
@ -1,2 +0,0 @@
|
||||
host1
|
||||
host2 host3
|
2
Testing/hyperv_list.txt
Normal file
2
Testing/hyperv_list.txt
Normal file
@ -0,0 +1,2 @@
|
||||
host1
|
||||
host2 host3 hv01
|
@ -1,72 +1,138 @@
|
||||
# Author: Oriol Filter
|
||||
# Date: 04/04/2022
|
||||
|
||||
# Date: 06/04/2022
|
||||
|
||||
|
||||
$DebugPreference = "Continue"
|
||||
$ErrorActionPreference = "Stop"
|
||||
$SCRIPT_PATH=$PSScriptRoot
|
||||
|
||||
$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list.txt"
|
||||
$HOSTS_FILE = "$SCRIPT_PATH/hosts.txt"
|
||||
## Objects
|
||||
|
||||
class VirtualizationServer {
|
||||
[string]$URL
|
||||
[bool]$Authenticated=$false
|
||||
[string]$url
|
||||
[Boolean] __FindHost ($vname) {
|
||||
$type=$this.GetType().BaseType
|
||||
Write-Output "Find Host not implemented type $type"
|
||||
return $false
|
||||
}
|
||||
[Boolean] FindHost ($vname) {
|
||||
Write-Output 123
|
||||
return $this.__FindHost($vname)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class HypervServer: VirtualizationServer{}
|
||||
class HypervisorServer: VirtualizationServer{
|
||||
[Boolean] __FindHost ($vname) {
|
||||
# Invoke-Command -ComputerName $this.url { Get-VM } | Write-Host
|
||||
$result=$false
|
||||
try
|
||||
{
|
||||
$result=Invoke-Command -ComputerName $this.url {
|
||||
Param ($name) Get-VM -Name $name
|
||||
} -ArgumentList $vname | Out-Null
|
||||
}
|
||||
catch
|
||||
{
|
||||
Format ("{0}: {1}",$this.url,$Error[0]) | Write-Debug
|
||||
}
|
||||
return $result
|
||||
}
|
||||
}
|
||||
class VMWareServer: VirtualizationServer{}
|
||||
|
||||
class VirtualizerManager{
|
||||
[Array]$_connected_servers=@()
|
||||
[Hashtable]$_found_vm=@{}
|
||||
[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
|
||||
}
|
||||
append_session($type,$url)
|
||||
{
|
||||
# $Allowed_Types = @(
|
||||
# VMWareServer;
|
||||
# HypervisorServer
|
||||
# )
|
||||
#$Allowed_Types
|
||||
$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
|
||||
}
|
||||
find_host($vname){
|
||||
$hostlist=$vname
|
||||
if ($hostlist -is [Array]){
|
||||
$hostlist=@($hostlist)
|
||||
}
|
||||
foreach ($_vname in $hostlist)
|
||||
{
|
||||
foreach ($server in $this.connected_servers)
|
||||
{
|
||||
Write-Host "<>< $server ><>"
|
||||
$result = $server.FindHost($_vname)
|
||||
Write-Host [String]::Format(">> {0} found in {1}? {2}", $_vname, $server.url, $result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
# Get hyperv's
|
||||
filter Get-Hyperv
|
||||
function Get-Hosts-From-File($filepath)
|
||||
{
|
||||
|
||||
$HYPERV_FILE = "$SCRIPT_PATH/hyperv_list"
|
||||
|
||||
## In a future use a yaml as a .conf (with the hyperv and vmware listed)
|
||||
#$__HYPERV_FILE = Get-Content "$_SCRIPT_PATH"
|
||||
$HYPERV_FILE = "host1
|
||||
host2 host3"
|
||||
|
||||
|
||||
$HYPERV_LIST = $HYPERV_FILE.Split()
|
||||
|
||||
|
||||
$HYPERV_ARRAY=@()
|
||||
foreach ($url in $HYPERV_LIST)
|
||||
$file_content = Get-Content "$filepath"
|
||||
$splitted_file_content = $file_content.Split()
|
||||
$host_array=@()
|
||||
foreach ($hostname in $splitted_file_content)
|
||||
{
|
||||
if ($url)
|
||||
if ($hostname)
|
||||
{
|
||||
[HypervServer]$new_hyerv=[HypervServer]::new()
|
||||
$new_hyerv.URL=$url
|
||||
$HYPERV_ARRAY+=$new_hyerv
|
||||
$host_array+=$hostname
|
||||
}
|
||||
}
|
||||
return $HYPERV_ARRAY
|
||||
return $host_array
|
||||
}
|
||||
|
||||
function Check-Access($hv_arr) {
|
||||
foreach ($connection in $hv_arr)
|
||||
{
|
||||
Invoke-Command -ComputerName $connection.URL | out-null
|
||||
$connection.Authenticated=$?
|
||||
}
|
||||
}
|
||||
|
||||
function Print-Connections-Status($vs_arr) {
|
||||
foreach ($connection in $vs_arr)
|
||||
{
|
||||
$connection
|
||||
}
|
||||
}
|
||||
|
||||
$hyperv_arr=Get-Hyperv
|
||||
|
||||
Check-Access($hv_arr)
|
||||
#$hyperv_arr.GetType()
|
||||
|
||||
Print-Connections-Status($hyperv_arr)
|
||||
|
||||
|
||||
|
||||
# Advanced params?
|
||||
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.2
|
||||
# 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 Main{
|
||||
$manager = [VirtualizerManager]::new()
|
||||
load_hyperv($manager)
|
||||
$manager.connected_servers()
|
||||
$vmhosts= Get-Hosts-From-File($HOSTS_FILE)
|
||||
$manager.find_host($vmhosts)
|
||||
}
|
||||
|
||||
|
||||
Main
|
||||
|
7
placeholder.md
Normal file
7
placeholder.md
Normal file
@ -0,0 +1,7 @@
|
||||
something |Select –Property
|
||||
Get-Member
|
||||
|
||||
Select-Object
|
||||
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-7.2
|
||||
|
||||
Get-Process Explorer | Select-Object -Property ProcessName -ExpandProperty Modules | Format-List
|
Loading…
x
Reference in New Issue
Block a user