diff --git a/Notes.md b/Notes.md index 1bd5aa4..7936685 100644 --- a/Notes.md +++ b/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) diff --git a/Testing/hosts.txt b/Testing/hosts.txt new file mode 100644 index 0000000..76c8bc8 --- /dev/null +++ b/Testing/hosts.txt @@ -0,0 +1,3 @@ +test1 +test5 +rest1 diff --git a/Testing/hyperv_list b/Testing/hyperv_list deleted file mode 100644 index 2f8036f..0000000 --- a/Testing/hyperv_list +++ /dev/null @@ -1,2 +0,0 @@ -host1 -host2 host3 \ No newline at end of file diff --git a/Testing/hyperv_list.txt b/Testing/hyperv_list.txt new file mode 100644 index 0000000..5b68443 --- /dev/null +++ b/Testing/hyperv_list.txt @@ -0,0 +1,2 @@ +host1 +host2 host3 hv01 \ No newline at end of file diff --git a/Testing/simple_for.ps1 b/Testing/simple_for.ps1 index 9e29c4d..1d1dbc5 100644 --- a/Testing/simple_for.ps1 +++ b/Testing/simple_for.ps1 @@ -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 \ No newline at end of file +# 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 diff --git a/placeholder.md b/placeholder.md new file mode 100644 index 0000000..97eb9a2 --- /dev/null +++ b/placeholder.md @@ -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 \ No newline at end of file