Big progress.

This commit is contained in:
OriolFilter 2022-04-06 07:45:36 +02:00
parent bfcc1634f3
commit 330f111590
2 changed files with 39 additions and 32 deletions

View File

@ -11,32 +11,32 @@ $HOSTS_FILE = "$SCRIPT_PATH/hosts.txt"
class VirtualizationServer {
[string]$url
[Boolean] __FindHost ($vname) {
[Boolean] _FindHost ($vname) {
$type=$this.GetType().BaseType
Write-Output "Find Host not implemented type $type"
Write-Host "Find Host not implemented type $type"
return $false
}
[Boolean] FindHost ($vname) {
Write-Output 123
return $this.__FindHost($vname)
return $this._FindHost($vname)
}
}
class HypervisorServer: VirtualizationServer{
[Boolean] __FindHost ($vname) {
# Invoke-Command -ComputerName $this.url { Get-VM } | Write-Host
[Boolean] _FindHost ($vname) {
$result=$false
try
{
$result=Invoke-Command -ComputerName $this.url {
Invoke-Command -ComputerName $this.url {
Param ($name) Get-VM -Name $name
} -ArgumentList $vname | Out-Null
} -ArgumentList $vname
$result=$?
}
catch
{
Format ("{0}: {1}",$this.url,$Error[0]) | Write-Debug
}
return $result
}
}
@ -87,49 +87,46 @@ class VirtualizerManager{
}
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)
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
}
}
}
}
}
# Get hyperv's
function Get-Hosts-From-File($filepath)
{
# Get hyperv's
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
}
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) {
# 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)
$manager.append_session([HypervisorServer], $url)
}
}
}
function Main{
function Main{
$manager = [VirtualizerManager]::new()
load_hyperv($manager)
$manager.connected_servers()
$vmhosts= Get-Hosts-From-File($HOSTS_FILE)
$manager.find_host($vmhosts)
}

View File

@ -16,4 +16,14 @@ At C:\Users\Mamoncete\Desktop\test.ps1:93 char:17
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MethodNotFound
```
```
El for fa el lio?
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