Hi everyone,
I am new to Powershell and I am trying to learn it and understand the concept. Recently I joined a company as an IT engineer and my job is to automate VMWare.
I edited my first script with powercli and now I'm stuck. I would like some suggestions.
Let me say the situation:
There are many clusters in the VMWare client with a particular name, and each cluster has many VMS. We want two scripts, one to create a custom attribute and the second to give them a value according to their cluster. If the cluster has an (owner-it) name, then the VMS that are inside the cluster should have IT as the value. For all VMS, the user-defined attribute already has "Unit" assigned to it. The value is specified according to the name of the unit or cluster.
Cluster's name : in Cluster VMs has names like: custome attribute: Value:
owner-IT test-owner Unit IT
trainign-xy Test-IT Unit xy
Job consulting-Job Unit job
Console-cons Program-cons Unit cons
what we want is that a PowerShell-script should give VM Value but the value should be the same as the cluster's name like (if a cluster has owner name so inside cluster the VM should have (test-owner) name.
A Sample:
loop $vm in get-vm | Get-Folder -Server $server -Name hasx
switch ($vm | get-datacenter)
devat-tsi $unit = "TSI"
devat-uk $unit = "UK"
$vm | Set-Annotation -CustomAttribute "Unit" -Value $unit
2nd Script should just create custom attribute for specific Cluster (owener-IT)
if it is not explained give a comment.
I would appreciate any suggestions.
Thank You
The following loops over all clusters in your environment.
And uses the suffix of the clustername for the Custom Value.
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-Annotation -Entity $vm -CustomAttribute 'Unit' | Set-Annotation -Value ($cluster.Name.Split('-')[1])
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not exactly sure I understand what you are trying to do.
I understand something like the following, but there are some open questions.
- loop over all clusters
- find VMs in cluster. Name of VMs is <text><clusterName>?
- create a Custom Attribute for the VM.
- The name of the Custom Attribute is <clusterName>?
- What value shall this Custom Attribute have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
you are right name of vm is <text>-tsi
First, we give each vm a value because each vm has a custome attribute (unit) and they should be sorted out by unit.
But they don't have a value yet, so we want to give them a value, and the value should be the last letter of the cluster (Management-tsi) for each vm, and in the owner cluster as owner and in the IT cluster as value IT.
Example:
ForEach($Line in $VMList)
{
# Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "Unit" -Value "ES"
#Get-Vm $Line.VMName | Set-Annotation -CustomAttribute " UNIT" -Value IT
Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "Unit" -Value "TSI"
}
You mean something like this?
ForEach-Object -Process {
$vm = Get-VM -Name $_.VMName
$cluster = Get-Cluster -VM $vm
Get-Annotation -Entity $vm -CustomAttribute 'Unit' | Set-Annotation -Value $cluster.Name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yeah similar to this, but we do not want to use CSV we want to search the cluster directly from the Vcenter and according to the cluster the last character we give VMS Value. Because we want to do some automation course in Vcenter.
they are the cluster names:
PS C:\Users\Administrator> $clusters = Get-Cluster | Get-View
foreach($cluster in $clusters){
$cluster.name
$cluster.summary.numhosts
}
text-mgmt
2
text-uk
4
text-base
9
text-es
2
text-mech
text-tsi
Not sure what this NumHost property does here.
I thought you wanted to set a Custom Attribute?
In any case, without a CSV, you could do
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-Annotation -Entity $vm -CustomAttribute 'Unit' | Set-Annotation -Value $cluster.Name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
in next script, I want to set a custom attribute but only for one cluster not for all. I mean, I want to set only for (text-tsi) cluster custom attribute.
You lost me completely now I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you it is working . i always follow your blog they are really helpful.
Thanks.
Not sure what I replied to if I'm honest. :smileygrin:
Is this thread now answered or is there anything still missing?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I just have to adjust your code and it has answered my main question. before I consider it as correct answer I would like to post my code here then
Sure
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
I have tested your given example and it works, but this way, under the example code, it would be more practical to change the value of the attribute each time. We do not want to give the attribute the full name of the cluster (devat-tsi) as a value in the future.
I tested your given example and it is working but like this, below example code. it would be more practice to change the value of attribute every time. we do not want to give the attribute complete name of Cluster (devat-tsi) as value in future.
could you please help me in this below example. actually I used get-folder for a test but this script should be applied on all cluster.
loop $vm in get-vm | Get-Folder -Name test
switch ($vm | get-datacenter)
devat-tsi $unit = "TSI"
devat-uk $unit = "UK"
$vm | Set-Annotation -CustomAttribute "Unit" -Value $unit
The following loops over all clusters in your environment.
And uses the suffix of the clustername for the Custom Value.
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
Get-Annotation -Entity $vm -CustomAttribute 'Unit' | Set-Annotation -Value ($cluster.Name.Split('-')[1])
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you, sir. That's what I wanted. Is there any way, if I give the attribute a different value in a particular cluster. like the VMs in "test-tsi" cluster should get as value "MA".
You could use a hashtable as a lookup table.
You can have multiple entries in there.
'tsi' = 'MA'
}
Get-Cluster -PipelineVariable cluster |
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
$suffix = $cluster.Name.Split('-')[1]
if($lookupTable.ContainsKey($suffix)){
$suffix = $lookupTable[$suffix]
}
Get-Annotation -Entity $vm -CustomAttribute 'Unit' | Set-Annotation -Value $suffix
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It is well practical, thank you Sir,