VMware Cloud Community
jeep2004
Enthusiast
Enthusiast

how to Combine Custom attributes with "Select -ExpandProperty"

hi 

I need get-vm with two  parameters Custom attributes and "Select -ExpandProperty"  (notes)

the report need like this 

VMname, Custom attributes1 , Custom attributes2 ,notes

THX 

 

0 Kudos
39 Replies
jeep2004
Enthusiast
Enthusiast

You mean "get-event"

this command take time (its not 20 min, its 20 days and more like 400 days )

Therefore there is the value in the attributes "Powered Off For"  and I have the information that I need 


Just need to do exclude om the Excel  

 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Yes, I meant the Get-VIEvent cmdlet.
I also asked earlier if that value was already present in the Custom Attribute, to which you didn't reply.

My last attempt to answer.
So provided the value is already in the Custom Attribute, you can change the Where-clause to

Get-VM | Where-Object { $_.PowerState -eq "PoweredOff" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value -gt 20 }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jeep2004
Enthusiast
Enthusiast

thank you,

its working ok  ,but still some of the "days" show me the number like "4".

any way, 

I run the this line command  to see the count of VMs I have

#######################

$PoweredOFFVM = @(get-VM |where {$_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019"}).count|

ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
'Powered Off At' = ''
Notes = $_.Notes
'Powered Off By' = ''
'Powered Off For' = ''
"Folder name" = $_.Folder.Name
'Last Backup' = ''
# 'VM Count' = $_.ExtensionData.Vm.Count
vCenter = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
}

#################################

and its not working good, what I need to do If I want every cluster that I run this command to calculate the a Count ?

thanks

 

 

0 Kudos
LucD
Leadership
Leadership

Can you perhaps show what you mean by "but still some of the "days" show me the number like "4"."?
Does it change when you change the Where-clause to

Get-VM | Where-Object { $_.PowerState -eq "PoweredOff" -and ([int](Get-Annotation -Entity $_ -Name 'Powered Off For').Value) -gt 20 }


Where do you want to see that count in the output?
This line, especially the pipe symbol at the end doesn't make any sense.

$PoweredOFFVM = @(get-VM |where {$_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019"}).count|

If you want to see that count in every record, you could do

$PoweredOFFVM = Get-VM | where { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" }

$PoweredOFFVM |  ForEach-Object -Process {
    $obj = New-Object -TypeName PSObject -Property (
        [ordered]@{
            VM                = $_.Name
            'Powered Off At'  = ''
            Notes             = $_.Notes
            'Powered Off By'  = ''
            'Powered Off For' = ''
            "Folder name"     = $_.Folder.Name
            'Last Backup'     = ''
            'VM Count'        = $PoweredOFFVM.Count
            vCenter           = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
        }
    )
    Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
        ForEach-Object -Process {
            $obj."$($_.Name)" = $_.Value
        }
        $obj
    }

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

If I replace the line where to Where-clause - I get error 

the count  I need to put on my email as a Variable (I know how to put on email)

In the end, I take the variable count and put it in the email

how do I perform the count on  each cluster ?

 

thank you

 

 

0 Kudos
LucD
Leadership
Leadership

Can you show the code for which you get the error?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

yes...

this is the code

######################################

$excelfile = "D:\Scheduler\PoweredOffVMs\Report_PoweredOffVMs.csv"
$FileHtml = "D:\Scheduler\PoweredOffVMs\PoweredOff_ver3.htm"
$caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'

$PoweredOFFVM = Get-VM | Where-clause { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value -gt 30 }
$PoweredOFFVM | ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
'Powered Off At' = ''
Notes = $_.Notes
'Powered Off By' = ''
'Powered Off For' = ''
"Folder name" = $_.Folder.Name
'Last Backup' = ''
'VM Count' = $PoweredOFFVM.Count
vCenter = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
}|
Export-Csv $excelfile -NoTypeInformation -UseCulture -Encoding UTF8

####################

and this is the error 

Where-clause : The term 'Where-clause' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:5 char:26
+ $PoweredOFFVM = Get-VM | Where-clause { $_.PowerState -eq "PoweredOff ...
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Where-clause:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Tags (1)
0 Kudos
LucD
Leadership
Leadership

That cmdlet should be Where-Object

$PoweredOFFVM = Get-VM | Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value -gt 30 }

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

ok . this what I do 

and you told me to change Where-clause .

but still some of the vm is like 7 days, 

and also , how can I do the count for each cluster with variable ?

 

THX 

0 Kudos
LucD
Leadership
Leadership

The cmdlet is Where-Object, the part within the curly braces is named the Where-clause.

Can you show me an example of that "is like 7 days"?
The Custom Attribute value and what is reported


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

HI 

this is my Report from Excel

I have a 120 VMs.... ( this is example )

Capture.PNG

0 Kudos
LucD
Leadership
Leadership

Now I understand, the value in those Custom Attributes contains the text Days.
That explains why the condition doesn't work asis.
You could have told that, it would have saved a lot of time

We can extract the actual number and use that.
Something like this

$PoweredOFFVM = Get-VM | Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value.Split(' ')[0] -gt 30 }


To get the count of VMs, just use $PoweredOFFVM.Count
 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

HI

days...Unfortunately still not working

and the $PoweredOFFVM.Count give me  0 

thank you

0 Kudos
LucD
Leadership
Leadership

Can you post the actual script you are using?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast


and each Vcenter like.....

 

Vcenter1 - is 15 VM

Vcenter2 - is 35 VM....

and so 

 

this is all My script after I connect to Vcenter

######################

$excelfile = "D:\Scheduler\PoweredOffVMs\Report_PoweredOffVMs.csv"
$FileHtml = "D:\Scheduler\PoweredOffVMs\PoweredOff_ver3.htm"
$caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'


$PoweredOFFVM = Get-VM| Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value.Split(' ')[0] -gt 30 }|

ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
'Powered Off At' = ''
Notes = $_.Notes
'Powered Off By' = ''
'Powered Off For' = ''
"Folder name" = $_.Folder.Name
'Last Backup' = ''
# 'VM Count' = $_.ExtensionData.Vm.Count
vCenter = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
} |

