hopefully a quick and easy one here...
i have the following script that checks a particular vlan ID and then shows me the VMs that are using that ID
foreach($vm in (get-vm)){
if (Get-NetworkAdapter -vm $vm.name |?{$_.NetworkName -eq "vlan13_VM"}){$vm.name} | export-csv -path c:\scripts\vlans.csv -NoTypeInformation
}
i have added the export-csv command at the end as id like the details to be put into a csv which i can then email (will try to script the email part after)
i cant get the export-csv option to create the csv file as it comes back with an error saying
An empty pipe element is not allowed at c:\scripts\checkvlan.ps1:2 char84
char84 is where the pipe starts before the export-csv
can anybody advise me on what ive done wrong?
thanks
Get-VM | where { (Get-NetworkAdapter -vm $_.name).NetworkName -eq "vlan13_VM" } | select Name | export-csv -path c:\scripts\vlans.csv -NoTypeInformation
Or if you want to keep the foreach
structure, either
...use Out-File
instead of Export-CSV
foreach ( $vm in (Get-VM) ) { if ( (Get-NetworkAdapter -vm $vm.name).NetworkName -eq "vlan13_VM" ) {$vm.name | out-file c:\scripts\vlans.csv -Append} }
...or collect the list in a hashtable and convert to objects as discussed here
$out = @()
foreach ( $vm in (Get-VM) ) { if ( (Get-NetworkAdapter -vm $vm.name).NetworkName -eq "vlan13_VM" ) {$out += $vm.name} }
$out | Select-Object @{Name='Name';Expression={$_}} | Export-CSV -path c:\scripts\vlans.csv -NoTypeInformation