powershellpowercli

How do I use a certain output as a variable (in PowerShell)?


I have created a simple PowerShell/PowerCLI Script:

Connect-VIServer -Server vcsa-01a.corp.local -User administrator@vsphere.local -Password VMware1!
Get-VM web-01a | Get-NetworkAdapter
Get-VM web-02a | Get-NetworkAdapter
Get-VM db-01a | Get-NetworkAdapter

This script produces this output:

PS C:\my\podStatusCheck> .\TestVM.ps1

Name                           Port  User
----                           ----  ----
vcsa-01a.corp.local            443   VSPHERE.LOCAL\Administrator

MacAddress       : 00:50:56:a1:46:33
WakeOnLanEnabled : True
NetworkName      : ov-web-stretched
Type             : Vmxnet3
ParentId         : VirtualMachine-vm-21001
Parent           : web-01a
Uid              : /VIServer=vsphere.local\administrator@vcsa-01a.corp.local:443/VirtualMachine=VirtualMachine-vm-21001/NetworkAdapter=4000/
ConnectionState  : NotConnected, GuestControl, StartConnected
ExtensionData    : VMware.Vim.VirtualVmxnet3
Id               : VirtualMachine-vm-21001/4000
Name             : Network adapter 1


MacAddress       : 00:50:56:a1:07:e6
WakeOnLanEnabled : True
NetworkName      : ov-web-stretched
Type             : Vmxnet3
ParentId         : VirtualMachine-vm-4019
Parent           : web-02a
Uid              : /VIServer=vsphere.local\administrator@vcsa-01a.corp.local:443/VirtualMachine=VirtualMachine-vm-4019/NetworkAdapter=4000/
ConnectionState  : NotConnected, GuestControl, StartConnected
ExtensionData    : VMware.Vim.VirtualVmxnet3
Id               : VirtualMachine-vm-4019/4000
Name             : Network adapter 1


MacAddress       : 00:50:56:a1:99:0f
WakeOnLanEnabled : True
NetworkName      : ov-db-stretched
Type             : Vmxnet3
ParentId         : VirtualMachine-vm-4017
Parent           : db-01a
Uid              : /VIServer=vsphere.local\administrator@vcsa-01a.corp.local:443/VirtualMachine=VirtualMachine-vm-4017/NetworkAdapter=4000/
ConnectionState  : NotConnected, GuestControl, StartConnected
ExtensionData    : VMware.Vim.VirtualVmxnet3
Id               : VirtualMachine-vm-4017/4000
Name             : Network adapter 1

I want to check if the VM is part using a specific network.

So something like this:

If (VM web-01a.Network Name -like "ov-web-stretched")
    
    {
    Write-Output "The VM is is the right network"
    }
else
    {
    Write-Output "The VM is is the wrong network"
    }

But I don't know how I call the variables, or how I put certain lines of part of the output in a variable.

Can someone help me with this, please?

I really appreciate any help you can provide.

I want to do a check if a certain virtual machine uses a specific network, if so I want to generate a SUCCESS message, if the virtual machine is not part of this network it should provide me a failure message.


Solution

  • You can try to assign output of certain commands to variables and then access their properties in condition, like this:

    $vm1 = Get-VM web-01a | Get-NetworkAdapter
    $vm2 = Get-VM web-02a | Get-NetworkAdapter
    $vm3 = Get-VM db-01a | Get-NetworkAdapter
    If ($vm1.NetworkName -like "ov-web-stretched") {
        Write-Output "The VM is is the right network"
    }
    else {
        Write-Output "The VM is is the wrong network"
    }