Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 124
  • Last Modified:

powershell script

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
Kay
Asked:
Kay
1 Solution
 
Joseph DalyCommented:
Try this

get-vm * | foreach {
$name = $_.name
$vmnetwork = (get-vm $name).guest.nics.networkname
"$name, $vmnetwork" >> C:\network.txt
}
0
 
footechCommented:
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
 
KayAuthor Commented:
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
KayAuthor Commented:
footech the script doesn't work, it gives me the same vminfo.net for every vm's
0
 
KayAuthor Commented:
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
 
KayAuthor Commented:
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
 
KayAuthor Commented:
$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
 
KayAuthor Commented:
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
 
footechCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
footechCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now