Wednesday, October 26, 2016

Automated UCS PowerTool - provision by service profile name - create DHCP & DNS

Install Cisco IMC PowerTool 2.0.2.2+
Save as .PS1

#region Load the UCS PowerTool
Write-Output "Checking Cisco PowerTool"
$PowerToolLoaded = $null
$Modules = Get-Module
$PowerToolLoaded = $modules.name
if ( -not ($Modules -like "Cisco.UCS.Core"))
{
Write-Output " Loading Module: Cisco UCS PowerTool Module"
Import-Module Cisco.UCS.Core
$Modules = Get-Module
if ( -not ($Modules -like "Cisco.UCS.Core"))
{
Write-Output ""
Write-Output "Cisco UCS PowerTool Module did not load.  Please correct his issue and try again"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
else
{
Write-Output " PowerTool is Loaded"
}
}
else
{
Write-Output " PowerTool is Loaded"
}
#endregion
#region UCS Login
#Define UCS Domain(s)
Write-Output ""
Write-Output "Connecting to UCSM"
if (!([string]::IsNullOrEmpty($UCSM)))
{
$myucs = $UCSM
}
else
{
$myucs = Read-Host "Enter UCS system IP or Hostname"
}
if (($myucs -eq "") -or ($myucs -eq $null) -or ($Error[0] -match "PromptingException"))
{
Write-Output ""
Write-Output "You have provided invalid input."
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
else
{
Disconnect-Ucs
}

#Test that UCSM is IP Reachable via Ping
Write-Output ""
Write-Output "Testing reachability to UCSM"
$ping = new-object system.net.networkinformation.ping
$results = $ping.send($myucs)
if ($results.Status -ne "Success")
{
Write-Output " Can not access UCSM $myucs by Ping"
Write-Output ""
Write-Output "It is possible that a firewall is blocking ICMP (PING) Access."
if ($SKIPERROR)
{
$Try = "y"
}
else
{
$Try = Read-Host "Would you like to try to log in anyway? (Y/N)"
}
if ($Try -ieq "y")
{
Write-Output ""
Write-Output "Trying to log in anyway!"
}
elseif ($Try -ieq "n")
{
Write-Output ""
Write-Output "You have chosen to exit"
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
else
{
Write-Output ""
Write-Output "You have provided invalid input.  Please enter (Y/N) only."
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
}
else
{
Write-Output " Successfully pinged UCSM: $myucs"
}

#Allow Logins to single or multiple UCSM systems
$multilogin = Set-UcsPowerToolConfiguration -SupportMultipleDefaultUcs $false

#Log into UCSM
Write-Output ""
Write-Output "Logging into UCSM"

#Verify PowerShell Version to pick prompt type
if (!$UCREDENTIALS)
{
if (!$USAVEDCRED)
{
Write-Output " Enter your UCSM credentials"
$credu = Get-Credential -Message "UCSM(s) Login Credentials" -UserName "network"

}
else
{
$CredFile = import-csv $USAVEDCRED
$Username = $credfile.UserName
$Password = $credfile.EncryptedPassword
$credu = New-Object System.Management.Automation.PsCredential $Username,(ConvertTo-SecureString $Password)
}
}
#Log into UCSM
$myCon = Connect-Ucs $myucs -Credential $credu

#Check to see if log in was successful
if (($myucs | Measure-Object).count -ne ($myCon | Measure-Object).count)
{
Write-Output " Error Logging into UCS."
Write-Output " Make sure your user has login rights the UCS system and has the"
Write-Output " proper role/privledges to use this tool..."
Write-Output " Exiting..."
Disconnect-Ucs
exit
}
else
{
if (!$UCREDENTIALS)
{
Write-Output " Login Successful"
}
else
{
Write-Output " Login Successful"
}
}

#endregion
#region Functions
function New-IPRange {
[cmdletbinding()]
param (
    [parameter( Mandatory = $true,
                Position = 0 )]
    [System.Net.IPAddress]$Start,

    [parameter( Mandatory = $true,
                Position = 1)]
    [System.Net.IPAddress]$End,

    [int[]]$Exclude = @( 0, 1, 255 )
)
    $ip1 = $start.GetAddressBytes()
    [Array]::Reverse($ip1)
    $ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address

    $ip2 = ($end).GetAddressBytes()
    [Array]::Reverse($ip2)
    $ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address

    for ($x=$ip1; $x -le $ip2; $x++)
    {
        $ip = ([System.Net.IPAddress]$x).GetAddressBytes()
        [Array]::Reverse($ip)
        if($Exclude -notcontains $ip[3])
        {
            $ip -join '.'
        }
    }
}
Function Add-DHCP ($script:scope, $script:ReservedIP, $script:MAC, $script:ReservedName){
Write-Output "Adding $script:ReservedIP with $script:ReservedName and $script:MAC to DHCP"
netsh dhcp server "$($script:DHCPServer)" scope $script:scope add reservedip $script:ReservedIP $script:MAC $script:ReservedName

}
Function Add-DNS ($script:SPName, $script:ReservedIP){
$script:SPNamePTR = "$(($script:SPName).ToUpper()).$(($script:domain).ToUpper())"
#$script:addr = $script:ReservedIP -split "\."
#$script:rzone = "$($addr[2]).$($addr[1]).$($addr[0]).in-addr.arpa"

#Create Dns entries
Write-Output "Adding $ReservedIP with $SPNamePTR to DNS"
dnscmd $script:DNSServer /recordadd $script:domain "$($script:SPName)" A "$($script:ReservedIP)"

#Create reverse DNS
#dnscmd $script:DNSServer /recordadd $rzone "$($addr[3])" PTR $SPNamePTR

}

#endregion
#region Select ServiceProfiles
$FullListNICs=Get-UcsServiceProfile | Get-UcsVnic |where {$_.Dn -like "org-root/org-VDi-UCS/ls-BTPESX*/ether-vNIC-A"}
$selectionList=($FullListNICs|%{($_.dn).Split("/")[2].trimstart("ls-")})

$Script:SelectedObjects = @()
$Script:Exit = 'n'

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Service Profiles"
$objForm.Size = New-Object System.Drawing.Size(300,600)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {
        foreach ($objItem in $objListbox.SelectedItems)
        {$Script:SelectedObjects += $objItem}
$objForm.Close()
}
})

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close(); Write-Output "" ; Write-Output "You pressed Escape"; Write-Output " exiting..."; Disconnect-Ucs; $Script:Exit = "y"}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,500)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

