-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-LabStatus.ps1
More file actions
106 lines (90 loc) · 3.78 KB
/
Get-LabStatus.ps1
File metadata and controls
106 lines (90 loc) · 3.78 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
.SYNOPSIS
Queries computers' LCM state to determine whether an existing DSC configuration has applied.
.EXAMPLE
.\Get-LabStatus.ps1 -ComputerName CONTROLLER, XENAPP
Queries the CONTROLLER and XENAPP computers' LCM state.
.EXAMPLE
.\Get-LabStatus.ps1 -ComputerName CONTROLLER, EXCHANGE -Credential (Get-Credential)
Prompts for credentials to connect to the CONTROLLER and EXCHANGE computers to query the LCM state.
.EXAMPLE
.\Get-LabStatus.ps1 -ConfigurationData .\TestLabGuide.psd1 -Credential (Get-Credential)
Prompts for credentials to connect to the computers defined in the DSC configuration document (.psd1) and query the LCM state.
#>
#requires -Version 4
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'ComputerName')]
[System.String[]]
$ComputerName,
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'ConfigurationData')]
[System.Collections.Hashtable]
[Microsoft.PowerShell.DesiredStateConfiguration.ArgumentToConfigurationDataTransformationAttribute()]
$ConfigurationData,
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'ConfigurationData')]
[System.String]
$PreferNodeProperty,
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
if ($PSCmdlet.ParameterSetName -eq 'ConfigurationData') {
$nodes = $ConfigurationData.AllNodes | Where-Object { $_.NodeName -ne '*' };
foreach ($node in $nodes) {
$nodeName = $node.NodeName;
if (($PSBoundParameters.ContainsKey('PreferNodeProperty')) -and
(-not [System.String]::IsNullOrEmpty($node[$PreferNodeProperty]))) {
$nodeName = $node[$PreferNodeProperty];
}
$ComputerName += $nodeName;
}
}
$sessions = Get-PSSession;
$activeSessions = @();
$inactiveSessions = @();
foreach ($computer in $computerName) {
$session = $sessions | Where { $_.ComputerName -eq $computer -and $_.State -eq 'Opened' } | Select-Object -First 1;
if (-not $session) {
if (-not (Test-WSMan -ComputerName $computer -ErrorAction SilentlyContinue)) {
Write-Warning -Message ("Computer '$computer' not reachable.");
$inactiveSessions += $computerName;
}
else {
$newPSSessionParams = @{
ComputerName = $computer;
Authentication = 'Default';
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$newPSSessionParams['Credential'] = $Credential;
}
Write-Verbose -Message ("Connecting to '$computer'.");
$activeSessions += New-PSSession @newPSSessionParams;
}
}
else {
Write-Verbose ("Using existing session to '$computer'.");
$activeSessions += $session
}
}
Write-Verbose -Message ("Querying active session(s) '$($activeSessions.ComputerName -join "','")'.");
$results = Invoke-Command -Session $activeSessions -ScriptBlock { Get-DscLocalConfigurationManager | Select-Object -Property LCMVersion,LCMState; };
foreach ($computer in $ComputerName) {
if ($computer -in $inactiveSessions) {
[PSCustomObject] @{
ComputerName = $inactiveSession;
LCMVersion = '';
LCMState = 'Unknown';
Completed = $false;
}
}
else {
$result = $results | Where-Object { $_.PSComputerName -eq $computer };
[PSCustomObject] @{
ComputerName = $result.PSComputerName;
LCMVersion = $result.LCMVersion;
LCMState = $result.LCMState;
Completed = $result.LCMState -eq 'Idle';
}
}
}