摘要PowerShell中的文件类型PS1 VS上下文PSM1与隐性远程
我目前集中PowerShell脚本。大量用户可以通过隐式远程访问这些脚本。
问:
它是更有效地使用PSM1在PS1?鉴于从下面的设置和脚本的结构?
例脚本来自动隐式远程处理:(请注意导入的模块是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
}
实施例...请注意,每个PS1脚本具有结构化约15〜20层的功能类似以下内容:
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
}
据我所知,这个设置可以在那里我强迫用户使用的.ps1脚本每次导入脚本,他们建立的会话是资源密集的。
如果我决定使用.psm1文件扩展名(将它们转换为模块),有没有办法让这些模块永久导入,以便我可以从启动PS会话的脚本中删除以下命令?
Invoke-command -ScriptBlock { Import-Module -Name 'ModuleA.ps1' } -Session $poshSession
您是否在寻找[在端点上启动脚本?](https://jamesone111.wordpress.com/2016/06/29/just-enough-admin-and-constrained-endpoints-part- 1-understanding-endpoints /) – BenH
那么我在寻找的是看看使用psm1比ps1更有效。您发给我的链接非常好,我实际上正在使用该设置,只要psconfig文件即可。 –