Automation

 View Only
  • 1.  How to prompt for vmhost name?

    Posted Jul 28, 2016 03:01 PM

    I would like to re-purpose the script below (borrowed from Find ESXi installation date » VCDX56) and use it as a function and prompt me for a host name when I invoke it. Currently the script will run against all VMs in all connected vCenters. I would like for it to prompt me to run against a specific host. How can I accomplish this?

    Function Get-InstallDate {

    Get-VMHost | Sort Name | %{

      $thisUUID = (Get-EsxCli -VMHost $_.name).system.uuid.get()

      $decDate = [Convert]::ToInt32($thisUUID.Split("-")[0], 16)

      $installDate = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($decDate))

      [pscustomobject][ordered]@{

        Name="$($_.name)"

        InstallDate=$installDate

      } # end custom object

    } # end host loop

    }

    Any help is appreciated.



  • 2.  RE: How to prompt for vmhost name?

    Posted Jul 28, 2016 03:13 PM

    I am unclear if you want to specify this for "host" or "vm"

    So I will leave a generic line here for you:

    $userinput = Read-Host -Prompt 'Input your server  name'

    write-host "The user input was '$userinput'"


    This will prompt you to type something in, then echo it back out. Instead of the echo, you can use that variable in your script.



  • 3.  RE: How to prompt for vmhost name?

    Posted Jul 28, 2016 03:21 PM

    My apologies. I meant host.



  • 4.  RE: How to prompt for vmhost name?
    Best Answer

    Posted Jul 28, 2016 03:21 PM

    Add Powershell parameters in the function like this:

    Function Get-InstallDate {

      Param(

        [string][parameter(Mandatory=$true)] $VMHost

      )

      $thisUUID = (Get-VMHost $VMHost | Get-EsxCli).system.uuid.get()

      $decDate = [Convert]::ToInt32($thisUUID.Split("-")[0], 16)

      $installDate = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($decDate))

      [pscustomobject][ordered]@{

        Name="$((Get-VMHost $VMHost).Name)"

        InstallDate=$installDate

      }

    }

    Just running Get-InstallDate will prompt you for the hostname, or you can supply the parameter directly in the command with Get-InstallDate -VMHost myesxi.local.



  • 5.  RE: How to prompt for vmhost name?

    Posted Jul 28, 2016 03:24 PM

    That did the trick. Thanks!