{"id":76,"date":"2021-06-30T13:22:36","date_gmt":"2021-06-30T17:22:36","guid":{"rendered":"https:\/\/nobrandsan.com\/wordpress\/?p=76"},"modified":"2021-06-30T14:08:32","modified_gmt":"2021-06-30T18:08:32","slug":"cisco-ucs-and-vmware-provisioning","status":"publish","type":"post","link":"https:\/\/nobrandsan.com\/wordpress\/index.php\/2021\/06\/30\/cisco-ucs-and-vmware-provisioning\/","title":{"rendered":"Cisco UCS and VMware Provisioning"},"content":{"rendered":"\n<p>These scripts not only deployed the Cisco UCS blades but also downloaded the&nbsp;latest version of ESXi from the VMware website, used this to PXE boot the UCS&nbsp;blades with VMware Auto Deploy and then finally applied created and utilized a&nbsp; host configuration using Host Profiles.<\/p>\n\n\n\n<p>There was very minimal pre-work performed for both UCS and VMware in the&nbsp;environment being demoed in the video below.<\/p>\n\n\n\n<p>For UCS, the pre-work that was performed was racking, stacking, and cabling&nbsp;the physical UCS gear and performing an initial configuration of UCS manager.&nbsp;&nbsp; On the storage side, a 200 GB LUN was created and zoned to a range of 18 WWPN\u2019s&nbsp;as well, of course this could also have been done with PowerShell snapins from&nbsp;some of the storage vendors if needed.<\/p>\n\n\n\n<p>For infrastructure support VMware vCenter and Auto-Deploy software were both installed and&nbsp;configured. DNS, DHCP, and TFTP&nbsp;installed and configured to comply with organizational practices.<\/p>\n\n\n\n<p>From there, PowerShell utilizing the Cisco UCS PowerTool module and VMware&nbsp;PowerCLI snap-ins takes over!!!<\/p>\n\n\n\n<p>Three different scripts were created to perform the following functions:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li><strong>Initial Configuration<\/strong>&nbsp;&#8211; The first scripts configures all of&nbsp;the pools, policies, VLANs, VSANs, Service Profile Templates, etc. needed on the&nbsp;UCS side to create new servers to be used in the cluster to be created in&nbsp;VMware.&nbsp; On the VMware side, this script will download the latest ESXi&nbsp;hypervisor, as well as create separate AutoDeploy Hypervisor and Cluster rules&nbsp;that define the hypervisor version and destination cluster for net new servers&nbsp;with the Service Profile Template name provided in the OEM strings like&nbsp; &#8220;oemstring=$SPT:CL2021&#8221;, where CL2021 is the service profile template name.&nbsp; The&nbsp;script will create a new service profile from a template, associate it with a&nbsp;server from a pool, monitor the progress of the association and addition of the&nbsp;host into the cluster, perform initial configuration of the new host in the&nbsp;cluster and create a host profile from the configuration, and create a rule for&nbsp;the cluster to use the new host profile.<\/li><li><strong>Addition of New Hypervisors<\/strong>&nbsp;&#8211; The second script is run to&nbsp;add new hypervisors to the cluster created in step one.&nbsp; This script&nbsp;accomplishes that by creating a new service profile from the template created in&nbsp;script one, associating it, and booting it which will cause the server to boot&nbsp;via AutoDeploy based on the AutoDeploy and cluster rules created from script&nbsp;one, which are triggered from the service profile template name.&nbsp; The script&nbsp;will verify the host is added to the cluster and is fully compliant with the&nbsp;host profile created in step one.<\/li><li><strong>Rolling Hypervisor and Server Firmware Upgrade of a Cluster<\/strong>&nbsp;&#8211; The&nbsp;third script will download the newest version of a ESXi hypervisor from VMware, create a new AutoDeploy rule utilizing it for servers that are created from the&nbsp;Service Profile Template created in step one.&nbsp; Once the rules are updated, the&nbsp;script will loop through each hypervisor one by one in the cluster by first&nbsp;setting the hypervisor in maintenance which will trigger VM evacuation, shutting&nbsp;the hypervisor down, change the host firmware pack on the service profile which&nbsp;upgrades the firmware on the server, powering the server on which will cause the&nbsp;server to boot the updated version of ESXi via AutoDeploy and rejoin the&nbsp;cluster.&nbsp;<\/li><\/ol>\n\n\n\n<p>These scripts are provided freely as examples of how you can use both UCS&nbsp;PowerTool and VMware PowerCLI together to achieve end to end automation.&nbsp; Please&nbsp;feel free to modify and utilize them as examples of how you might automate UCS&nbsp;and VMware in your own datacenter.<\/p>\n\n\n\n<p>Script 1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# Import Modules\nif ((Get-Module |where {$_.Name -ilike \"CiscoUcsPS\"}).Name -ine \"CiscoUcsPS\")\n    {\n    Write-Host \"Loading Module: Cisco UCS PowerTool Module\"\n    Import-Module CiscoUcsPs\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"Vmware*Core\"}).Name -ine \"VMware.VimAutomation.Core\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VimAutomation Core\"\n    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \" VMware.DeployAutomation\"}).Name -ine \"VMware.DeployAutomation\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.DeployAutomation\"\n    Add-PSSnapin VMware.DeployAutomation -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"VMware.ImageBuilder\"}).Name -ine \"VMware.ImageBuilder\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.ImageBuilder\"\n    Add-PSSnapin VMware.ImageBuilder -ErrorAction SilentlyContinue\n    }\n    \nset-ucspowertoolconfiguration -supportmultipledefaultucs $false\n# Global Variables\n$ucs = \"10.3.0.100\"\n$ucsuser = \"admin\"\n$ucspass = \"@_$ecure_P@$$\"\n$ucsorg = \"org-root\"\n$tenantname = \"CL2021\"\n$macpoolblockfrom = \"00:25:B5:ED:01:01\"\n$macpoolblockto = \"00:25:B5:ED:01:09\"\n$wwpnpoolblockfrom = \"20:00:00:25:B5:ED:02:01\"\n$wwpnpoolblockto = \"20:00:00:25:B5:ED:02:12\"\n$private = \"10\"\n$public = \"206\"\n$vCenter = \"10.3.0.80\"\n$vcuser = \"Administrator@vsphere.local\"\n$vcpass = \"@_$ecure_P@$$\"\n$WarningPreference = \"SilentlyContinue\"\nTry {\n    # Login to UCS\n    Write-Host \"UCS: Logging into UCS Domain: $ucs\"\n    $ucspasswd = ConvertTo-SecureString $ucspass -AsPlainText -Force\n    $ucscreds = New-Object System.Management.Automation.PSCredential ($ucsuser, $ucspasswd)\n    $ucslogin = Connect-Ucs -Credential $ucscreds $ucs\n    # Create Deploy Rule for Hypervisor to be deploy based on SP name and another rule for cluster name\n    Write-Host \"vC: Logging into vCenter: $vCenter\"\n    $vcenterlogin = Connect-VIServer $vCenter -User $vcuser -Password $vcpass | Out-Null\n    # Create Mac Pool\n    Write-Host \"UCS: Creating MAC Pool: $tenantname\"\n    $startucstransaction = Start-UcsTransaction\n    $macpool =  Get-UcsManagedObject -Dn $ucsorg | Add-UcsMacPool -name $tenantname -ModifyPresent\n    $macpoolblock = $macpool | Add-UcsMacMemberBlock -From $macpoolblockfrom -To $macpoolblockto -ModifyPresent\n    $completeucstransaction = Complete-UcsTransaction\n    # Create WWPN Pool\n    Write-Host \"UCS: Creating WWPN Pool: $tenantname\"\n    $startucstransaction = Start-UcsTransaction\n    $wwpnpool = Get-UcsManagedObject -Dn $ucsorg  | Add-UcsWwnPool -Name $tenantname -Purpose \"port-wwn-assignment\" -ModifyPresent\n    $wwpnpoolblock = $wwpnpool | Add-UcsWwnMemberBlock -From $wwpnpoolblockfrom -To $wwpnpoolblockto -ModifyPresent\n    $completeucstransaction = Complete-UcsTransaction\n    # Create Server Pool\n    Write-Host \"UCS: Creating Server Pool: $tenantname\"\n    $serverpool =  Get-UcsManagedObject -Dn $ucsorg | Add-UcsServerPool -Name $tenantname -ModifyPresent\n    # Create Server Qualification Policy\n    Write-Host \"UCS: Creating Server Qualification Policy: $tenantname\"\n    $startucstransaction = Start-UcsTransaction\n    $serverqualpol = Get-UcsManagedObject -Dn $ucsorg | Add-UcsServerPoolQualification -Name $tenantname -ModifyPresent\n    $serveradaptorqual = $serverqualpol | Add-UcsAdaptorQualification -ModifyPresent\n    $serveradaptorcapqual = $serveradaptorqual | Add-UcsAdaptorCapQualification -Maximum \"unspecified\" -Model \"N20-AC0002\" -Type \"virtualized-eth-if\" -ModifyPresent\n    $servercpuqual = $serverqualpol | Add-UcsCpuQualification -Model \"N20-X00002\" \n    $completeucstransaction = Complete-UcsTransaction\n    # Create Server Pool Policy (for dynamic server pools based on qualification policy)\n    Write-Host \"UCS: Creating Server Pool Policy: $tenantname\"\n    $startucstransaction = Start-UcsTransaction\n    $serverpoolpol = Get-UcsManagedObject -Dn $ucsorg | Add-UcsServerPoolPolicy -Name $tenantname -PoolDn $serverpool.dn -Qualifier $serverqualpol.Name -ModifyPresent\n    $completeucstransaction = Complete-UcsTransaction\n    # Create Boot Policy\n    Write-Host \"UCS: Creating Boot Policy: $tenantname\"\n    $startucstransaction = Start-UcsTransaction\n    $bootpol = Get-UcsManagedObject -Dn $ucsorg | Add-UcsBootPolicy -EnforceVnicName \"yes\" -Name $tenantname -RebootOnUpdate \"no\" -ModifyPresent\n    $pxe = $bootpol | Add-UcsLsbootLan -ModifyPresent -Order \"1\" -Prot \"pxe\"  \n    $eth0 = $pxe | Add-UcsLsbootLanImagePath -BootIpPolicyName \"\" -ISCSIVnicName \"\" -ImgPolicyName \"\" -ImgSecPolicyName \"\" -ProvSrvPolicyName \"\" -Type \"primary\" -VnicName \"eth0\"  -ModifyPresent\n    $completeucstransaction = Complete-UcsTransaction\n    # Create any needed VLANs\n    Write-Host \"UCS: Creating VLAN vlan$private\"\n    $vlanprivate = get-ucslancloud | Add-UcsVlan -Name vlan$private -Id $private -ModifyPresent\n    Write-Host \"UCS: Creating VLAN vlan$public\"\n    $vlanpublic = get-ucslancloud | Add-UcsVlan -Name vlan$public -Id $public -ModifyPresent\n    # Create any needed VSANs\n    Write-Host \"UCS: Creating VSAN default\"\n    $vsan1 = Get-UcsSanCloud | Add-UcsVsan -Name default -Id 1 -ModifyPresent\n    # Create Service Profile Template (using MAC, WWPN, Server Pools, VLANs, VSans, Boot Policy, etc. previously created steps) with desired power state to down\n    Write-Host \"UCS: Creating SP Template: $tenantname in UCS org: $ucsorg\"\n    $startucstransaction = Start-UcsTransaction\n    $sptemplate = Get-UcsManagedObject -Dn $ucsorg |  Add-UcsServiceProfile -BootPolicyName $tenantname -HostFwPolicyName \"default\" -IdentPoolName \"default\" -LocalDiskPolicyName \"default\" -MaintPolicyName \"default\" -Name $tenantname -PowerPolicyName \"default\" -StatsPolicyName \"default\" -Type \"initial-template\" \n    $sptvnic1 = $sptemplate | Add-UcsVnic -AdaptorProfileName \"VMWare\" -AdminVcon \"any\" -IdentPoolName $tenantname -Mtu 1500 -Name \"eth0\" -Order \"1\" -StatsPolicyName \"default\" -SwitchId \"A-B\"\n    $vnic1private = $sptvnic1 | Add-UcsVnicInterface -DefaultNet \"yes\" -Name $vlanprivate.Name\n    $sptvnic2 = $sptemplate | Add-UcsVnic -AdaptorProfileName \"VMWare\" -AdminVcon \"any\" -IdentPoolName $tenantname -Mtu 1500 -Name \"eth1\" -Order \"2\" -StatsPolicyName \"default\" -SwitchId \"B-A\"\n    $vnic2public = $sptvnic2 | Add-UcsVnicInterface -DefaultNet \"yes\" -Name $vlanpublic.Name\n    $sptfc0 = $sptemplate | Add-UcsVhba -AdaptorProfileName \"VMWare\" -AdminVcon \"any\" -IdentPoolName $tenantname -MaxDataFieldSize 2048 -Name \"fc0\" -Order \"3\" -PersBind \"disabled\" -PersBindClear \"no\" -StatsPolicyName \"default\" -SwitchId \"A\"\n    $fc0vsan = $sptfc0 | Add-UcsVhbaInterface -Name $vsan1.Name\n    $sptfc1 = $sptemplate | Add-UcsVhba -AdaptorProfileName \"VMWare\" -AdminVcon \"any\" -IdentPoolName $tenantname -MaxDataFieldSize 2048 -Name \"fc1\" -Order \"4\" -PersBind \"disabled\" -PersBindClear \"no\" -StatsPolicyName \"default\" -SwitchId \"B\"\n    $fc1vsan = $sptfc1 | Add-UcsVhbaInterface -Name $vsan1.Name\n    $sptwwnn = $sptemplate | Add-UcsVnicFcNode -IdentPoolName \"node-default\"\n    $sptserverpool = $sptemplate | Add-UcsServerPoolAssignment -Name $tenantname -RestrictMigration \"no\"\n    $sptpowerstate = $sptemplate | Set-UcsServerPower -State \"down\" -Force\n    $completeucstransaction = Complete-UcsTransaction\n    # Add ESXi Image Profile from VMware.com\n    Write-Host \"vC: Adding VMware ESXi Library from online depot\"\n    $SoftwareDepot = Add-EsxSoftwareDepot \"https:\/\/hostupdate.vmware.com\/software\/VUM\/PRODUCTION\/main\/vmw-depot-index.xml\"\n    $LatestImageProfile = Get-EsxImageProfile | Where { $_.Name -match \"no-tools\" } | Sort ModifiedTime -desc | select -first 1\n    # Create Auto-deploy rule using the Service Profile Template OEM string from UCS\n    $pattern = \"oemstring=`$SPT:$($SPTemplate.name)\"\n    Write-Host \"vC: Creating ESXi deploy rule for '$($pattern)'\"\n    $DeployRule = New-DeployRule -Name \"DeployESXiImage\" -Item $LatestImageProfile -Pattern $pattern\n    $AddRule = $DeployRule | Add-DeployRule\n    # Create vCenter Cluster\n    Write-Host \"vC: Creating vCenter Cluster: $tenantname if it doesnt already exist\"\n    If (-Not (Get-Cluster $tenantname -ErrorAction SilentlyContinue)) {\n        $Cluster = Get-Datacenter | Select -First 1 | New-Cluster $tenantname\n    } Else {\n        $Cluster = Get-Cluster $tenantname\n    }\n    Write-Host \"vC: Enabling DRS and setting mode to 'FullyAutomated' for Cluster: $($Cluster)\"\n    $DRSenable = $Cluster | Set-Cluster -DrsEnabled:$true -DrsMode FullyAutomated -drsautomationlevel FullyAutomated -Confirm:$false\n    \n    Write-Host \"vC: Creating vCenter Cluster rule for '$($pattern)'\"\n    $DeployRule = New-DeployRule -Name \"AddHostsTo$($tenantname)Cluster\" -Item $Cluster -Pattern $pattern\n    $AddRule = $DeployRule | Add-DeployRule\n    Write-Host \"vC: Repairing active ruleset\"\n    $RepairRules = Get-VMHost | Test-DeployRuleSetCompliance | Repair-DeployRuleSetCompliance -ErrorAction SilentlyContinue\n    # Create Initial ESXi Host from SP Template in UCS\n    Write-Host \"UCS: Creating new Service Profile from SP Template: $($sptemplate.name)\"\n    $createnewsp = $sptemplate | Add-UcsServiceProfileFromTemplate -Count 1 -DestinationOrg (Get-UcsManagedObject -Dn $ucsorg) -Prefix \"esxi-host\"\n    $spmacaddr = $createnewsp | Get-UcsVnic -Name eth0\n    # Monitor UCS SP Associate for completion\n    Write-Host \"UCS: Waiting for UCS SP: $($createnewsp.name) to complete SP association process\"\n        do {\n            if ( (Get-UcsManagedObject -Dn $createnewsp.Dn).AssocState -ieq \"associated\")\n            {\n                break\n            }\n            Sleep 40\n        } until ((Get-UcsManagedObject -Dn $createnewsp.Dn).AssocState -ieq \"associated\")\n            \n    # Set SP Desired Power State to UP in newly created SP\n    Write-Host \"UCS: Setting Desired Power State to 'up' of Service Profile: $createnewsp\"\n    $powerspon = $createnewsp | Set-UcsServerPower -State \"up\" -Force\n    # Wait for Hypervisor to boot from network w\/ Auto-Deploy and connect to vCenter\n    Write \"vC: Waiting for Host with MAC address of $($spmacaddr.Addr) to connect to vCenter\"\n    do {\n        Sleep 40\n    } until ($VMHost = (get-vmhost -ErrorAction SilentlyContinue | foreach { $_.NetworkInfo.PhysicalNic | where { $_.Mac -ieq $spmacaddr.Addr } } | select -expandproperty vmhost ))\n    If ($VMHost.State -ne \"Maintenance\") {\n        $Maint = $VMHost | Set-VMHost -State Maintenance\n    }\n    # Config changes to host before host profile\n    Write \"vC: COnfiguring VM Hypervisor Host: $($VMhost.name) before creating a Host Profile\"\n    Write \"vC: Removing defualt VM Network\"\n    $VMNetwork = Get-VirtualPortGroup -Name \"VM Network\" -VMHost $VMHost | Remove-VirtualPortGroup -Confirm:$false\n    Write \"vC: Creating VMotion Network\"\n    $Vmotion = New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup \"VMotion\" -VirtualSwitch (Get-VirtualSwitch -VMHost $vmhost -Name vSwitch0) -VMotionEnabled:$true\n    Write \"vC: Creating New Virtual Switch\"\n    $vSwitch1 = New-VirtualSwitch -VMHost $vmhost -Name \"vSwitch1\" -Nic (Get-VMHostNetworkAdapter -VMHost $vmhost -Name \"vmnic1\")\n    Write \"vC: Creating New VM Network\"\n    $VMNetwork = $vSwitch1 | New-VirtualPortGroup \"VM Network\"\n    Write \"vC: Setting Syslog Server\"\n    $SyslogServer = Set-VMHostSysLogServer -SysLogServer $vCenter -SysLogServerPort 514 -VMHost $VMhost\n    Write-Host \"vC: Creating Host Profile: $tenantname from VM Hypervisor Host: $($VMhost.name)\"\n    If (-Not (Get-VMHostProfile $tenantname -ErrorAction SilentlyContinue) ) {\n        $HostProfile = New-VMHostProfile -Name $tenantname -Description \"Automatically generated host profile for $tenantname\" -ReferenceHost $VMHost\n        # Edit the host profile to add the static password entry\n        function Copy-Property ($From, $To, $PropertyName =\"*\")\n        {\n           foreach ($p in Get-Member -In $From -MemberType Property -Name $propertyName)\n           {  trap {\n                 Add-Member -In $To -MemberType NoteProperty -Name $p.Name -Value $From.$($p.Name) -Force\n                 continue\n              }\n              $To.$($P.Name) = $From.$($P.Name)\n           }\n        }\n        $hostProfileName = $tenantname\n        $newAdminPswd = \"VMw@re123\"\n        $spec = New-Object VMware.Vim.HostProfileCompleteConfigSpec\n        Copy-Property -From $HostProfile.ExtensionData.Config -To $spec\n        $secpol = New-Object VMware.Vim.ProfilePolicy\n        $secpol.Id = \"AdminPasswordPolicy\"\n        $secpol.PolicyOption = New-Object VMware.Vim.PolicyOption\n        $secpol.PolicyOption.Id = \"FixedAdminPasswordOption\"\n        $secpol.PolicyOption.Parameter += New-Object VMware.Vim.KeyAnyValue\n        $secpol.PolicyOption.Parameter&#91;0].Key = \"password\"\n        $secpol.PolicyOption.Parameter&#91;0].Value = New-Object VMware.Vim.PasswordField\n        $secpol.PolicyOption.Parameter&#91;0].Value.Value = $newAdminPswd\n        $spec.ApplyProfile.Security.Policy = @($secpol)\n        $ChangeHostProfile = $HostProfile.ExtensionData.UpdateHostProfile($spec)\n    } else {\n        $Hostprofile = Get-VMHostProfile $tenantname\n    }\n\n    # Add a new Deployrule to associate the host profile to the Hosts\n    Write-Host \"vC: Adding rule to use host profile\"\n    $DeployRule = New-DeployRule -Name \"$($tenantname)HostProfile\" -Item $HostProfile -Pattern $pattern\n    $AddRule = $DeployRule | Add-DeployRule\n    Write-Host vC: \"Repairing active ruleset\"\n    $RepairRules = $VMHost | Test-DeployRuleSetCompliance | Repair-DeployRuleSetCompliance -ErrorAction SilentlyContinue\n    Write-Host \"vC: Assigning HostProfile to new host\"\n    $Assign = $VMHost | Apply-VMHostProfile -profile $HostProfile -Confirm:$false\n    $Test = $VMHost | Test-VMHostProfileCompliance\n    Write-Host \"vC: VM Hypervisor Host: $($VMHost.Name) Ready to use, removing Maintenance mode\"\n    $Maint = $VMHost | Set-VMHost -State Connected\n    # Logout of UCS\n    Write-Host \"UCS: Logging out of UCS: $ucs\"\n    $ucslogout = Disconnect-Ucs \n    # Logout of vCenter\n    Write-Host \"vC: Logging out of vCenter: $vCenter\"\n    $vcenterlogout = Disconnect-VIServer $vCenter -Confirm:$false\n}\nCatch\n{\n     Write-Host \"Error occurred in script:\"\n     Write-Host ${Error}\n     exit\n}\n<\/code><\/pre>\n\n\n\n<p>Script 2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Import Modules\nif ((Get-Module |where {$_.Name -ilike \"CiscoUcsPS\"}).Name -ine \"CiscoUcsPS\")\n    {\n    Write-Host \"Loading Module: Cisco UCS PowerTool Module\"\n    Import-Module CiscoUcsPs\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"Vmware*Core\"}).Name -ine \"VMware.VimAutomation.Core\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VimAutomation Core\"\n    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \" VMware.DeployAutomation\"}).Name -ine \"VMware.DeployAutomation\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.DeployAutomation\"\n    Add-PSSnapin VMware.DeployAutomation -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"VMware.ImageBuilder\"}).Name -ine \"VMware.ImageBuilder\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.ImageBuilder\"\n    Add-PSSnapin VMware.ImageBuilder -ErrorAction SilentlyContinue\n    }    \n    \n    \nset-ucspowertoolconfiguration -supportmultipledefaultucs 0\n# Global Variables\n$ucs = \"10.3.0.100\"\n$ucsuser = \"admin\"\n$ucspass = \"@_$ecure_P@$$\"\n$ucsorg = \"org-root\"\n$tenantname = \"CL2021\"\n$vCenter = \"10.3.0.80\"\n$vcuser = \"Administrator@vsphere.local\"\n$vcpass = \"@_$ecure_P@$$\"\n$WarningPreference = \"SilentlyContinue\"\nTry\n{\n    # Login to UCS\n    Write-Host \"UCS: Logging into UCS Domain: $ucs\"\n    $ucspasswd = ConvertTo-SecureString $ucspass -AsPlainText -Force\n    $ucscreds = New-Object System.Management.Automation.PSCredential ($ucsuser, $ucspasswd)\n    $ucslogin = Connect-Ucs -Credential $ucscreds $ucs\n    # Login to vCenter\n    Write-Host \"vC: Logging into vCenter: $vCenter\"\n    $vcenterlogin = Connect-VIServer $vCenter -User $vcuser -Password $vcpass | Out-Null\n    # Create Initial ESXi Host\n    Write-Host \"UCS: Creating new Service Profile from SP Template: ${tenantname}\"\n    $sptemplate = get-ucsmanagedobject -dn $ucsorg | Get-UcsServiceProfile -Type \"initial-template\" -Name $tenantname\n    $createnewsp = $sptemplate | Add-UcsServiceProfileFromTemplate -Count 1 -DestinationOrg (Get-UcsManagedObject -Dn $ucsorg) -Prefix \"esxi-host\"\n    $spmacaddr = $createnewsp | Get-UcsVnic -Name eth0\n    # Monitor UCS SP Associate for completion\n    Write-Host \"UCS: Waiting for UCS SP: $($createnewsp.name) to complete SP association process\"\n        do {\n            if ( (Get-UcsManagedObject -Dn $createnewsp.Dn).AssocState -ieq \"associated\" )\n            {\n                break\n            }\n            Sleep 40\n        } until ( (Get-UcsManagedObject -Dn $createnewsp.Dn).AssocState -ieq \"associated\" )\n            \n    Write-Host \"UCS: Setting Desired Power State to 'up' of Service Profile: $($createnewsp.name)\"\n    $powerspon = $createnewsp | Set-UcsServerPower -State \"up\" -Force\n    Write \"vC: Waiting for VM Hypervisor Host with MAC address of $($spmacaddr.Addr) to connect to vCenter\"\n    do {\n        Sleep 40\n    } until ($VMHost = (get-vmhost -ErrorAction SilentlyContinue | foreach { $_.NetworkInfo.PhysicalNic | where { $_.Mac -ieq $spmacaddr.Addr } } | select -expandproperty vmhost ))\n    Write \"vC: Putting VM Hypervisor Host: $($VMhost.Name) into Maintenance mode\"\n    If ($VMHost.State -ne \"Maintenance\") {\n        $Maint = $VMHost | Set-VMHost -State Maintenance\n    }\n    Write-Host \"vC: Checking HostProfile Compliance against new VM Hypervisor Host: $($VMhost.Name) \"\n    $Test = $VMHost | Test-VMHostProfileCompliance\n    Write-Host \"vC: VM Hypervisor Host: $($VMhost.Name) Ready to use, removing Maintenance mode\"\n    $Maint = $VMHost | Set-VMHost -State Connected\n    #Disconnect from UCS\n    Write-Host \"UCS: Logging out of UCS Domain: $ucs\"\n    $ucslogout = Disconnect-Ucs \n    #disconnect from vCenter\n    Write-Host \"vC: Logging out of vCenter: $vCenter\"\n    $vcenterlogout = Disconnect-VIServer $vCenter -confirm:$false\n}\nCatch\n{\n     Write-Host \"Error occurred in script:\"\n     Write-Host ${Error}\n     exit\n}\n<\/code><\/pre>\n\n\n\n<p>Script 3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Import Modules\nif ((Get-Module |where {$_.Name -ilike \"CiscoUcsPS\"}).Name -ine \"CiscoUcsPS\")\n    {\n    Write-Host \"Loading Module: Cisco UCS PowerTool Module\"\n    Import-Module CiscoUcsPs\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"Vmware*Core\"}).Name -ine \"VMware.VimAutomation.Core\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VimAutomation Core\"\n    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"Vmware*Core\"}).Name -ine \"VMware.VimAutomation.Core\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VimAutomation Core\"\n    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \" VMware.DeployAutomation\"}).Name -ine \"VMware.DeployAutomation\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.DeployAutomation\"\n    Add-PSSnapin VMware.DeployAutomation -ErrorAction SilentlyContinue\n    }\nif ((Get-PSSnapin | where {$_.Name -ilike \"VMware.ImageBuilder\"}).Name -ine \"VMware.ImageBuilder\")\n    {\n    Write-Host \"Loading PS Snap-in: VMware VMware.ImageBuilder\"\n    Add-PSSnapin VMware.ImageBuilder -ErrorAction SilentlyContinue\n    }\n    \nset-ucspowertoolconfiguration -supportmultipledefaultucs 0\n# Global Variables\n$ucs = \"10.3.0.100\"\n$ucsuser = \"admin\"\n$ucspass = \"@_$ecure_P@$$\"\n$ucsorg = \"org-root\"\n$tenantname = \"CL2021\"\n$vCenter = \"10.3.0.80\"\n$vcuser = \"Administrator@vsphere.local\"\n$vcpass = \"@_$ecure_P@$$\"\n$WarningPreference = \"SilentlyContinue\"\n$NewImageProfile = \"C:\\VMware\\ESX7.1.0.NextImageProfile.zip\"\n$ucshfpname = \"4.1.3c\"\n$WarningPreference = \"SilentlyContinue\"\ntry {\n    # Login to UCS\n    Write-Host \"UCS: Logging into UCS Domain: $ucs\"\n    $ucspasswd = ConvertTo-SecureString $ucspass -AsPlainText -Force\n    $ucscreds = New-Object System.Management.Automation.PSCredential ($ucsuser, $ucspasswd)\n    $ucslogin = Connect-Ucs -Credential $ucscreds $ucs\n    \n    Write-Host \"vC: Logging into vCenter: $vCenter\"\n    $vcenterlogin = Connect-VIServer $vCenter -User $vcuser -Password $vcpass | Out-Null\n    Write-Host \"vC: Disable the current ESXi Image DeployRule\" \n    $RemDepRule = Get-DeployRule -Name \"DeployESXiImage\" | Remove-DeployRule\n    $ESXDepot = Add-EsxSoftwareDepot \"https:\/\/hostupdate.vmware.com\/software\/VUM\/PRODUCTION\/main\/vmw-depot-index.xml\"\n    #$ESXDeppt = Add-EsxSoftwareDepot $NewImageProfile\n    $LatestImageProfile = Get-EsxImageProfile | sort ModifiedTime -Descending | Select -First 1\n    $pattern = \"oemstring=`$SPT:$($tenantname)\"\n    Write-Host \"vC: Creating ESXi deploy rule for '$($pattern)'\"\n    $NewRule = New-DeployRule -Name \"AddHostsTo$($tenantname)ClusterUpdatedImage\" -Item $LatestImageProfile -Pattern $pattern\n    $SetActive = $NewRule | Add-DeployRule\n    Write-Host \"vC: Repairing active ruleset\"\n    $RepairRules = Get-VMHost | Test-DeployRuleSetCompliance | Repair-DeployRuleSetCompliance -ErrorAction SilentlyContinue\n    Foreach ($VMHost in (Get-Cluster $tenantname | Get-VMHost )) {\n        Write-Host \"vC: Adding VM Hypervisor Host: $($VMHost.Name) into maintenance mode\"\n        $Maint = $VMHost | Set-VMHost -State Maintenance\n        \n        Write-Host \"vC: Waiting for VM Hypervisor Host: $($VMHost.Name) to enter Maintenance Mode\"\n        do {\n            Sleep 10\n        } until ((Get-VMHost $VMHost).State -eq \"Maintenance\")\n        \n        Write-Host \"vC: VM Hypervisor Host: $($VMHost.Name) now in Maintenance Mode, shutting down Host\"\n        $Shutdown = $VMHost.ExtensionData.ShutdownHost($true)\n        \n        Write-Host \"UCS: Correlating VM Hypervisor Host: $($VMHost.Name) to running UCS Service Profile (SP)\"\n        $vmMacAddr = $vmhost.NetworkInfo.PhysicalNic | where { $_.name -ieq \"vmnic0\" }\n        \n        $sp2upgrade =  Get-UcsServiceProfile | Get-UcsVnic -Name eth0 |  where { $_.addr -ieq  $vmMacAddr.Mac } | Get-UcsParent \n        \n        Write-Host \"UCS: VM Hypervisor Host: $($VMhost.Name) is running on UCS SP: $($sp2upgrade.name)\"\n        Write-Host \"UCS: Waiting to for UCS SP: $($sp2upgrade.name) to gracefully power down\"\n         do {\n            if ( (get-ucsmanagedobject -dn $sp2upgrade.PnDn).OperPower -eq \"off\")\n            {\n                break\n            }\n            Sleep 40\n        } until ((get-ucsmanagedobject -dn $sp2upgrade.PnDn).OperPower -eq \"off\" )\n        Write-Host \"UCS: UCS SP: $($sp2upgrade.name) powered down\"\n        \n        Write-Host \"UCS: Setting desired power state for UCS SP: $($sp2upgrade.name) to down\"\n        $poweron = $sp2upgrade | Set-UcsServerPower -State \"down\" -Force\n        Write-Host \"UCS: Changing Host Firmware pack policy for UCS SP: $($sp2upgrade.name) to '$($ucshfpname)'\"\n        $updatehfp = $sp2upgrade | Set-UcsServiceProfile -HostFwPolicyName (Get-UcsFirmwareComputeHostPack -Name $ucshfpname).Name -Force\n        \n        Write-Host \"UCS: Acknowlodging any User Maintenance Actions for UCS SP: $($sp2upgrade.name)\"\n        if (($sp2upgrade | Get-UcsLsmaintAck| measure).Count -ge 1)\n            {\n                $ackuserack = $sp2upgrade | get-ucslsmaintack | Set-UcsLsmaintAck -AdminState \"trigger-immediate\" -Force\n            }\n        Write-Host \"UCS: Waiting for UCS SP: $($sp2upgrade.name) to complete firmware update process for Host Firmware pack '$($ucshfpname)'\"\n        do {\n            Sleep 40\n        } until ((Get-UcsManagedObject -Dn $sp2upgrade.Dn).AssocState -ieq \"associated\")\n        \n        Write-Host \"UCS: Host Firmware Pack update process comlete.  Setting desired power state for UCS SP: $($sp2upgrade.name) to 'up'\"\n        $poweron = $sp2upgrade | Set-UcsServerPower -State \"up\" -Force\n        \n        Write \"vC: Waiting for VM Hypervisor Host: $($VMHost.Name) to connect to vCenter\"\n        do {\n            Sleep 40\n        } until (($VMHost = Get-VMHost $VMHost).ConnectionState -eq \"Connected\" )\n    }\n    # Logout of UCS\n    Write-Host \"UCS: Logging out of UCS Domain: $ucs\"\n    $ucslogout = Disconnect-Ucs \n    # Logout of vCenter\n    Write-Host \"vC: Logging out of vCenter: $vCenter\"\n    $vcenterlogout = Disconnect-VIServer $vCenter -Confirm:$false\n}\nCatch \n{\n     Write-Host \"Error occurred in script:\"\n     Write-Host ${Error}\n     exit\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>These scripts not only deployed the Cisco UCS blades but also downloaded the&nbsp;latest version of ESXi from the VMware website, used this to PXE boot the UCS&nbsp;blades with VMware Auto Deploy and then finally applied created and utilized a&nbsp; host configuration using Host Profiles. There was very minimal pre-work performed for both UCS and VMware&hellip;&nbsp;<a href=\"https:\/\/nobrandsan.com\/wordpress\/index.php\/2021\/06\/30\/cisco-ucs-and-vmware-provisioning\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Cisco UCS and VMware Provisioning<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[1],"tags":[5,6,3,4],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell","tag-scripting","tag-ucs","tag-vmware"],"_links":{"self":[{"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":5,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nobrandsan.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}