【发布时间】:2011-02-28 10:14:31
【问题描述】:
在本地计算机上使用Enter-PSSession 打开远程 PowerShell 会话时,如何在远程计算机上使用我的配置文件中的函数。
【问题讨论】:
-
有完整源代码的最终解决方案吗?
标签: powershell powershell-remoting
在本地计算机上使用Enter-PSSession 打开远程 PowerShell 会话时,如何在远程计算机上使用我的配置文件中的函数。
【问题讨论】:
标签: powershell powershell-remoting
JonZ 和 x0n:
当您将 pssession 与默认会话配置一起使用时,不会运行任何配置文件脚本。
使用
Enter-PSSession启动远程交互会话时,会加载远程配置文件。此外,仅加载了$pshome中的机器级配置文件。
如果您希望预先配置会话(以加载自定义函数、管理单元、模块等),请将配置文件脚本添加到新的 会话配置(用于在启动脚本中初始化它们远程会话配置)。
Register-PSSessionConfiguration cmdlet 在本地计算机上创建并注册新的会话配置。使用Get-PSSessionConfiguration 查看现有会话配置。 Get-PSSessionConfiguration 和 Register-PSSessionConfiguration 都需要提升权限(使用“以管理员身份运行”选项启动 PowerShell)。
在目标计算机中,profile.ps1 包含您的所有功能:
Register-PSSessionConfiguration -Name WithProfile -StartupScript $PsHome\Profile.ps1
要使用此预配置会话,您需要从本地计算机键入:
Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile
或
Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile -Credential youradminuser@yourtargetdomain
(其中$computername 是您注册 pssession 配置的远程服务器的主机名)。
关于 PowerShell 远程处理的一个很好的来源是 Administrator's Guide to Powershell Remoting。
参考资料:
Powershell Remoting: Use Functions loaded in Powershell remote Profile?
http://jrich523.wordpress.com/2010/07/21/update-creating-a-profile-for-a-remote-session/
了解和使用 PowerShell 配置文件
http://blogs.technet.com/b/heyscriptingguy/archive/2013/01/04/understanding-and-using-powershell-profiles.aspx
About_Profiles (Microsoft Docs) https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
六种不同的 Windows PowerShell 配置文件路径和用途
当前用户,当前主机 - 控制台
$Home[我的]Documents\WindowsPowerShell\Profile.ps1
当前用户,所有主机
$Home[我的]Documents\Profile.ps1
所有用户,当前主机 - 控制台
$PsHome\Microsoft.PowerShell_profile.ps1
所有用户,所有主机
$PsHome\Profile.ps1
当前用户,当前主机 - ISE
$Home[我的]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
所有用户,当前主机 - ISE
$PsHome\Microsoft.PowerShellISE_profile.ps1
Windows PowerShell 配置文件
http://msdn.microsoft.com/en-us/library/bb613488%28VS.85%29.aspx
此配置文件适用于所有用户和所有 shell。
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
此配置文件适用于所有用户,但仅适用于 Microsoft.PowerShell shell。
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
此配置文件仅适用于当前用户,但会影响所有 shell。
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
此配置文件仅适用于当前用户和 Microsoft.PowerShell shell。
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PowerShell 核心配置文件 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
此配置文件适用于所有用户和所有主机。
$env:ProgramFiles\PowerShell\6\profile.ps1
此配置文件适用于所有用户,但仅适用于当前主机。
$env:ProgramFiles\PowerShell\6\Microsoft.PowerShell_profile.ps1
此配置文件仅适用于当前用户,但会影响所有主机。
$env:USERPROFILE\Documents\PowerShell\profile.ps1
此配置文件仅适用于当前用户和 Microsoft.PowerShell shell。
$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
【讨论】:
[Environment]::GetFolderPath('MyDocuments')
杰森, 就我而言,当我远程连接到另一台计算机时,我想让我的 Powershell 配置文件跟随我。
我创建了一个包装函数 Remote,它接受一个计算机名,创建一个会话,将您的配置文件加载到会话中,并使用 enter-pssession。
下面是代码:
function Remote($computername){
if(!$Global:credential){
$Global:credential = Get-Credential
}
$session = New-PSSession -ComputerName $computername -Credential $credential
Invoke-Command -FilePath $profile -Session $session
Enter-PSSession -Session $session
}
您可以修改 Invoke-Command -FilePath 参数以获取您喜欢的任何文件。
【讨论】:
$id = (New-Guid).Guid; $sleep = Get-Random -Min 1 -Max 10; "Job $id sleep-time: $sleep"0..5 | % {"$id : $_";sleep $sleep}测试脚本$session = New-PSSession;0..10 | %{ Invoke-Command -FilePath "j.ps1" -Session $session}结果为序列号。
看看这个
http://jrich523.wordpress.com/2010/07/08/creating-a-profile-for-a-remote-session/
它是创建远程配置文件的一种解决方法。
【讨论】:
你不能。使用 enter-pssession 启动远程交互会话时,会加载远程配置文件。此外,仅加载 $pshome 中的机器级配置文件。如果您希望远程功能可用,您必须在远程会话配置的启动脚本中初始化它们。查看远程服务器上的 get/set-pssessionconfiguration。
【讨论】:
还有另一种在远程会话中使用配置文件的方法:
Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents
Enter-PSSession -ComputerName ComputerName
. .\Profile.ps1
此解决方案的缺点:
此解决方案的优点:
【讨论】: