0

There is this existing PS snippet that works as intended:

$creds = New-Object -TypeName System.Management.Automation.PSCredential ("USERID", (new-object System.Security.SecureString));
$FullVM | Where-Object {$_.runtime.powerState -eq "PoweredOn"} | 
    Select-Object -Property Name,@{N="GuestId";E={$_.Guest.GuestId}},
      @{N="Installed Guest OS";E={$_.Guest.GuestFullName}},
      @{N="Configured Guest OS";E={$_.Config.GuestFullName}},
      @{N="ACCESS";E={$CurrentSession=New-SSHSession -ComputerName $_.Name -AcceptKey -keyfile C:\temp\key.txt -Credential $creds ;(Invoke-SSHCommand -SSHSession $CurrentSession -Command "echo YES" -OutVariable result).output}} 

Basically, from what I understand, it collects data and saves them to variables 'or objects?'.

For the last line, it tries to SSH to remote machine(defined in $_.Name) and echoes "YES" if successful and it gets saved to the 'object' "ACCESS":

@{N="ACCESS";E={$CurrentSession=New-SSHSession -ComputerName $_.Name -AcceptKey Credential $creds ;(Invoke-SSHCommand -SSHSession $CurrentSession -Command "echo YES" -OutVariable result).output}}

The issue that I'm running into right now is trying to figure out how to inject/attach/rewrite that same line into this:

  foreach ($vmguest in $FullVM) {
       $vmguest.Config.Hardware.Device | Where-Object {$vmguest.Guest.GuestFullName -cnotlike "*Microsoft*" -and  $_.GetType().Name -match $unwantedHardware} | Foreach-Object {
          New-Object -TypeName PSObject -Property @{
             Name = $vmguest.name
             Label = $_.DeviceInfo.Label
             OS = $vmguest.Guest.GuestFullName
          }
       }
    }

How do I rewrite this so it would still provide the same function as from the previous snippet? Do I write it like this?

foreach ($vmguest in $FullVM) {
       $vmguest.Config.Hardware.Device | Where-Object {$vmguest.Guest.GuestFullName -cnotlike "*Microsoft*" -and  $_.GetType().Name -match $unwantedHardware} | Foreach-Object {
          New-Object -TypeName PSObject -Property @{
             Name = $vmguest.name
             Label = $_.DeviceInfo.Label
             OS = $vmguest.Guest.GuestFullName
             @{N="ACCESS";E={$CurrentSession=New-SSHSession -ComputerName $_.Name -AcceptKey Credential $creds ;(Invoke-SSHCommand -SSHSession $CurrentSession -Command "echo YES" -OutVariable result).output}}
          }
       }
    }

Could you please guide me on how to accomplish this? Thank you very much in advance!

pilipeet
  • 47
  • 6
  • that line is NOT saving OR creating a $var named `ACCESS`. it uses `-OutVariable` to create a $Var named `$Result` and sends the `.Output` to the final calculated prop. it also requires some sort of input object that already has several properties. the `Select-Object` is taking props from the input object, creating a new object with the selected & calculated props, and saving that into a new object. ///// unfortunately, i can't tell where the final object goes since **_the snippet is not all there_**. [*grin*] – Lee_Dailey Feb 07 '19 at 18:40
  • so ... what are you trying to do? what output of the incomplete snippet do you want, where do you want it, and what is it supposed to DO? [*grin*] – Lee_Dailey Feb 07 '19 at 18:42
  • Forgive my lack of understanding of Powershell. The final objects get printed to a report, in html format. I just want to rewrite the second snippet to something like this: `code` OS = $vmguest.Guest.GuestFullName @{N="ACCESS";E={$CurrentSession=New-SSHSession -ComputerName $_.Name -AcceptKey -keyfile C:\temp\key.txt -Credential $creds ;(Invoke-SSHCommand -SSHSession $CurrentSession -Command "echo YES" -OutVariable result).output}} `code` What I'm trying to discover is the correct way of including/defining the object 'ACCESS' in this snippet. – pilipeet Feb 07 '19 at 18:50
  • Sorry, I meant to say was how do I rewrite the following object definition so it would work in the second snippet of code? @{N="ACCESS";E={$CurrentSession=New-SSHSession -ComputerName $_.Name -AcceptKey -keyfile C:\temp\key.txt -Credential $creds ;(Invoke-SSHCommand -SSHSession $CurrentSession -Command "echo YES" -OutVariable result).output}} – pilipeet Feb 07 '19 at 18:51
  • without the remainder of the code surrounding that snippet, there is no way to do it. for instance, the `$_.Name` is coming from the _input_ to the `Select-Object` cmdlet. plus, it uses what appears to be a custom command - `Invoke-SSHCommand`. ///// you will need to rewrite your OP to detail what you are trying to achieve, not HOW ... but the why of it all. – Lee_Dailey Feb 07 '19 at 19:19

0 Answers0