VMware Cloud Community
victor120392_19
Contributor
Contributor

Invoke-VMscript (Network)

Hello, good day,

I am trying to run several commands through invoke-vmscript and return an Arraylist within my objects,

Is there a way to achieve this?

$Disks2 = @'

$collectionWithItems = New-Object System.Collections.ArrayList

$netroute1 = Get-NetRoute | Where-Object -FilterScript {$_.NextHop -Ne "::"} | Where-Object -FilterScript { $_.NextHop -Ne "0.0.0.0" } | Where-Object -FilterScript { ($_.NextHop.SubString(0,6) -Ne "fe80::") } | Get-NetAdapter 

$networkadapter = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE

$netroute2 = Get-NetRoute | Where-Object -FilterScript { $_.NextHop -Ne "::" } | Where-Object -FilterScript { $_.NextHop -Ne "0.0.0.0" } | Where-Object -FilterScript { ($_.NextHop.SubString(0,6) -Ne "fe80::") }

$dnsclient = Get-DnsClientServerAddress

$netipinterface = Get-NetIPInterface

 

  $temp2 = New-Object System.Object

   $temp2 | Add-Member -MemberType NoteProperty -Name netroute1 -Value $netroute1

$temp2 | Add-Member -MemberType NoteProperty -Name networkadapter -Value $networkadapter

$temp2 | Add-Member -MemberType NoteProperty -Name netroute2 -Value $netroute2

$temp2 | Add-Member -MemberType NoteProperty -Name dnsclient -Value $dnsclient

$temp2 | Add-Member -MemberType NoteProperty -Name netipinterface -Value $netipinterface

   $collectionWithItems.Add($temp2) | Out-Null

  $collectionWithItems  | ConvertTo-CSV -NoTypeInformation

'@

$Result2 = Invoke-VMScript -VM $server  -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptText $disks2   -ScriptType Powershell  -ErrorAction stop  | Select -ExpandProperty ScriptOutput  | ConvertFrom-CSV

I have tried with and without Converto-CSV | ConvertFrom-CSV without success.

I would like to work with the objects after the invoke method is run, as example:

$collectionwithitems.netipinterface should return info but $result2.netipinterface is not,

Is there any way to implement this? I have been working a couple days on this Smiley Sad

Thanks!

0 Kudos
3 Replies
LucD
Leadership
Leadership

The issue is that you are assigning objects to columns in a CSV.

This has nothing to with Invoke-VMScript.

If you run the script you have in $Disks1 locally, export it to a CSV, you will also have an issue.

If you import the CSV for example, you would see something like this

csv.png

The solution is to assign singular values to the properties.

For example:

$Disks2 = @'

$collectionWithItems = New-Object System.Collections.ArrayList

$netroute1 = Get-NetRoute |

    Where-Object -FilterScript {$_.NextHop -Ne "::"} |

    Where-Object -FilterScript { $_.NextHop -Ne "0.0.0.0" } |

    Where-Object -FilterScript { ($_.NextHop.SubString(0,6) -Ne "fe80::") } |

    Get-NetAdapter

$networkadapter = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE

$netroute2 = Get-NetRoute | Where-Object -FilterScript { $_.NextHop -Ne "::" } | Where-Object -FilterScript { $_.NextHop -Ne "0.0.0.0" } | Where-Object -FilterScript { ($_.NextHop.SubString(0,6) -Ne "fe80::") }

$dnsclient = Get-DnsClientServerAddress

$netipinterface = Get-NetIPInterface

$temp2 = New-Object System.Object

$temp2 | Add-Member -MemberType NoteProperty -Name netroute1 -Value ($netroute1.Name -join '|')

$temp2 | Add-Member -MemberType NoteProperty -Name networkadapter -Value ($networkadapter.Description -join '|')

$temp2 | Add-Member -MemberType NoteProperty -Name netroute2 -Value ($netroute2.ifIndex -join '|')

$temp2 | Add-Member -MemberType NoteProperty -Name dnsclient -Value ($dnsclient.InterfaceAlias -join '|')

$temp2 | Add-Member -MemberType NoteProperty -Name netipinterface -Value ($netipinterface.InterfaceAlias -join '|')

$collectionWithItems.Add($temp2) | Out-Null

$collectionWithItems  | ConvertTo-CSV -NoTypeInformation

'@


$Result = Invoke-VMScript -VM $server  -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptText $disks2   -ScriptType Powershell  -ErrorAction stop

$Result | Select -ExpandProperty ScriptOutput  |

ConvertFrom-CSV

Note that this is just an example to show the concept.
If you want to have more properties of each of the objects in your CSV, you need to add additional Add-Member statements to your $Disk2 code.
Just remember, each column in the CSV format needs to a singular value (not an array or another object)


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

0 Kudos
victor120392_19
Contributor
Contributor

Hi LucD, thank you for your fast reply,

However, I ran this and it sent me the same,

What I would like to do is, after the script is run, work with the results, for example $Result.netroute1,

is this possible?,

Thank you,

Regards!

0 Kudos
LucD
Leadership
Leadership

I would be very much amazed if that send you the same.

You were assigning objects to columns, while my version is assigning single values to columns.


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

0 Kudos