lib/TMD.Registration.ps1

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

Function Register-TMDPSSessionConfiguration {
    <#
    .SYNOPSIS
        This function creates and configures a 'TMD' PSSessionConfiguration using the
        desired default settings to use the TMD client
     
    .NOTES
        Name: Register-TMDPSSessionConfiguration
        Author: TransitionManager
        Version: 1.0
        DateCreated: 2021-04-05
     
     
    .EXAMPLE
        Register-TMDPSSessionConfiguration
     
     
    .LINK
        https://support.transitionmanager.net #>

     
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0
        )]
        [String]  $Name = 'TMD'
    )
     
    BEGIN {

        
        ## Define the Session Configuration Settings
        $SessionConfiguration = @{
            
            # Version number of the schema used for this document
            SchemaVersion   = '2.0.0.0'
                
            # ID used to uniquely identify this document
            GUID            = '3a310522-717a-4982-97e2-0c2cda1deaf8'
                
            # Author of this document
            Author          = 'TransitionManager'
            
            # Description of the functionality provided by these settings
            Description     = 'TMD Powershell endpoint'
                
            # Session type defaults to apply for this session configuration. Can be 'RestrictedRemoteServer' (recommended), 'Empty', or 'Default'
            SessionType     = 'Default'
            LanguageMode    = 'FullLanguage'
            ExecutionPolicy = 'Unrestricted'
            
            ModulesToImport = @{
                'ModuleName'    = 'TMD.Common'
                'ModuleVersion' = '1.0.0.0' 
            }
        }
            
        ## Write the Session Configuration File
        $PsscFilePath = Join-Path $ENV:TEMP 'TMDSessionConfig.pssc'
        New-PSSessionConfigurationFile @SessionConfiguration -Path $PsscFilePath
    }
     
    PROCESS {

        ## Enabling PSRemoting for Version 6.
        if ((Get-PSSessionConfiguration).Count -eq 0) {
            Write-Verbose "Enabling PS Remoting"
            Enable-PSRemoting -Force -SkipNetworkProfileCheck -Confirm:$False
        }

        ## Unregister the Existing TMD Session
        Unregister-TMDPSSessionConfiguration -Name $Name
        
        ## Create Session Transport Option Defaults
        $CreateSessionTransportOptions = @{
            MaxIdleTimeoutSec   = 60
            IdleTimeoutSec      = 60
            OutputBufferingMode = 'Drop'
        }
        $SessionTransportOptions = New-PSTransportOption @CreateSessionTransportOptions

        ## Register a PowerShell Core handler (TMD)
        $SessionConfigSettings = @{
            Name             = $Name
            Path             = $PsscFilePath
            AccessMode       = 'Remote'
            Force            = $true
            TransportOption  = $SessionTransportOptions
            UseSharedProcess = $false
        }
        Register-PSSessionConfiguration @SessionConfigSettings | Out-Null

        ## Add User's account to the allowed access list
        Add-PoShEndpointAccess -EndpointName $Name -SamAccountName $env:username

    }
     
    END {

        ## Remove the Temporary Pssc File
        Remove-Item $PsscFilePath -Force | Out-Null

    }
}


Function Unregister-TMDPSSessionConfiguration {
    <#
    .SYNOPSIS
        This function Removes a 'TMD' PSSessionConfiguration
     
    .NOTES
        Name: Unregister-TMDPSSessionConfiguration
        Author: TransitionManager
        Version: 1.0
        DateCreated: 2021-04-05
     
     
    .EXAMPLE
        Unregister-TMDPSSessionConfiguration
     
     
    .LINK
        https://support.transitionmanager.net
    #>

     
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0
        )]
        [String]  $Name = 'TMD'
    )
     
    BEGIN {
        ## Remove an existing TMD Session Endpoints
        Get-PSSessionConfiguration $Name -ErrorAction SilentlyContinue | Unregister-PSSessionConfiguration
        Start-Sleep -Seconds 1
    }
    END {

    }
}


