Getting UCS server details using PowerShell

There are a few things that Cisco is really good at, and one of those things is providing an excellent API for their UCS servers.

Here's a quick guide to getting basic details from a server in UCS, using the UCS Central PowerTools. I'll have more posts in future that explain how to take this information and use it for various different functions.

Yes, you could read the manual for this, but who wants to read a manual? Besides, documentation is one of the things that Cisco is NOT really good at :)

You always start by connecting to your UCS Central server (note, that if you don't use UCS Central, most of these commands are very similar, but you would substitute the UCS Central cmdlet for the equivalent UCS Manager cmdlet).

$creds = "replace with your method of getting a credential object"
$CentralServer = "servername"
$ucsCentral = Connect-UcsCentral -Name $CentralServer -Credential $Creds

For the credential object, you can prompt for a user to enter credentials using the get-credential cmdlet (if your script is going to be interactive). More commonly, though, you will need to pre-create your credential object. There are many ways to do this, but I have a simple method to do this in my "Creating a Credential Object From Plain Text" post.
Obviously, in a production environment, you will want to do this in a more secure way than plain text - most likely using a certificate, I'll have posts on this later.

This will fill the $ucsCentral variable with some basic info about the instance you're connected to:

Cookie                      : 1478794642/b0a8e319-a7fe-40fa-9229-584c1927d273
Domains                     : 
LastUpdateTime              : 09/10/2016 11:17:22 AM
Name                        : yourservername
NoSsl                       : False
NumPendingConfigs           : 0
NumWatchers                 : 0
Port                        : 443
Priv                        : {read-only}
Proxy                       : 
RefreshPeriod               : 600
SessionId                   : web_38840
TransactionInProgress       : False
PromptOnCompleteTransaction : False
UcsCentral                  : yourservername
Uri                         : https://yourservername
UserName                    : ucs-domain\account
VirtualIpv4Address          :
Version                     : 1.4(1b)
WatchThreadStatus           : None

Notice that the username has a prefix of "ucs-" on the domain. If you are using Active Directory credentials, your domain name needs to be prefixed with ucs-.

Once you're connected, it's incredibly simple to get server details:

$servers = get-ucscentralserver -ucscentral $ucsCentral

The above code will return an array of powershell objects, containing details about every server found in your UCS Central instance, and place it into the $servers variable.

You can then work through this object using regular powershell commands to get the details you need.

I prefer to find the server I'm looking for by its serial number, since it's usually the most unique and permanent identifier. Here's an example:

$myServer = $servers | where {$_.serial -eq "mySerialNumber"}

Obviously replace "mySerialNumber" with the serial you're looking for, and you will receive something like this back:

AdminPower        : policy
AdminState        : in-service
AssignedToDn      : org-root/specifictoyoursetup/org-something/specifictoyoursetup
Association       : associated
Availability      : unavailable
AvailableMemory   : 786432
CheckPoint        : discovered
ConnPath          : {A, B}
ConnStatus        : {A, B}
Descr             : 
Discovery         : complete
Id                : 3
Lc                : discovered
LcTs              : 1969-12-31T19:00:00.000
LocalId           : sys/rack-unit-3
LowVoltageMemory  : regular-voltage
LsDn              : org-root/specifictoyoursetup/org-something/specifictoyoursetup
ManagingInst      : B
MemorySpeed       : 1866
MfgTime           : 2016-07-13T01:00:00.000
Model             : UCSC-C240-M4SX
Name              : Name this server was assigned
NumOfAdaptors     : 1
NumOfCores        : 16
NumOfCoresEnabled : 16
NumOfCpus         : 2
NumOfEthHostIfs   : 1
NumOfFcHostIfs    : 2
NumOfThreads      : 32
OperPower         : on
OperPwrTransSrc   : unknown
OperQualifier     : 
OperState         : ok
Operability       : operable
OriginalUuid      : d7ec25fb-2325-4741-8e8a-9229e3cefc8f
PartNumber        : 74-12504-02
PolicyLevel       : 0
PolicyOwner       : local
Presence          : equipped
Revision          : 0
Serial            : YourSerial
ServerId          : 3
TotalMemory       : 786432
UsrLbl            : YourServerName
Uuid              : 68227356-9ed0-13e5-1000-000000000027
Vendor            : Cisco Systems Inc
Vid               : 0
Dn                : compute/sys-1009/rack-unit-3
Rn                : rack-unit-3
Status            : 
XtraProperty      : {[NumOf40GAdaptorsWithOldFw, 0], [NumOf40GAdaptorsWithUnknownFw, 0]}
UcsCentral        : YourUCSCentralServerName

Note that I changed some of the text above to remove identifiable info, but you get the idea. You can now drill down into this object to get whatever information you may need.

Similarly, you can retrieve other UCS Central objects, and select them using any value you choose, such as:

Service profiles:

$ServiceProfiles = get-ucscentralservicetemplate -ucscentral $ucsCentral
$myServiceProfiles = $ServiceProfiles | where {$_.BootPolicyName -eq "myBootPolicyName"}


$VLANs = get-ucscentralvlan -ucscentral $ucsCentral
$myVLANs = $VLANs | where {$_.Id -eq "20"}

And so on.

Be aware that the "where" statement, depending on which values you are filtering by, may return multiple objects, so it always makes sense to do a quick check to make sure you're getting what you expect.

For example, if you expect only one object to be returned, you may want to check the count returned, and then error out if more or less than one item is there.

if($myServiceProfiles.count -gt 1 -or $myServiceProfiles.count -lt 1)
    "Place your code to handle the error here"
   "One object was successfully returned"

Hopefully this is helpful to get you started with dealing with UCS Central through the PowerTools cmdlets. You will start to realize that almost everything is handled in the same way, so the best way to figure out what you can do with these tools, is to just try it out (in a test environment, of course :) )