1

Summary

I am currently centralizing powershell scripts. Where a significant amount of users can access these scripts with implicit remoting.

Question:

Is it more efficient to use psm1 over ps1? Given the structure of the setup and scripts from below?

Example script to automate implicit remoting: (Please observe the imported modules are ps1)

$poshSession = New-PSSession -ComputerName serverA -Authentication Kerberos -ConfigurationName poshconfig
Set-Alias -Name rs -Value Resolve-RemotingSession -Description 'Resolves and imports sessions and obtains specific commands'
function Resolve-RemotingSession
{ 
  # Import Modules
  Invoke-command -ScriptBlock { Import-Module -Name 'ModuleA.ps1' } -Session $poshSession
  Invoke-command -ScriptBlock { Import-Module -Name 'ModuleB.ps1' } -Session $poshSession
  Invoke-command -ScriptBlock { Import-Module -Name 'ModuleC.ps1' } -Session $poshSession
  Invoke-Command -ScriptBlock { Import-Module -Name 'ModuleD.ps1' } -Session $poshSession
  Import-PSSession -Session $poshSession -commandname *CommandA,CommandB,CommandC,CommandD* -AllowClobber
}

Example of one of the Imported Scripts... Please note that each ps1 script has about 15 to 20 functions structured similar to the following:

function Get-FooBarA{
param(
[switch]$Search,
[string]$Term
)
$foobarResults = Invoke-Restmethod -method Get -Uri www.fooA.com/$Search/$term
$foobaresults.Something
}

function Get-FooBarB{
param(
[switch]$Search,
[string]$Term
)
$foobarResults = Invoke-Restmethod -method Get -Uri www.fooB.com/$Search/$term
$foobaresults.Something
}

To my knowledge this setup can be resource intensive where I am forcing the user to import the script each time they setup the session by using a .ps1 script.

If I decide to user a .psm1 file extension (convert these to modules) is there a way to keep these modules persistently imported so that I can remove the following command from the script that initiates the PS session?

Invoke-command -ScriptBlock { Import-Module -Name 'ModuleA.ps1' } -Session $poshSession
Alexander Sinno
  • 554
  • 5
  • 24
  • Are you looking for a [Start up script on the endpoint?](https://jamesone111.wordpress.com/2016/06/29/just-enough-admin-and-constrained-endpoints-part-1-understanding-endpoints/) – BenH Feb 16 '17 at 14:35
  • Well what I am looking for is to see if using psm1 is more efficient over ps1. That link you sent me is really nice, I actually am using that setup currently as far as the psconfig file goes. – Alexander Sinno Feb 16 '17 at 14:36

1 Answers1

1

I'm not sure what you mean by more efficient but let me point out a few things:

  1. Use well-formed modules, that is create a proper directory structure for your modules, use a .psm1 module file and a .psd1 module manifest.
  2. Ensure modules are located in a path that's in the module path (see above link). That way can be imported by name without full path info.
  3. When you use Import-PSSession, instead of importing the individual commands, you can use -Module ModuleA,ModuleB to import only the commands exported by the specified modules (you would leave off -command completely).
  4. You may run many commands in a single call to Invoke-Command. It takes a scriptblock; you could put an entire program in there so there's no reason to make multiple separate calls to import multiple modules.
  5. But further, Import-Module can import multiple modules: Import-Module -Name ModuleA,ModuleB,ModuleC
  6. Using a session configuration will allow you to specify modules that get automatically loaded, so that you can remote directly into a pre-configured session.
Community
  • 1
  • 1
briantist
  • 45,546
  • 6
  • 82
  • 127
  • Thanks Brian is is what I was looking for. I was asking about ps1 vs psm1 because I understand that ps1 can be more resource taxing because of dot sourcing. I'll post back with my results in a while. – Alexander Sinno Feb 16 '17 at 14:51