Function Test-TMDPSSessionConfiguration {
    <#
    .SYNOPSIS
        Test the existence and configuration of the
        TMD PsSession Configuration
     
    .NOTES
        Name: Test-TMDPSSessionConfiguration
        Author: TransitionManager
        Version: 1.0
        DateCreated: 2021-04-05
     
     
    .EXAMPLE
        Test-TMDPSSessionConfiguration
     
     
    .LINK
        https://support.transitionmanager.net
    #>

     
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0
        )]
        [String]  $Name = 'TMD'
    )

    Begin {

    }

    Process {

        ## Check Windows Configuration
        if ($IsWindows) {

            ## Define a Variable Stating if this user is an admin or not
            $IsAdmin = Test-IsAdmin

            ## Check to see if WinRM is running
            $WinRMServiceStatus = Get-Service -Name 'WinRM' | Select-Object -ExpandProperty 'Status'

            if($WinRMServiceStatus -ne 'Running') {
    
                ## If The user is an admin, start the WinRM Service
                if($IsAdmin) { 

                    ## Configure WinRM, which will enable it and start it. Or just start it.
                    Enable-PSRemoting -Force -SkipNetworkProfileCheck -Confirm:$False -Verbose

                }
            }
    

            ##
            ## Check the user's registry key to ensure they have a configuration
            ##
            
            Write-Host 'PSSessionManager||Status||Checking TMD User Configuration'
            $UserRegKey = Get-ItemProperty -Path "HKCU:\Software\TransitionManager" -ErrorAction SilentlyContinue
            
            ## Ensure the Key exists
            if (-Not $UserRegKey) {
                
                ## Create the Registry Key first
                New-Item -Path 'HKCU:\Software' -Name 'TransitionManager' | Out-Null
                
                ## Create a user default TMD_Files folder path
                $TMDFilesFolderPath = Join-Path $ENV:USERPROFILE 'TMD_Files'
                
                ## Create the Registry Settings for TMD
                $RegValues = @{
                    Company                  = 'TransitionManager'
                    TmdPsRoot                = 'C:\Program Files\TransitionManager\tmconsole\resources\app\integrations\PowerShell'
                    SessionManagerInstanceId = '68cc4602-eefd-43bf-8547-790cf8ae7a84'
                    UserFilesRoot            = $TMDFilesFolderPath 
                    DebugFolderPath          = Join-Path $TMDFilesFolderPath 'debug'
                }
                
                $RegValues.Keys | ForEach-Object {
                    Set-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name $_ -Value $RegValues[$_] 
                }
                
            } else {
                
                $TMDFilesFolderPath = Get-ItemPropertyValue -Path "HKCU:\Software\TransitionManager" -Name UserFilesRoot
                
            }
          
            ##
            ## Check for the required File Paths
            ##
            
            ## Create appropriate file paths for the TMD_Files folder (Per user, defaults to the user's environment home folder)
            $TMDFilesFolders = @(
                'Input',
                'Output',
                'Credentials',
                'Debug',
                'Queue',
                'Reference Designs'
                'Config'
            )
                
            ## Create each folder
            foreach ($Folder in $TMDFilesFolders) {
                Test-FolderPath -FolderPath (Join-Path $TMDFilesFolderPath $Folder)
            }

                
            ##
            ## Check for the TMD PowerShell Session Endpoint
            ##
            ## Only an Administrator can check on the TMD Endpoint Configuration
            if ($IsAdmin) {
                    
                try {
                        
                    ## Get the TMD SessionConfiguration
                    Write-Host 'PSSessionManager||Status||Checking TMD PowerShell Endpoint'
                    $TMDEndpoint = Get-PSSessionConfiguration -Name 'TMD' -ErrorAction SilentlyContinue
                    if (-Not $TMDEndpoint) {
                            
                        ## Run the TMD PSSessionConfiguration Registration
                        Write-Host 'PSSessionManager||Status||Registering TMD PowerShell Endpoint'
                        Register-TMDPSSessionConfiguration
                        Write-Host 'PSSessionManager||Status||Registration Complete'
                        
                    }

                } catch {
                    Throw $_
                }
            }
        }

        ## Check Mac OS Configuration
        if ($IsMacOS) {

            if ($OutputVerbose) {

                Write-Host "Checking Mac Configuration"
            }
        }
    }

    End {

    }

}

