Solved

powershell script

Posted on 2014-10-29
11
119 Views
Last Modified: 2014-11-10
HI Guys,

I know this is something simple, but i cant seem to figure it out! Please review my code below:

get-vm *| % {

$vminfo={} | select name, network
$vminfo.name = $_.name
$vminfo.network = (Get-VM -Name $vm).guest.nics.networkname
} | export-csv vmnetwork3.csv

thank you in advance!
0
Comment
Question by:Kay
11 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 40411322
Try this

get-vm * | foreach {
$name = $_.name
$vmnetwork = (get-vm $name).guest.nics.networkname
"$name, $vmnetwork" >> C:\network.txt
}
0
 
LVL 40

Expert Comment

by:footech
ID: 40411464
You set the properties for the variable, but you never output it, so nothing is sent to Export-CSV.  The following should work.
get-vm *| % {

 $vminfo={} | select name, network
 $vminfo.name = $_.name
 $vminfo.network = (Get-VM -Name $vm).guest.nics.networkname
 $vminfo
 } | export-csv vmnetwork3.csv

Open in new window

0
 

Author Comment

by:Kay
ID: 40412897
problem is sometimes there is more than one network, on (Get-VM -Name $vm).guest.nics.networkname, and therefore the output is system.object[]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kay
ID: 40412908
footech the script doesn't work, it gives me the same vminfo.net for every vm's
0
 

Author Comment

by:Kay
ID: 40412917
i have this script:

(get-vm) | %{
  $vm = $_
  echo $vm.name
  $vm.Guest.Nics | %{
    $vminfo = $_
    echo $vminfo.NetworkName
 
  }
}

however how do i out this info in a csv file?
0
 

Author Comment

by:Kay
ID: 40412944
thats the best way i've found so far, can anyone thing of a better way:


$Report=@()

(get-vm) | %{
$vm=$_
$vminfo={} | select Name, Network
$vminfo.name = $_.name
$vminfo.Guest.Nics | %{
$vminfo.network = (Get-VM -Name $vm).guest.nics.networkname
$Report += $vminfo

 }

}

after the script completes, the do a $report | output-csv vmnetwork.csv
0
 

Author Comment

by:Kay
ID: 40412976
$Report=@()

(get-vm) | %{
$vm=$_
$vminfo={} | select Name, Network
$vminfo.name = $_.name
$vminfo.Guest.Nics | %{
$vminfo.network = (Get-VM -Name $vm).guest.nics.networkname
$Report += $vminfo

 }

} | export-csv vmguest.csv

^ can i do something like this?
0
 

Author Comment

by:Kay
ID: 40413101
when i export-csv ,

for ones win more than one network card i get System.Object[]
System.Object[]
System.Object[]
System.Object[]
System.Object[]

on excel.

however i can see the out fine on the powershell window, please help. this is in relation to this code:

$Report=@()

(get-vm) | %{
$vm=$_
$vminfo={} | select Name, Network
$vminfo.name = $_.name
$vminfo.Guest.Nics | %{
$vminfo.network = (Get-VM -Name $vm).guest.nics.networkname
$Report += $vminfo

 }

}

$Report | export-csv vmguestnetwork.csv
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40413463
I don't see how $vminfo.Guest.Nics could be anything.  So unless I'm missing something, piping it to ForEach-Object wouldn't get you anywhere.
Export-CSV can only display the contents of properties that have a simple type like a string.  So if a property is an array it doesn't work.  The (or one) solution is to join all the elements of the array into a single string.  I can't test on my end, so you'll have to let me know how the output is.
get-vm | % {
    $vm = $_
    $vminfo={} | select name, network
    $vminfo.name = $_.name
    $vminfo.network = ((Get-VM -Name $vm).guest.nics.networkname) -join ";"
    $vminfo
} | Export-Csv vmnetwork3.csv

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40414669
That should work, but does too much. You do not need to run Get-VM twice, you already have the VM object at hand. And we should create the output object at once:
get-vm | % {
  New-Object PsObject -Property @{
    name    = $_.Name
    network = $_.guest.Nics.Networkname -join ';'
  }
} | Export-Csv vmnetwork3.csv

Open in new window

Of course the separator for the NICs is arbitrary, and semicolon just one typical choice besides comma and pipe.
0
 
LVL 40

Expert Comment

by:footech
ID: 40433917
Qlemo's correct.  No need to run the same command twice when you already have the info.  He took the initiative to clean up the code, and as such I would've split the points with him.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question