|
1 |
| -function Set-NetworkLevelAuthentication |
2 |
| -{ |
3 |
| -<# |
| 1 | +function Set-NetworkLevelAuthentication { |
| 2 | + <# |
4 | 3 | .SYNOPSIS
|
5 | 4 | This function will set the NLA setting on a local machine or remote machine
|
6 | 5 |
|
|
37 | 36 | #Requires -Version 3.0
|
38 | 37 | [CmdletBinding()]
|
39 | 38 | PARAM (
|
40 |
| - [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)] |
| 39 | + [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] |
41 | 40 | [System.String[]]$ComputerName = $env:ComputerName,
|
42 | 41 |
|
43 | 42 | [Parameter(Mandatory)]
|
|
47 | 46 | [System.Management.Automation.Credential()]
|
48 | 47 | $Credential = [System.Management.Automation.PSCredential]::Empty
|
49 | 48 | )#Param
|
50 |
| - BEGIN |
51 |
| - { |
52 |
| - TRY |
53 |
| - { |
54 |
| - IF (-not (Get-Module -Name CimCmdlets)) |
55 |
| - { |
| 49 | + BEGIN { |
| 50 | + TRY { |
| 51 | + IF (-not (Get-Module -Name CimCmdlets)) { |
56 | 52 | Write-Verbose -Message '[BEGIN] Import Module CimCmdlets'
|
57 | 53 | Import-Module CimCmdlets -ErrorAction 'Stop' -ErrorVariable ErrorBeginCimCmdlets
|
58 | 54 | }
|
59 | 55 | }
|
60 |
| - CATCH |
61 |
| - { |
62 |
| - IF ($ErrorBeginCimCmdlets) |
63 |
| - { |
| 56 | + CATCH { |
| 57 | + IF ($ErrorBeginCimCmdlets) { |
64 | 58 | Write-Error -Message "[BEGIN] Can't find CimCmdlets Module"
|
65 | 59 | }
|
66 | 60 | }
|
67 | 61 | }#BEGIN
|
68 | 62 |
|
69 |
| - PROCESS |
70 |
| - { |
71 |
| - FOREACH ($Computer in $ComputerName) |
72 |
| - { |
| 63 | + PROCESS { |
| 64 | + FOREACH ($Computer in $ComputerName) { |
73 | 65 | Write-Verbose -message $Computer
|
74 |
| - TRY |
75 |
| - { |
| 66 | + TRY { |
76 | 67 | # Building Splatting for CIM Sessions
|
77 | 68 | Write-Verbose -message "$Computer - CIM/WIM - Building Splatting"
|
78 | 69 | $CIMSessionParams = @{
|
79 |
| - ComputerName = $Computer |
80 |
| - ErrorAction = 'Stop' |
| 70 | + ComputerName = $Computer |
| 71 | + ErrorAction = 'Stop' |
81 | 72 | ErrorVariable = 'ProcessError'
|
82 | 73 | }
|
83 | 74 |
|
84 | 75 | # Add Credential if specified when calling the function
|
85 |
| - IF ($PSBoundParameters['Credential']) |
86 |
| - { |
| 76 | + IF ($PSBoundParameters['Credential']) { |
87 | 77 | Write-Verbose -message "[PROCESS] $Computer - CIM/WMI - Add Credential Specified"
|
88 | 78 | $CIMSessionParams.credential = $Credential
|
89 | 79 | }
|
|
94 | 84 |
|
95 | 85 | # CIM/WMI Connection
|
96 | 86 | # WsMAN
|
97 |
| - IF ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: 3.0') |
98 |
| - { |
| 87 | + IF ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: 3.0') { |
99 | 88 | Write-Verbose -Message "[PROCESS] $Computer - WSMAN is responsive"
|
100 | 89 | $CimSession = New-CimSession @CIMSessionParams
|
101 | 90 | $CimProtocol = $CimSession.protocol
|
102 | 91 | Write-Verbose -message "[PROCESS] $Computer - [$CimProtocol] CIM SESSION - Opened"
|
103 | 92 | }
|
104 | 93 |
|
105 | 94 | # DCOM
|
106 |
| - ELSE |
107 |
| - { |
| 95 | + ELSE { |
108 | 96 | # Trying with DCOM protocol
|
109 | 97 | Write-Verbose -Message "[PROCESS] $Computer - Trying to connect via DCOM protocol"
|
110 | 98 | $CIMSessionParams.SessionOption = New-CimSessionOption -Protocol Dcom
|
|
119 | 107 | $NLAinfo | Invoke-CimMethod -MethodName SetUserAuthenticationRequired -Arguments @{ UserAuthenticationRequired = $EnableNLA } -ErrorAction 'Continue' -ErrorVariable ErrorProcessInvokeWmiMethod
|
120 | 108 | }
|
121 | 109 |
|
122 |
| - CATCH |
123 |
| - { |
| 110 | + CATCH { |
124 | 111 | Write-Warning -Message "Error on $Computer"
|
125 | 112 | Write-Error -Message $_.Exception.Message
|
126 | 113 | if ($ErrorTestConnection) { Write-Warning -Message "[PROCESS] Error - $ErrorTestConnection" }
|
127 | 114 | if ($ProcessError) { Write-Warning -Message "[PROCESS] Error - $ProcessError" }
|
128 | 115 | if ($ErrorProcessInvokeWmiMethod) { Write-Warning -Message "[PROCESS] Error - $ErrorProcessInvokeWmiMethod" }
|
129 | 116 | }#CATCH
|
130 |
| - FINALLY |
131 |
| - { |
132 |
| - if ($CimSession) |
133 |
| - { |
| 117 | + FINALLY { |
| 118 | + if ($CimSession) { |
134 | 119 | # CLeanup/Close the remaining session
|
135 | 120 | Write-Verbose -Message "[PROCESS] Finally Close any CIM Session(s)"
|
136 | 121 | Remove-CimSession -CimSession $CimSession
|
137 | 122 | }
|
138 | 123 | }
|
139 | 124 | } # FOREACH
|
140 | 125 | }#PROCESS
|
141 |
| - END |
142 |
| - { |
| 126 | + END { |
143 | 127 | Write-Verbose -Message "[END] Script is completed"
|
144 | 128 | }
|
145 | 129 | }
|
0 commit comments