Solved

powershell script

Posted on 2014-10-29
11
120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 70

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

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!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

710 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