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.
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.
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.
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.
My apologies. I meant host.
That did the trick. Thanks!