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

100 Views Asked by At

I have created a simple PowerShell/PowerCLI Script:

Connect-VIServer -Server vcsa-01a.corp.local -User [email protected] -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\[email protected]: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\[email protected]: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\[email protected]: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.

3

There are 3 best solutions below

0
Gennadii Saltyshchak On BEST ANSWER

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"
}
1
Avshalom On

You can structure you code differently.

Connect-VIServer -Server vcsa-01a.corp.local[...]
$VMs = Get-VM
foreach ($vm in $VMs)
{
    $Network = $vm | Get-NetworkAdapter

    if ($Network.NetworkName -like "ov-web-stretched")
    {
        "Do Something"
    }
}
0
Theo On

You could simply loop over the array of VM names you have and test each one like this:

Connect-VIServer -Server vcsa-01a.corp.local -User [email protected] -Password VMware1!

'web-01a', 'web-02a', 'db-01a' | ForEach-Object {
    $network = (Get-VM $_ | Get-NetworkAdapter).NetworkName
    if ($network -like '*ov-web-stretched*') {
        "$_ is in the correct network ($network)"
    }
    else {
        "$_ is in the wrong network ($network)"
    }
}

Or output objects which will give you the advantage of outputting a table and if you like export the results to csv file:

Connect-VIServer -Server vcsa-01a.corp.local -User [email protected] -Password VMware1!

$result = 'web-01a', 'web-02a', 'db-01a' | ForEach-Object {
    $network = (Get-VM $_ | Get-NetworkAdapter).NetworkName
    [PsCustomObject]@{
        VmName         = $_
        NetworkName    = $network
        CorrectNetwork = if ($network -like '*ov-web-stretched*') { 'SUCCESS' } else { 'FAILURE' }
    }
}

# output on screen
$result | Format-Table -AutoSize

# output to CSV file
$result | Export-Csv -Path 'X:\Somewhere\VMNetwork.csv' -NoTypeInformation

P.S. using the -like comparison operator without wildcard characters results in the same comparison as used with -eq