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
What do you already have, and where did you get stuck?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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
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
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
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
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 ?
Just do
Get-VM | where {$_.PowerState -eq "PoweredOff"}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 }
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
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
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
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
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
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
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
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
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
That is what the Where-clause I posted earlier is doing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference