Search code examples
powershellpowershell-4.0powershell-remoting

Constrained endpoint on specific user


I'm trying to create a PowerShell endpoint constraint that restricts an user to only execute the functions in a custom module I made.

The first thing I did is import-module mymodule.psm1 which allows me to run my modules fine withing my host system.

Then the following PS command creates the configuration file for the endpoint which allows the functions inside the brackets to be the only functions the user gets to execute.

New-PSSessionConfigurationFile -VisibleFunctions('Get-Command','Get-Info', 'CreateAD-User','Generate-Html','Change-Logon') -LanguageMode ‘ConstrainedLanguage’ –SessionType ‘RestrictedRemoteServer’ –Path ‘c:\test\helpdesk.pssc’

Then I register the endpoint with

Register-PSSessionConfiguration –Name ‘HelpDesk’ -ShowSecurityDescriptorUI –Path ‘c:\test\helpdesk.pssc’

and selected which user I want allow to have these constrains once the SecurityDescriptorUI pops up. Once I log into the user that I set up the constrains for with

Enter-PSSession -computername SRV1-AD -Credential $credential -ConfigurationName HelpDesk

These are the allowed cmdlets / functions that the user is allowed to execute. These are the default required cmdlets to allow remote connections into a system.

Image

How can I allow my custom module to be the only functions the endpoint allows users to execute? or How can I import my module into configuration file so it executes every time the HelpDesk end point configuration is used. I know that in the configuration file there's a line to import modules but Import-Module is not actually a module an example of a module would be ActiveDirectory, if I'm able to find what module import-module is a part of I think I should be able to do a quick and dirty work around for this.

UPDATE

A dirty solution I found for this was to enter into the user's session and disable all cmdlets / functions except the ones I want to allowed for example import-module & Get-Command with import-module I can manually import my custom module and my functions will be the only ones visible to user. But this is not a perfect solution because this means that I would need to download my module into every system I want this to take effect and it's no longer a one to many solution. The ideal solution is to have my module locally stored, enter into a session with the registered end point and have my module already imported into the users account.

Enter-PSSession -computername SRV1-AD -Credential $credential -ConfigurationName HelpDesk

Further Update

User @prasoon-karunan-v suggested I used -ScriptsToProcess & FunctionDefinitions to import the module so I used the following command

New-PSSessionConfigurationFile -VisibleFunctions('Get-Command','Get-Info', 'CreateAD-User','Generate-Html','Change-Logon') -LanguageMode ‘ConstrainedLanguage’ –SessionType ‘RestrictedRemoteServer’ –Path ‘.\EndPoint.pssc’ -ScriptsToProcess C:\Users\Administrator\Desktop\Modules\ImportM.psm1

In the configuration file I also set the functions I want to use like so

# Functions defined in this session configuration
FunctionDefinitions = 'Get-Command','Get-Info', 'CreateAD-User','Generate-Html','Change-Logon'

When I tried to establish a session it would throw the following error

Error1

Then I thought maybe it's not working because were not telling the command to import anything were just pointing to the module file, so maybe I need to create a small script that imports the module then add it the configuration file. So that's exactly what I did I created a small script with just, import-module C:\Modules\ImportM.psm1 and then I went over to the .pssc file and added this script to the ScriptsToProcess but I get the following error after I try to establish a session to the constrained endpoint.

Error

Language Mode is set to

LanguageMode = 'RestrictedLanguage'

Solution

  • use -ScriptsToProcess parameter, which can be used to import your custom module.

    See below as well.

    Get-Help New-PSSessionConfigurationFile -Parameter ScriptsToProcess

    Get-Help New-PSSessionConfigurationFile -Parameter FunctionDefinitions

    Update:

    Be sure about the language mode to use, see here