function Add-PoShEndpointAccess {
    <#
    .Synopsis
       Adds a group or user to a PowerShell (WinRM) endpoint to allow remote management.

    .DESCRIPTION
       This function will edit the SDDL of a PowerShell (WinRM) endpoint to
       allow remote management for the specified account/group.

       If you run this against a remote computer, CredSSP needs to be enabled and you need
       to restart the WinRM-service manually afterwards (this function uses WinRM to connect
       to the remote machine, which is why it will not restart the service itself).

    .PARAMETER SamAccountName
       The SamAccount name of the user or group that you want to give access to. Could also be in the form
       domain\SamAccountName, for example contoso\Administrator.

    .PARAMETER ComputerName
       Specifies the computer on which the command runs. The default is the local computer.

    .PARAMETER EndpointName
       Specifies then name of the WinRM endpoint you want to configure, the default is Microsoft.PowerShell.

    .EXAMPLE
       Add-PoShEndpointAccess -SamAccountName "contoso\PoShUsers" -ComputerName MyPoShEndpoint.contoso.com

    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipelineByPropertyName = $true)]
        $SamAccountName,

        [Parameter(Mandatory = $false)]
        $ComputerName = '.',

        [Parameter(Mandatory = $false)]
        $EndpointName = 'Microsoft.PowerShell'
    )

    Begin { }

    Process {
        if ($ComputerName -eq '.' -OR $ComputerName -eq "$($env:COMPUTERNAME)") {
            $IdentityObject = New-Object Security.Principal.NTAccount $SamAccountName
            try {
                $sid = $IdentityObject.Translate([Security.Principal.SecurityIdentifier]).Value
            } catch {
                throw "Failed to translate $SamAccountName to a valid SID."
            }

            try {
                $PSSConfig = Get-PSSessionConfiguration -Name $EndpointName -ErrorAction Stop
            } catch {
                if ($_.Tostring() -like '*access is denied*') {
                    throw 'You need to have Admin-access to run this command!'
                }
            }

            $existingSDDL = $PSSConfig.SecurityDescriptorSDDL
            $isContainer = $false
            $isDS = $false

            $SecurityDescriptor = New-Object -TypeName Security.AccessControl.CommonSecurityDescriptor -ArgumentList $isContainer, $isDS, $existingSDDL
            $accessType = 'Allow'
            $accessMask = 268435456
            $inheritanceFlags = 'none'
            $propagationFlags = 'none'
            $SecurityDescriptor.DiscretionaryAcl.AddAccess($accessType, $sid, $accessMask, $inheritanceFlags, $propagationFlags)

            $null = Set-PSSessionConfiguration -Name $EndpointName -SecurityDescriptorSddl ($SecurityDescriptor.GetSddlForm('All')) -Confirm:$false -Force

        } else {
            Invoke-Command -ArgumentList $SamAccountName, $EndpointName -ScriptBlock {
                $IdentityObject = New-Object Security.Principal.NTAccount $args[0]
                $EndpointName = $args[1]

                try {
                    $sid = $IdentityObject.Translate([Security.Principal.SecurityIdentifier]).Value
                } catch {
                    throw "Failed to translate $($args[0]) to a valid SID."
                }

                try {
                    $PSSConfig = Get-PSSessionConfiguration -Name $EndpointName -ErrorAction Stop
                } catch {
                    if ($_.Tostring() -like '*access is denied*') {
                        throw 'You need to have Admin-access and enable CredSSP to run this command remotely!'
                    }
                }

                $existingSDDL = $PSSConfig.SecurityDescriptorSDDL
                $isContainer = $false
                $isDS = $false

                $SecurityDescriptor = New-Object -TypeName Security.AccessControl.CommonSecurityDescriptor -ArgumentList $isContainer, $isDS, $existingSDDL
                $accessType = 'Allow'
                $accessMask = 268435456
                $inheritanceFlags = 'none'
                $propagationFlags = 'none'
                $SecurityDescriptor.DiscretionaryAcl.AddAccess($accessType, $sid, $accessMask, $inheritanceFlags, $propagationFlags)

                $null = Set-PSSessionConfiguration -Name $EndpointName -SecurityDescriptorSddl ($SecurityDescriptor.GetSddlForm('All')) -Confirm:$false -Force -NoServiceRestart

            } -ComputerName $ComputerName
        }
    }

    End { }
}