2017-12-22 08:10:37 +01:00
|
|
|
function Test-CSConnection {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Returns the connection state of a given type to the remote host.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Use this script to check a connection which was established with the `Connect-To` cmdlet.
|
|
|
|
|
|
|
|
.PARAMETER RemoteHost
|
|
|
|
Define the remote host you would like to check.
|
|
|
|
|
|
|
|
.Parameter Type
|
|
|
|
Define the connection type you would like to check. See the `Connect-To` documentation
|
|
|
|
for valid type values.
|
|
|
|
|
|
|
|
.INPUTS
|
|
|
|
[None]
|
|
|
|
|
|
|
|
.OUTPUTS
|
2019-04-29 16:05:43 +02:00
|
|
|
[bool]
|
2017-12-22 08:10:37 +01:00
|
|
|
|
|
|
|
.EXAMPLE
|
2019-04-29 16:05:43 +02:00
|
|
|
Test-CMConnection -RemoteHost "vcr01.internal.net" -Type VMware
|
2017-12-22 08:10:37 +01:00
|
|
|
#>
|
|
|
|
|
|
|
|
[CmdletBinding()]
|
2019-04-29 16:05:43 +02:00
|
|
|
[OutputType([bool])]
|
2022-06-28 08:56:33 +02:00
|
|
|
param (
|
2017-12-22 08:10:37 +01:00
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$RemoteHost,
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $true)]
|
2017-12-22 08:10:37 +01:00
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[ValidateSet("CiscoUcs", "FTP", "NetAppFAS", "VMware")]
|
|
|
|
[string]$Type
|
|
|
|
)
|
|
|
|
|
|
|
|
switch ($Type) {
|
|
|
|
'VMware' {
|
|
|
|
try {
|
|
|
|
$Conn = Get-Variable -Name DefaultVIServer -Scope Global -ErrorAction Stop
|
|
|
|
}
|
|
|
|
catch [System.Management.Automation.ItemNotFoundException] {
|
|
|
|
$MsgParams = @{
|
|
|
|
Message = "There is no open PowerCLI VMware connection bound to 'DefaultVIServer'."
|
|
|
|
}
|
|
|
|
Write-Verbose @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
if ($Conn.Value.Name -eq $RemoteHost) {
|
|
|
|
if ($Conn.Value.IsConnected) {
|
|
|
|
$MsgParams = @{
|
|
|
|
Message = "'DefaultVIServer' found. Connection to given remote host already established."
|
|
|
|
}
|
|
|
|
Write-Verbose @MsgParams
|
|
|
|
return $True
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$MsgParams = @{
|
|
|
|
Message = "'DefaultVIServer' found. RemoteHost matches but the connection is closed."
|
|
|
|
}
|
|
|
|
Write-Verbose @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
'CiscoUcs' {
|
|
|
|
$MsgParams = @{
|
|
|
|
ErrorAction = "Stop"
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "CiscoUCS connection test is not implemented yet!"
|
2017-12-22 08:10:37 +01:00
|
|
|
}
|
|
|
|
Write-Error @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
|
|
|
|
'FTP' {
|
|
|
|
$MsgParams = @{
|
|
|
|
ErrorAction = "Stop"
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "FTP connection test is not implemented yet!"
|
2017-12-22 08:10:37 +01:00
|
|
|
}
|
|
|
|
Write-Error @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
|
|
|
|
'NetAppFAS' {
|
|
|
|
$MsgParams = @{
|
|
|
|
ErrorAction = "Stop"
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "NetAppFAS connection test is not implemented yet!"
|
2017-12-22 08:10:37 +01:00
|
|
|
}
|
|
|
|
Write-Error @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
|
|
|
|
# The Default section will never be shown as long as the powershell framework isn't broken.
|
|
|
|
Default {
|
|
|
|
$MsgParams = @{
|
|
|
|
ErrorAction = "Stop"
|
2019-04-29 16:05:43 +02:00
|
|
|
Message = "Panic: There is an invalid type value! This error should never be thrown."
|
2017-12-22 08:10:37 +01:00
|
|
|
}
|
|
|
|
Write-Error @MsgParams
|
|
|
|
return $false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|