【发布时间】:2017-07-05 17:18:18
【问题描述】:
总结
我目前正在集中化 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
【问题讨论】:
-
嗯,我正在寻找的是看看使用 psm1 是否比 ps1 更有效。你发给我的那个链接真的很好,就 psconfig 文件而言,我实际上正在使用该设置。
标签: powershell