Export-Csv $excelfile -NoTypeInformation -UseCulture -Encoding UTF8

$smtp = 'aaaa.bbb.com'
$from = "PoweredOffVMs@kuku.com"
$SendEmailTo = 'Mysalfe@kuku.com'
$Subject = "Powered Off VMs - entire Vcenter - $CountVMsPoweredOFF.count "
$bodyasHtml = "The Total VMs is $CountVMsPoweredOFF.count This report runs on the entire Vcenter environment"
send-mailmessage -from $from -to $SendEmailTo -subject $PoweredOFFVM.Count -BodyAsHtml $PoweredOFFVM.Count -Attachments $excelfile -priority Normal -dno onSuccess, onFailure -smtpServer $smtp -Encoding UTF8

###########################

 

THX 

0 Kudos
LucD
Leadership
Leadership

That is not the code I provided earlier, you still have that pipe symbol at the end of the line

$PoweredOFFVM = Get-VM| Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" -and (Get-Annotation -Entity $_ -Name 'Powered Off For').Value.Split(' ')[0] -gt 30 }|

Try with the code I provided.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

if I run witout symbol 

I get error 

------------------

Get-Annotation : Cannot validate argument on parameter 'Entity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:22 char:32
+ Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAct ...
+ ~~
+ CategoryInfo : InvalidData: (:) [Get-Annotation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetAnnotation

0 Kudos
LucD
Leadership
Leadership

Again, because you didn't run the code that I posted.
Your script

ForEach-Object -Process {

My code

$PoweredOFFVM = Get-VM | where { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019" }

$PoweredOFFVM |  ForEach-Object -Process {

This is becoming a bit tedious.

You ask for help, which I gave, but you ignore what I post.

Also for future threads, you might open, when asking for help try to provide as much information as possible.
When you had mentioned in the beginning that the Custom Attribute contained not just a number but also the text "Days", we could have avoided a lot of back-and-forth in this thread.




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

You're right, it's not on purpose
New in the field, trying to learn

 

Thank you

0 Kudos
jeep2004
Enthusiast
Enthusiast

I try to study alone ...

need a count on each Vcenter

Let me get back to you with the hope that I have you

Thanks again

0 Kudos