当我在本地计算机上使用Enter-PSSession
打开远程PowerShell会话时,如何在我的配置文件中使用函数?
当我在本地计算机上使用Enter-PSSession
打开远程PowerShell会话时,如何在我的配置文件中使用函数?
由JonZ和x0n撰写:
当您使用默认会话配置的 PSSessions 时,不会运行任何配置文件脚本。
当使用
Enter-PSSession
启动远程交互会话时,会加载远程配置文件。此外,仅会加载位于$pshome
中的机器级配置文件。
如果您希望会话预配置(加载自定义函数、插件、模块等),请将配置文件脚本添加到新的sessionconfiguration中(以便在远程会话配置的启动脚本中初始化它们)。
Register-PSSessionConfiguration cmdlet 在本地计算机上创建并注册新的会话配置。使用Get-PSSessionConfiguration查看现有的会话配置。获取-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
是您注册pssessionconfiguration的远程服务器的主机名)。
有关PowerShell远程的良好资源是管理员指南:Powershell远程。
参考文献:
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://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
六个不同的Windows PowerShell配置文件路径和用途
当前用户,当前主机 - 控制台
$Home[我的]文档\WindowsPowerShell\Profile.ps1
当前用户,所有主机
$Home[我的]文档\Profile.ps1
所有用户,当前主机 - 控制台
$PsHome\Microsoft.PowerShell_profile.ps1
所有用户,所有主机
$PsHome\Profile.ps1
当前用户,当前主机 - ISE
$Home[我的]文档\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 Core配置文件 https://learn.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
此配置
[Environment] :: GetFolderPath('MyDocuments')
。 - Mark TomanJason, 在我的情况下,我希望当我远程连接到另一台计算机时,我的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
}
$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}
结果表明串行。 - Devon Dieffenbach看一下这个
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 \\计算机名\C$\Users\MyAccountName\Documents
Enter-PSSession -ComputerName 计算机名
. .\Profile.ps1
这种解决方案的缺点:
这种解决方案的优点:
(感谢{{link1:Jeffery Hicks提供有关点源的提示))