Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Saturday, 26 September 2015

Nutanix cmdlets and PowerCLI One Liners and then some [Updated]

I really like powershell but I never used it to its full potential. A recent change in job role sees me more involved in the day to day stuff so I started brushing up on my coding skills. I will be sharing some of the code I find useful. I still consider myself a novice so don't expect anything fancy but please share your thoughts, I would love to learn from your experiences.

Stop a protection domain replication


This one liner returns all replications
Get-NTNXProtectionDomainReplication | Select ProtectionDomainName, ID
The above command will return the name and ID of the replication. You will need to specify these when you want to stop the replication
Abort-NTNXProtectionDomainReplication -ProtectionDomainName PD1 -ID 1234567

Using PowerCLI and vSphere tags for VM lifecycle


VM sprawl can get seriously out of control. I recently deleted 50 VM in an environment after it appeared they were no longer required or worse, nobody knew what they were used for. Read More

Find unprotected VM on your Nutanix cluster


This one liner returns which VM are currently not protected.
Get-NTNXUnprotectedVM | Select vmName

List protected VM in a protection domain


This one liner lists all VM in a given protection domain
Get-NTNXVM  | where {$_.protectiondomainame -eq "My_PD"} | Select vmname

Add unprotected VM to a protection domain


This one liner adds a VM to a protection domain
Add-NTNXProtectionDomainVM  -name "My_PD" -names "VM1"

Remove protected VM from a protection domain


This one liner removes a VM from a protection domain
Remove-NTNXProtectionDomainVM  -name "My_PD" -input "VM1"

Configuring the scratch partition


Configure the scratch partition of your ESXi host when making use of a SD install Read More
Disclaimer: Please use examples at your own risk. I take no responsibility for any possible damage to your infrastructure.

Sunday, 2 August 2015

Using PowerCLI and vSphere tags for VM lifecycle

VM sprawl can get seriously out of control. I recently deleted 50 VM in an environment after it appeared they were no longer required or worse, nobody knew what they were used for. Some of these VM were deployed a few years ago and were never used. The obvious thing to do here was to delete them. Going forward I want to ensure that all VM get reviewed on an annual basis so that they can be deleted if no longer required. One good way of identifying objects in vCenter is by making use of tags. Before you can create tags you will need to have a tag category. I created a category called "Expiry Date" and ensured that it is only applicable to virtual machines

New-TagCategory -Name "Expiry Date" -Cardinality "Single" -EntityType "VirtualMachine" -Description "Expiry Date for VM"

The idea is that I check my VM once a month and I want to retrieve a listing of all VM that expire in a given month. I will need to create a tag for each month. Additionally I want a tag that is called "No Expiry" which I will apply to the foundation infrastucture such as Active Directory servers or Nutanix CVM. I will place all tags in an array and then create a tag for each.

$Months = @("January", "February","March","April","May","June","July","August","September","October","November","December","No Expiry")

ForEach($Month in $Months){
    Get-TagCategory "Expiry Date" | New-Tag -Name $Month -Description "VM that expire in $Month"    
}


With the tags in place we can now assign them to the VM.

$Tag = Get-Tag "August"
Get-vm VM1 | New-TagAssignment -Tag 

Assigning tags one by one would have taken me a long time so I have written a function that should make life a bit easier

function Set-VMExpiry {
<#
.SYNOPSIS
    Set the Expiry month of a VM.
.DESCRIPTION
    Set the Expiry month of a VM . This will allow for determining when VM is due for review.
.NOTES
    Author      : Guy Defryn
    Version     : 1.0
    Date        : 28/7/2015   
.PARAMETER VirtualMachine
    Specify the VM to which apply expiry date
.PARAMETER Month
    Specify the month in which the VM expires
.EXAMPLE
    Set-VMExpiry -VirtualMachine VM1 -Month July
.EXAMPLE
    Import-CSV .\vmexpiry.csv | Set-VMExpiry
    Add all expiry dates as defined in the csv file vmexpiry.csv.
#>
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True)][String] $VirtualMachine,
    [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$True)][String] $Month
    )
    Begin{
        If (!$Month) {
            $Month = get-date -Format MMMM
        }
    }
    Process{
        write-output "Setting Expiry for $VirtualMachine to $Month"
        $VM = get-vm $Virtualmachine
        $OldTag = Get-TagAssignment $VM -Category "Expiry Date"
        if($OldTag){
           Remove-Tagassignment $OldTag -Confirm:$false 
        }
        $VM | New-TagAssignment -Tag $Month | out-null
    }
    End{
    }
}

