Solved

powershell script

Posted on 2014-10-29
11
113 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 39

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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 39

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 68

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 39

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now