Powershell 远程处理配置文件
在本地计算机上使用 Enter-PSSession
打开远程 PowerShell 会话时,如何使用远程计算机上我的配置文件中的函数。
How do I use a function in my profile on the remote machine when using Enter-PSSession
on my local machine to open a remote PowerShell session.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
作者:JonZ 和 x0n:
如果您希望预配置会话(以加载自定义功能、管理单元、模块等),请将配置文件脚本添加到新的会话配置(用于在远程的启动脚本中初始化它们)会话配置)。
Register-PSSessionConfiguration cmdlet 在本地创建并注册新的会话配置电脑。使用 Get-PSSessionConfiguration 查看现有会话配置。 Get-PSSessionConfiguration 和 Register-PSSessionConfiguration 都需要提升权限(使用“以管理员身份运行”选项启动 PowerShell)。
在目标计算机中,其中
profile.ps1
包含您的所有功能:要使用此预配置会话,您需要从本地计算机键入:
或
(其中
$computername
是主机名您在其中注册 psessionconfiguration 的远程服务器的名称)。Powershell 远程管理员指南是有关 PowerShell 远程处理的一个很好的资料。
参考文献:
Powershell 远程处理:使用 Powershell 远程配置文件中加载的函数?
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 文档)
https://learn.microsoft.com/en -us/powershell/module/microsoft.powershell.core/about/about_profiles
六个不同的 Windows PowerShell 配置文件路径并使用
当前用户、当前主机 - 控制台
$Home[My ]Documents\WindowsPowerShell\Profile.ps1
当前用户,所有主机
$Home[My ]Documents\Profile.ps1
所有用户,当前主机 - 控制台
$PsHome\Microsoft.PowerShell_profile.ps1
所有用户,所有主机
$PsHome\Profile.ps1
当前用户,当前主机 - ISE
$Home[My ]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://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
此配置文件仅适用于当前用户,但会影响所有主机。
$env:USERPROFILE\Documents\PowerShell\profile.ps1
此配置文件仅适用于当前用户和 Microsoft.PowerShell shell。
$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
By JonZ and x0n:
If you want a session to be preconfigured (to load custom functions, snap-ins, modules, etc.), add a profile script to a new sessionconfiguration (for initialize them in the startup script of the remote session configuration).
The Register-PSSessionConfiguration cmdlet creates and registers a new session configuration on the local computer. Use Get-PSSessionConfiguration to view existing session configurations. Both Get-PSSessionConfiguration and Register-PSSessionConfiguration require elevated rights (start PowerShell with the “Run as Administrator” option).
In the target computer, where
profile.ps1
contains all your functions:To use this preconfigured session you would type, from the local computer:
or
(where
$computername
is the hostname of the remote server where you registered the pssessionconfiguration).A good source on PowerShell remoting is the Administrator's Guide to Powershell Remoting.
References:
Powershell Remoting: Use Functions loaded in Powershell remote Profile?
http://jrich523.wordpress.com/2010/07/21/update-creating-a-profile-for-a-remote-session/
Understanding and Using PowerShell Profiles
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
The six different Windows PowerShell profile paths and use
Current User, Current Host - console
$Home[My ]Documents\WindowsPowerShell\Profile.ps1
Current User, All Hosts
$Home[My ]Documents\Profile.ps1
All Users, Current Host - console
$PsHome\Microsoft.PowerShell_profile.ps1
All Users, All Hosts
$PsHome\Profile.ps1
Current user, Current Host - ISE
$Home[My ]Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
All users, Current Host - ISE
$PsHome\Microsoft.PowerShellISE_profile.ps1
Windows PowerShell Profiles
http://msdn.microsoft.com/en-us/library/bb613488%28VS.85%29.aspx
This profile applies to all users and all shells.
%windir%\system32\WindowsPowerShell\v1.0\profile.ps1
This profile applies to all users, but only to the Microsoft.PowerShell shell.
%windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1
This profile applies only to the current user, but affects all shells.
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1
This profile applies only to the current user and the Microsoft.PowerShell shell.
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PowerShell Core Profiles
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
This profile applies to all users and all hosts.
$env:ProgramFiles\PowerShell\6\profile.ps1
This profile applies to all users, but only to the current host.
$env:ProgramFiles\PowerShell\6\Microsoft.PowerShell_profile.ps1
This profile applies only to the current user, but affects all hosts.
$env:USERPROFILE\Documents\PowerShell\profile.ps1
This profile applies only to the current user and the Microsoft.PowerShell shell.
$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
杰森,
就我而言,我希望当我远程连接到另一台计算机时,我的 Powershell 配置文件会跟随我。
我创建了一个包装函数 Remote,它接受计算机名、创建会话、将您的配置文件加载到会话中,并使用 Enter-pssession。
下面是代码:
您可以修改 Invoke-Command -FilePath 参数以获取您喜欢的任何文件。
Jason,
In my case, I wanted to have my Powershell Profile follow me when I remoted into another computer.
I have created a wrapper function Remote that takes a computername, creates a session, loads your profile into the session, and uses enter-pssession.
Here is the code below:
You could modify the Invoke-Command -FilePath parameter to take any file of your liking.
看看这个
http: //jrich523.wordpress.com/2010/07/08/creating-a-profile-for-a-remote-session/
它是创建远程配置文件的解决方法。
take a look at this
http://jrich523.wordpress.com/2010/07/08/creating-a-profile-for-a-remote-session/
its a work around for creating a remote profile.
你不能。当使用 Enter-pssession 启动远程交互会话时,将加载远程配置文件。此外,仅加载 $pshome 中的机器级配置文件。如果您希望远程功能可用,您必须在远程会话配置的启动脚本中初始化它们。查看远程服务器上的 get/set-pssessionconfiguration。
You can't. When starting a remote interactive session with enter-pssession, a remote profile is loaded. Additionally, only the machine-level profile in $pshome is loaded. If you want remote functions available you'll have to initialize them in the startup script of the remote session configuration. Have a look at get/set-pssessionconfiguration on the remote server.
还有另一种在远程会话上使用配置文件的方法:
Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents
Enter-PSSession -ComputerName ComputerName
。 .\Profile.ps1
此解决方案的缺点:
此解决方案的优点:
(感谢 Jeffery Hicks 提供有关点来源的提示)
There is another way to use a profile on a remote session:
Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents
Enter-PSSession -ComputerName ComputerName
. .\Profile.ps1
Drawbacks of this solution:
Advantages of this solution:
(Thanks to Jeffery Hicks for the tip about the dot sourcing)