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
LucD
Leadership
Leadership

What do you already have, and where did you get stuck?


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

this what I have - I take from this site 

I need to get view on notes, and Vcenter ,, and more ...just need to get start

#$vmlist = Import-Csv "C:\Users\myvms.csv"
$caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'
$vmlist = get-vm -name "vmname" 
Get-VM -Name $vmlist.Name |
ForEach-Object -Process {
$obj = New-object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
'Powered Off By' = ''
'Powered Off For' = ''
'Last Backup' = ''
'Powered Off At' = ''
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
} |
Export-Csv c:\Temp.testcsv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

Just add those additional properties to $obj

$caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'

$vmlist = Import-Csv "C:\Users\myvms.csv"

Get-VM -Name $vmlist.Name |
    ForEach-Object -Process {
        $obj = New-Object -TypeName PSObject -Property (
            [ordered]@{
                VM                = $_.Name
                Notes             = $_.Notes
                vCenter           = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
                'Powered Off By'  = ''
                'Powered Off For' = ''
                'Last Backup'     = ''
                'Powered Off At'  = ''
            }
        )
        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

ok , its work fine -THX 

now ..

In need add more info all my VMs (not vm list) Vcenter, folder name , disk used (on vm) 

to get something like 

foreach ($vmlist in $VMName) 

and some of the Custom attributes if the value than 30 (number) I want in my report 

if it is below 30 that will not appear in the report.

thanks

 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

To handle all your VMs, remove the Import-CSV line.
Then replace the line

Get-VM -Name $vmlist.Name

with

Get-VM


To add the extra properties just use the ones available in the object returned by Get-VM.

The requirement "... some of the Custom attributes" is rather vague and hard to code I'm afraid

 


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

ok , Thanks 
let me try to do the value if then 30 show in report....if not work I wilך back to you...


1. by the way . it any way to do it faster ... I am run now this command  to One cluster and its take time 

 

 

0 Kudos
jeep2004
Enthusiast
Enthusiast

I am stuck - need help please 😀

1 . need to put this command | where {$_.PowerState -eq "PoweredOff"}  but where I put on script ?  

2.how can I do it with  parallel to get faster  ?

 

 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Just do

Get-VM | where {$_.PowerState -eq "PoweredOff"} 


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

ok
few things

how can I do with loop in my script with "where" parameters ?

like 

$notlike = "vm-*"  , "WIN2019*" , "2k19*

Get-VM | where {$_.PowerState -eq "PoweredOff" -and $_.name -notlike "w2K19*"  -and $_.name -notlike "WIN2019*"}|

so on my script need to write something like this 

Get-VM | where {$_.PowerState -eq "PoweredOff" -and $_.name -notlike $notlike }

Tags (1)
0 Kudos
LucD
Leadership
Leadership

For more complex matches (like the name of a VM with multiple options), I prefer to use a RegEx.

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

This uses some RegEx features:
- the '|' symbol is used as an 'or'
- the '^' is used to indicate the beginning of the string
 


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

Maybe I haven't explained well

I need the line get-vm |where {$_.PowerState -eq "PoweredOff" -and $_.name -notlike "w2K19*"  -and $_.name -notlike "WIN2019*" } 

with $notlike  

like this 

$notlike  = "w2K19*" ,"WIN2019*"

and the line need to be like this 

Get-VM | where {$_.PowerState -eq "PoweredOff" -and $_.name -notlike $notlike }

and please one more things 

'how can I do on the 'Powered Off For' value like  if the number the under 20 not show in report 

 

thanks you 

0 Kudos
LucD
Leadership
Leadership

Did you actually try the -notmatch?
That is exactly doing what you want to achieve with your -notlike


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

Its works fine , THX  but I want with $...
never mind


how can you help me with my second question 
'how can I  'Powered Off For' value like  if the number  20 or below not show in report

Thanks

 

0 Kudos
LucD
Leadership
Leadership

You can use the Get-VIEvent cmdlet and look for the presence or absence of a VMPoweredOffEvent during the last 20 minutes.
Something like this for example

Get-VM | where{
(Get-VIEvent -Entity $vm -Start (Get-Date).AddMinutes(-20) -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VMPoweredOffEvent]}) -eq $null}


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

0 Kudos
LucD
Leadership
Leadership

If you want to the $notLike variable, you can still do the RegEx (which is way superior to -notlike imho).

$notlike = "vm-"  , "WIN2019" , "2k19"
Get-VM | 
Where-Object { $_.PowerState -eq "PoweredOff" -and $_.name -notmatch (($notLike | ForEach-Object { "^$($_)" }) -join '|')) } |

You can even automate the removal of the asterisk if you want to keep that




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

0 Kudos
jeep2004
Enthusiast
Enthusiast

About value 

the report need on the value in Get-Annotation / line 12

$excelfile = "D:\scripts\PowerOffVMs\VMAnnotations_poweredOFF.csv"
$caNames = 'Last Backup', 'Powered Off By', 'Powered Off For' , 'Powered Off At'
Get-vm| where {$_.PowerState -eq "PoweredOff" -and $_.name -notmatch "^w2K19|^WIN2019"}|
ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property (
[ordered]@{
VM = $_.Name
Notes = $_.Notes
vCenter = ([uri]$_.ExtensionData.Client.ServiceUrl).Host
"Folder name" = $_.Folder.Name
'Powered Off By' = ''
'Powered Off For' = ''  
'Last Backup' = ''
'Powered Off At' = ''
}
)
Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
ForEach-Object -Process {
$obj."$($_.Name)" = $_.Value
}
$obj
} |


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

0 Kudos
LucD
Leadership
Leadership

Not sure what the question is.
Do you want the value in the Custom Attribute 'Powered Off For' in line 12?
Or do you want to calculate that value and set the CA?


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

0 Kudos
jeep2004
Enthusiast
Enthusiast

hi

sorry for my late

After I runs the script . I get the info that I need 

but , I need to show only the report  VMs that over then 20 Days  ( on the line Powered Off For) .I don't have to see all the rest - it is irrelevant

thanks

0 Kudos
LucD
Leadership
Leadership

That is what the Where-clause I posted earlier is doing.


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

0 Kudos