VMware Cloud Community
robertfriend
Contributor
Contributor
Jump to solution

How to prompt for vmhost name?

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.

0 Kudos
1 Solution

Accepted Solutions
MKguy
Virtuoso
Virtuoso
Jump to solution

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.

-- http://alpacapowered.wordpress.com

View solution in original post

0 Kudos
4 Replies
jpsider
Expert
Expert
Jump to solution

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.

0 Kudos
MKguy
Virtuoso
Virtuoso
Jump to solution

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.

-- http://alpacapowered.wordpress.com
0 Kudos
robertfriend
Contributor
Contributor
Jump to solution

My apologies. I meant host.

0 Kudos
robertfriend
Contributor
Contributor
Jump to solution

That did the trick. Thanks!

0 Kudos