$OKButton.Add_Click(
    {
        foreach ($objItem in $objListbox.SelectedItems)
            {$Script:SelectedObjects += $objItem}
        $objForm.Close()
    })

$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,500)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close(); Write-Output ""; Write-Output "You pressed Cancel"; Disconnect-Ucs; $Script:Exit = "y"})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Select from below. SHIFT or CNTRL for multi-select:"
$objForm.Controls.Add($objLabel)

$objListbox = New-Object System.Windows.Forms.Listbox
$objListbox.Location = New-Object System.Drawing.Size(10,40)
$objListbox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Sorted = $True

$objListbox.SelectionMode = "MultiExtended"
if ($selectionList.count -le 1)
{
[void] $objListbox.Items.Add("--ALL--")
}
foreach ($Selection in $selectionList)
{
[void] $objListbox.Items.Add($Selection)
}

$objListbox.Height = 450
$objForm.Controls.Add($objListbox)
$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
#endregion
#region Variables
$script:DNSServer = "10.1.21.99"
$script:DHCPServer= "10.1.21.109"
$script:domain = "$((Get-ADDomain).forest)"
$script:IPSub= Read-Host 'What is the Subnet? (default = "10.1.14.")'
if([string]::IsNullOrEmpty($script:IPSub)){$script:IPSub = "10.1.14."}
$script:IPOct= Read-Host 'What is the Starting Octet? ("50")'
$script:IPCount= $Script:SelectedObjects.count
$startIP= $script:IPSub+$script:IPOct
$endIP=$script:IPSub+([int]($script:IPOct)+[int]($script:IPCount))
$script:IParray=@()
$script:resultsCount=0
$script:outip=""
#endregion

#region Create IP List
New-IPRange -Start $startIP -End $endIP|%{$IParray+=$_}
            foreach ($IP in $IParray){
            $Results=Test-Connection $ip -count 2 -quiet
            If($results -eq $true){$script:resultsCount++;$script:OutIP+="`n$($ip) Is Responding!!"}
            If($script:resultsCount -ge "1"){Write-host "$($script:outip)";break}
                                }
#endregion

#region Process the
$serviceProfiles=$FullListNICs|where{$Script:SelectedObjects -contains (($_.dn).Split("/")[2].trimstart("ls-"))}
$script:SPcount
$Nextip=0
        foreach($serviceProfile in $serviceProfiles){

                $script:SPName = (($serviceProfile.Dn).Split("/")[2].trimstart("ls-"))
                $script:ReservedName= "$(($SPName).ToUpper()).$(($script:domain).ToUpper())"
                $script:MAC = (($serviceprofile.addr).Replace(":","")).tolower()
                $script:scope=("$($script:IPSub)0")
                $script:ReservedIP = $IParray[$Nextip]

                Add-DNS $script:SPName $script:ReservedIP
                Add-DHCP $script:scope $script:ReservedIP $script:MAC $script:ReservedName
                $Nextip++
                    }
#endregion

No comments:

Post a Comment

Vmware NSX SSL creation 

Using OpenSSL for NSX Manager SSL import: Creates CSR and 4096 bit KEY Creating NSX 6.4.2 SSL    openssl req -out nsxcert.csr -newkey rsa:40...