Solved

powershell script

Posted on 2014-10-29
11
116 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

21 Experts available now in Live!

Get 1:1 Help Now