Now that we have our VM tagged with an expiry we can report on these. I have created a function that will allow to get you VM that are set to expire in given month. I also like to know which VM are not tagged so I can actually go and tag these accordingly.

function Get-VMExpiry {
<#
.SYNOPSIS
    Get a list of VM that are expiring based on month input
.DESCRIPTION
    Get a list of VM that are expiring based on month input. This will allow for determining if the VM is still required.
.NOTES
    Author      : Guy Defryn
    Version     : 1.1
    Date        : 31/7/2015   
.PARAMETER MONTH
    Specify Month to obtain list of expired VM. If not specified current month will be returned.
.PARAMETER ExportPath
    Specify the path if you want to export to CSV
.EXAMPLE
    Get-VMExpiry -Month July
    Get VM that expire in the month of July
.EXAMPLE
    Get-VMExpiry
    Get VM that expire in current month
.EXAMPLE
    Get-VMExpiry -Month "No Expiry"
    Get VM that are set to not expire
.EXAMPLE
    Get-VMExpiry -Month Unspecified
    Get VM that have no expiry date tag set
.EXAMPLE
    Get-VMExpiry -Month Unspecified -ExportPath C:\Temp\tags.csv
    Get VM that have no expiry date tag set and export it to file
.TODO
#>
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$false)][String] $Month,
    [Parameter(Mandatory=$false)][String] $ExportPath

    )
    Begin{
        If(!$Month){
            $Month = get-date -Format MMMM
        }
        $Category = Get-TagCategory -Name "Expiry Date"
        
    }
    Process{
        If($Month -eq "No Expiry"){
            write-output "VM which are not expiring"
            $Tags = get-vm -tag $Month | select Name
        }
        ElseIf($Month -eq "Unspecified"){
            write-output "VM which have no expiry date specified"
            $Tags = get-vm | Where {!(Get-Tagassignment -Entity $_ -Category $Category)} | Select Name
        }
        Else{
            write-output "VM expiring during the month of $Month"
            $Tags = get-vm -tag $Month | select Name
        }
    }
    End{
        $Tags
        if($ExportPath){
            $Tags | export-csv $ExportPath -NoTYpeInformation
        }
    }
}

Thursday, 30 July 2015

Installing Nutanix Powershell cmdlets

Before you can start making use of Nutanix Powershell you will need to install the Nutanix cmdlets that are provided in an installer. You can easily install them from your PRISM interface.

  • Log in to PRISM and click the gear icon
  • Click Download cmdlet installer to download installer
  • Start the installer 
  • Accept default path
  • Click Finish
Once the installer has finished you will need to install snapin. You can do this by running the following command:

.\InstallUtil.exe “C:\Program Files (x86)\Nutanix Inc\NutanixCmdlets\Modules\NutanixCmdletsPSSnapin.dll”

You will need to run the install.exe from the .NET directory. There are several of these depending on the .Net frameworks you have installed. In my case it was C:\Windows\Microsoft.Net\Framework\v4.0.30319

You also need to ensure that the snapin loads when you start your powershell session. Run "notepad $profile" and add the following to your powershell profile

Add-PsSnapin NutanixCmdletsPSSnapin

That should be it. Currently there are around 150 Nutanix cmdlets. Make sure you print off this very handy poster and hang it above your desk.