Hey all, sorry to bother i try to google my way through most things. The problem i'm having is the below script when ran doesnt seem to do anything, i hit enter and it just goes to a new line. I've run the command thats in the foreach loop singularly and it works and displays the correct output. Any help would be much appreciated.
$vmlist = Import-CSV C:\scripts\VMOwners\VMNames.csv
foreach ($vm in $vmlist){
Get-VM $vm | Select Name,
@{N='Category';E={ ($_ | Get-Annotation -CustomAttribute 'Business Owner').Value}} | Export-CSV -Append C:\scripts\VMOwners\BusinessOwners.csv
}
If you are using the column name, you have to specify it.
Get-VM -Name $vmlist.Name |
Select Name,
@{N = 'Category'; E = { ($_ | Get-Annotation -CustomAttribute 'Business Owner').Value}} |
Export-CSV -Append C:\scripts\VMOwners\BusinessOwners.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are overwriting the CSV for each VM since you placed the Export-Csv inside the ForEach loop.
A better way would be to use the pipeline, something like this
Get-VM -Name $vmlist |
Select Name,
@{N = 'Category'; E = { ($_ | Get-Annotation -CustomAttribute 'Business Owner').Value}} |
Export-CSV -Append C:\scripts\VMOwners\BusinessOwners.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Wow LucD you've helped me unwillining so many times lol, thank you for this and all the others!
I am having an issue though, when running i'm getting the below error, for all VMs.
PowerCLI C:\scripts> .\getBusinessOwner.ps1
Get-VM : 1/25/2019 10:12:03 AM Get-VM VM with name '@{156stagingweb02=156testweb02}' was not found using the specified filter(s).
At C:\scripts\getBusinessOwner.ps1:2 char:1
+ Get-VM -Name $vmlist |
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
At first i thought the CSV wasn't importing correctly so manually did that, excuse the variable name i'm frustrated, but that seems alright. Any ideas?
PowerCLI C:\scripts> $shit = Import-CSV C:\scripts\VMOwners\VMNames.csv
PowerCLI C:\scripts> echo $shit
Name
----
156stagingweb02
156testweb02
BMCB2K8LR2TEST
If you are using the column name, you have to specify it.
Get-VM -Name $vmlist.Name |
Select Name,
@{N = 'Category'; E = { ($_ | Get-Annotation -CustomAttribute 'Business Owner').Value}} |
Export-CSV -Append C:\scripts\VMOwners\BusinessOwners.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I can't thank you enough LucD, thank you!