Skip to content

Instantly share code, notes, and snippets.

@ChrisLynchHPE
Last active December 6, 2019 17:15
Show Gist options
  • Save ChrisLynchHPE/e1522506cc2555c9d21aa4b11ee60dec to your computer and use it in GitHub Desktop.
Save ChrisLynchHPE/e1522506cc2555c9d21aa4b11ee60dec to your computer and use it in GitHub Desktop.
$ServerName = "ReplaceMe"
$Server = Get-HPOVServer -Name $ServerName
$Credential = Get-Credential
$_InputObject = $Server.PSObject.Copy()
$_uri = $_InputObject.uri + "/refreshState"
$_body = @{
refreshState = 'RefreshPending'
}
if (($_InputObject.state -ieq 'Unmanaged' -and $_InputObject.stateReason -ieq 'Unconfigured') -or $Force)
{
# Test for FQDN
if (([Regex]::Match($_InputObject.mpHostInfo.mpHostName, $FQDNRegex)).Success -or [ipaddress]::TryParse($_InputObject.mpHostInfo.mpHostName, [ref]$null))
{
"[{0}] Found valid FQDN or IP Address in mpHostName value." -f $MyInvocation.InvocationName.ToString().ToUpper() | Write-Verbose
$Hostname = $_InputObject.mpHostInfo.mpHostName
}
# First find the IPv4 Address and use if available
elseif ($IPAddress = $_InputObject.mpHostInfo.mpIpAddresses | ? { ([ipaddress]$_.address).AddressFamily.Equals([System.Net.Sockets.AddressFamily]::InterNetwork) })
{
"[{0}] Found valid IPv4 Address in mpIpAddresses collection." -f $MyInvocation.InvocationName.ToString().ToUpper() | Write-Verbose
# Only get the IPv4 Address
$Hostname = $IPAddress.address
}
# if the appliance type is Composer, use the Link Local address?
elseif (($ConnectedSessions | ? ConnectionID -eq $ApplianceConnection.ConnectionID).ApplianceType -eq 'Composer')
{
"[{0}] Synergy composer. Attempting to use IPv6 Link Local Address from mpIpAddresses collection." -f $MyInvocation.InvocationName.ToString().ToUpper() | Write-Verbose
$Hostname = ($_InputObject.mpHostInfo.mpIpAddresses | ? { $_.type -eq 'LinkLocal' -and ([ipaddress]$_.address).AddressFamily.Equals([System.Net.Sockets.AddressFamily]::InterNetworkv6) }).address
}
# try to use the IPv6 address found?
else
{
"[{0}] Attempting to use first IPv6 non-Link Local Address from mpIpAddresses collection." -f $MyInvocation.InvocationName.ToString().ToUpper() | Write-Verbose
$Hostname = ($_InputObject.mpHostInfo.mpIpAddresses | ? { $_.type -ne 'LinkLocal' -and ([ipaddress]$_.address).AddressFamily.Equals([System.Net.Sockets.AddressFamily]::InterNetworkv6) } | Select -First 1).address
}
"[{0}] Will use hostname value: {1}" -f $MyInvocation.InvocationName.ToString().ToUpper(), $Hostname | Write-Verbose
}
$_body.Add('hostname', $Hostname)
$_body.Add('username', $Credential.Username)
$_body.Add('password', [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)))
$_resp = Send-HPOVRequest -Uri $_uri -Method PUT -Body $_body -Hostname $_InputObject.ApplianceConnection
Wait-HPOVTaskComplete -InputObject $_resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment