1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<#
.SYNOPSIS
Validates credentials by doing a login to the target host via the VI API.
.EXAMPLE
.\Test-VMHostAccountPassword.ps1 -VMHost "host1","host2","host3"
Get prompted for all credential information so it is not on the command line.
.EXAMPLE
"host1","host2","host3" | .\Test-VMHostAccountPassword.ps1
Use pipelining.
#>
PARAM
(
[parameter(Mandatory=$true,
ValueFromPipeline=$true,
HelpMessage="An array of VM host names.")]
[String[]]$VMHost,
[parameter(Mandatory=$false,
HelpMessage="The credential used to connect to the host.")]
[System.Management.Automation.PSCredential]$credential
)
BEGIN
{
# Provide param prompting. This is a workaround for the multiple prompting that
# happens if this were done in the PARAM block.
if (!$credential)
{
$myCredential = Get-Credential
}
else
{
$myCredential = $credential
}
}
PROCESS
{
foreach ($h in $VMHost)
{
$connection = Connect-VIServer -Server $h -Credential $myCredential -ErrorAction SilentlyContinue
$success = $false
if ($connection)
{
$success = $true
Disconnect-VIServer -Server $connection -Confirm:$false | Out-Null
}
"" | Select-Object @{Name="VMHost"; Expression={$h}}, @{Name="LoginSuccess"; Expression={$success}}
}
}
|