PowerCLI front end tool

This topic contains 5 replies, has 2 voices, and was last updated by  Mohammed Sabuwala 4 years, 1 month ago.

  • Author
    Posts
  • #9615

    Mohammed Sabuwala
    Participant

    Hi There,

    I am in the process of building a tool which captures information of VMware infrastructure to a web dashboard. This involves SQL DB in the backened which will store the data, a web front end built with wavemaker and WAR files deployed on a Tomcat server.

    The requirement is to be able to run powercli for each anamoly, for eg. retrieve information about active Snapshots etc...

    Some more requirements:
    – The dashboard will allow the user to enter the vCenter credentials
    – Dashboard will allow the user to enter the script names (these scripts will be stored at a specified location)
    – create master script which first connects to the SQL database, retrieve the vcenter credentials and script list and execute all the scripts one by one

    What should my approach be here?
    – should I create individual scripts which captures the output to the database
    – how should i build my master script so that it runs all the other anomaly scripts

    Suggestions please.

    Regards,
    Mohammed

  • #9616

    Poshoholic
    Participant

    Here's how I would approach it:

    1. Create a module with one PowerShell command for each task you need to accomplish using PowerShell (e.g. Get-VMwareActiveSnapshot). Create these so that they can be run and tested independently when passed vCenter credentials. Don't have these output data into the database, that should be done separately to allow for isolated execution and testing independent of their integration into your tool.

    2. Define a string table (you could use a psd1 file and then convert its contents using ConvertFrom-StringData) that provides friendly display names for the commands you have written in your module. Use these names when displaying the tasks that can be run through the dashboard, as well as when looking up the script that would be run when a given display name is selected.

    3. Write a command that can get a list of the display names for the commands that are written, so that you can display those names on the dashboard for users to choose from.

    4. Write a script/command that can invoke one or more commands in your module using a display name (which would then be used to look up the actual name of the command that would be run). This would be where I would work with SQL Server. You could push objects with individual properties into custom tables based on the command name or you could convert the objects into HTML tables for easy processing/display on the client and simplified storage in SQL Server. This can also be executed in isolation for testing purposes.

    That's just off the cuff, without thinking too hard about the problem. I hope this helps.

  • #9617

    Mohammed Sabuwala
    Participant

    Hi Poshoholic,

    Thanks for your inputs, really appreciate it.

    I followed your solution until creating a module, but does that mean that every command will be a separate psm1 file, sorry I am very new to PowerShell and will take some time to understand the terminologies.

    Regards,
    Mohammed

  • #9618

    Poshoholic
    Participant

    A module is a collection/library of PowerShell commands. You can use a bunch of separate scripts, or you can create a module which packages a bunch of scripts/commands up together. Typically I create my modules with one psm1 file which dot-sources one ps1 file per command, and I define each command in its own file. That way I can work on the module with other people while using a source control system without having to worry as much about merging changes later, because we can each work in separate files for the most part. You could put all of your commands into one psm1 file, but I find it is easier to put them in separate files.

    You can learn more about modules here:

  • #9619

    Mohammed Sabuwala
    Participant

    Thanks brother, I am starting to understand a bit now.

  • #9620

    Mohammed Sabuwala
    Participant

    Hi Poshoholic,

    Here is my first script:

    Although I intend to connect to vCenter using a master script. The master script will also connect to Database, retrieve vCenter login details (there can be multiple vcenters) i.e IP/hostname, user ID and password. Then run through the table which will have a list of powercli scripts and iteratively connect to each vcenter server and run all the scripts.

    # load the VMware module
    $VimAutoCore = "VMware.VimAutomation.Core"
    if ( (Get-PSSnapin -Name $VimAutoCore -ErrorAction SilentlyContinue) -eq $null ) {
    Write-Host "loading $VimAutoCore powershell module"
    Add-PsSnapin $VimAutoCore
    }

    #This variable connects to SQL Server Database
    $SQLConnectionString = "Server=hostname;Database=dbname;User Id=sa;Password=password;"

    #Variable to retrieve vCenter IP, Username and password
    $vCenterIP = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query "SELECT vCenterIP FROM server_table"
    $vCenterUser = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query "SELECT UserID FROM server_table"
    $vCenterPassword = Get-MOLDatabaseData -connectionString $SQLConnectionString -isSQLServer -query "SELECT Password FROM server_table"

    #Connect to vCenter Server
    Connect-VIServer -Server $vCenterIP -User $vCenterUser -Password $vCenterPassword -ErrorAction Stop -Protocol https

    # List any Snapshot older than $Age days and bigger than $SnapSize MB.

    function Get-ActiveSnapshot {

    BEGIN{}
    PROCESS{
    $Age = 1

    $Snapshot = Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-$Age)}
    foreach ($sn in $Snapshot){
    $created = $sn.Created.ToLocalTime()
    $size = "{0:N2}" -f $sn.SizeGB
    $props =@{
    'Virtual Machine'=$sn.VM;
    'Category' = "Snapshot";
    'Cluster' = $sn.VM.Host.Parent.Name;
    'Results' = "This Snapshot was created on $Created which is $size GB in Size"}
    $obj = New-Object -TypeName PSObject -Property $props
    Write-Output $obj

    }
    }
    END{}
    }
    Get-ActiveSnapshot

    Although the above code doesn't connect to vcenter server, but it can retrieve information from SQL table though.

You must be logged in to reply to this topic.