Create custom attributes with PowerCLI

As I mentioned one of my previous articles, I would like to write a PowerCLI script, which creates a fully automated inventory on my vCenter Server environment. Most of the information that I need stored on the vCenter Server database, but I need some additional data: owner, ordering ticket number, etc. Where can I store them? In the SAP – as the bookkeeper says. In an Excel file – as the manager says. No way! My recommendation is the vCenter Server database, easily accessible by my PowerCLI script.

You can easily add Custom Attributes in the vSphere Client. Click on Administration menu then choose Custom Attributes.

Here you can add multiple values.

I automatize adding Custom Attributes to the vCenter Server via PowerCLI, because I need almost the same inventory for all customer environments. Here is my script, which adds new attributes.

# set customer specific Custom Attributes
cls
$cas = Get-CustomAttribute
$caNumber = $cas.Count

$needHostID = $true
$needSAPnumber = $true
$needOwner = $true

$i = 0
for ($i=0; $i -lt $caNumber; $i++)
{
switch ($cas[$i].Name.tolower())
{
"hostid" {$needHostID = $false}
"sapnumber" {$needSAPnumber = $false}
"owner" {$needOwner = $false}
}
}
if ($needHostID)
{
New-CustomAttribute -Name hostID -Confirm:$False
Write-host "   hostID created"
}
Else {Write-host "   hostID exists"}

if ($needSAPnumber)
{
New-CustomAttribute -Name SAPnumber -Confirm:$False
Write-host "   SAPnumber created"
}
Else {Write-host "   SAPnumber exists"}

if ($needOwner)
{
New-CustomAttribute -Name owner -Confirm:$False
Write-host "   owner created"
}
Else {Write-host "   owner exists"}</pre>
<div>

When the script finish, you should see this.

That's all!