VMware Cloud Community
guesmi1977
Enthusiast
Enthusiast
Jump to solution

need to export the linux network name (like eth0, eth1, ens ...) into power CLI variable

hello,
I added a new network interface vnic from vcenter for a linux VM.

I need to get the new network name into power cli variable to use it later

I am using vminvoke-script and linux script (based in nmcli command) to show all network card 
but I am not able to get the netwok name into powercli variable
any idea please .
here is the script and the output.

 

 

$LinuxScript= @'
nmcli dev status

'@

$output1=Invoke-VMScript -VM $VmName -ScriptText $LinuxScript -GuestCredential $LinuxCredential -ScriptType Bash -Verbose

 

PS C:\WINDOWS\system32> $output1=Invoke-VMScript -VM $VmName -ScriptText $LinuxScript -GuestCredential $LinuxCredential -ScriptType Bash -Verbose
VERBOSE: Performing the operation "Invoke-VMScript" on target "centos_vm".
VERBOSE: 11/12/2023 7:46:25 PM Invoke-VMScript Finished execution

PS C:\WINDOWS\system32> $output1

ScriptOutput
-------------------------------------------------------------------------------
----------------------------------------| DEVICE TYPE STATE
CONNECTION
| virbr0 bridge connected virbr0
| ens192 ethernet connecting (getting IP configuration) ens192
| ens224 ethernet disconnected --
| lo loopback unmanaged --
| virbr0-nic tun unmanaged --
|
-------------------------------------------------------------------------------
----------------------------------------

 

PS C:\WINDOWS\system32>

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This is not really a PowerCLI question but a bash question.
In any case, this seems to work for me

$vmName = 'MyVM'
$user = 'user'
$pswd = 'VMware1!'

$cred = New-Object -TypeName PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)
$code = @'
echo 'name,MAC'
ip -o link | awk '$2 != "lo:" {print $2 "," $(NF-2)}'
'@

$result = Invoke-VMScript -VM $vmName -ScriptText $code -GuestCredential $cred -ScriptType Bash
$net = $result.ScriptOutput | ConvertFrom-Csv

Get-NetworkAdapter -VM $vmName -PipelineVariable vnic|
Select @{N='VM';E={$vmName}},
  Name,NetworkName,MacAddress,
  @{N='Check';E={$vnic.Type}},
  @{N='GuestLink';E={$net | where{$_.MAC -eq $vnic.MacAddress} | Select -ExpandProperty name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
7 Replies
guesmi1977
Enthusiast
Enthusiast
Jump to solution

My first idea is to put the $output1 ( before adding the vnic) into CSV file 

and the $output2 (after adding the vnic) into anoter CSV file
and after that compare the two files to extract the name off the added vnic

but my problem : when i do $output1 |export-csv  the file doesn't contain the same as $output1 viariable

why ?

and if you have another idea to do this please help me

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You didn't say which Linux distribution you are using, but doesn't the ip command return that info?

ip link show


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
guesmi1977
Enthusiast
Enthusiast
Jump to solution

it a centos 8 linux 

need to get the correspondance of vsphere name and the OS name off the added vnic  

guesmi1977_0-1699819897982.png

 

PS C:\WINDOWS\system32> Get-VM -Name centos_vm | Get-NetworkAdapter

Name Type NetworkName MacAddress WakeOnLan
Enabled
---- ---- ----------- ---------- ---------
Network adapter 1 Vmxnet3 DPortGroup-01 00:50:56:ab:f4:5e True
Network adapter 2 Vmxnet3 DPortGroup-01 00:50:56:ab:50:f8 False

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You need to use the MAC address to link the ens* names to the vNIC names.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
guesmi1977
Enthusiast
Enthusiast
Jump to solution

Visualy I can identify the added vnic by MAC address or by comparing an old nmcli output with a new one 

but my problem how to do this by powercli and pass this vnic into variable

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is not really a PowerCLI question but a bash question.
In any case, this seems to work for me

$vmName = 'MyVM'
$user = 'user'
$pswd = 'VMware1!'

$cred = New-Object -TypeName PSCredential -ArgumentList $user,(ConvertTo-SecureString -String $pswd -AsPlainText -Force)
$code = @'
echo 'name,MAC'
ip -o link | awk '$2 != "lo:" {print $2 "," $(NF-2)}'
'@

$result = Invoke-VMScript -VM $vmName -ScriptText $code -GuestCredential $cred -ScriptType Bash
$net = $result.ScriptOutput | ConvertFrom-Csv

Get-NetworkAdapter -VM $vmName -PipelineVariable vnic|
Select @{N='VM';E={$vmName}},
  Name,NetworkName,MacAddress,
  @{N='Check';E={$vnic.Type}},
  @{N='GuestLink';E={$net | where{$_.MAC -eq $vnic.MacAddress} | Select -ExpandProperty name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
guesmi1977
Enthusiast
Enthusiast
Jump to solution

Thank you very much LucD

it helps me a lot

Reply
0 Kudos