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!