From 330f1115901abd5428433eb5f4cd8723f4ffce6c Mon Sep 17 00:00:00 2001 From: OriolFilter Date: Wed, 6 Apr 2022 07:45:36 +0200 Subject: [PATCH] Big progress. --- Testing/simple_for.ps1 | 59 ++++++++++++++++++++---------------------- placeholder.md | 12 ++++++++- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/Testing/simple_for.ps1 b/Testing/simple_for.ps1 index 1d1dbc5..59cf943 100644 --- a/Testing/simple_for.ps1 +++ b/Testing/simple_for.ps1 @@ -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) } diff --git a/placeholder.md b/placeholder.md index 843e617..64ae247 100644 --- a/placeholder.md +++ b/placeholder.md @@ -16,4 +16,14 @@ At C:\Users\Mamoncete\Desktop\test.ps1:93 char:17 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MethodNotFound -``` \ No newline at end of file +``` + + +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 \ No newline at end of file