Powershell 远程处理配置文件

发布于 2024-09-04 15:51:29 字数 87 浏览 1 评论 0原文

在本地计算机上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深空失忆 2024-09-11 15:51:29

作者:JonZ 和 x0n:

当您使用 pssessions 和默认会话配置时,不会运行配置文件脚本。

使用 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 是主机名您在其中注册 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:

When you use pssessions with the default session configurations, no profile scripts run.

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 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:

Register-PSSessionConfiguration -Name WithProfile -StartupScript $PsHome\Profile.ps1

To use this preconfigured session you would type, from the local computer:

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile

or

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile -Credential youradminuser@yourtargetdomain

(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

Spring初心 2024-09-11 15:51:29

杰森,
就我而言,我希望当我远程连接到另一台计算机时,我的 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 参数以获取您喜欢的任何文件。

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:

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
}

You could modify the Invoke-Command -FilePath parameter to take any file of your liking.

陪我终i 2024-09-11 15:51:29

看看这个

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.

水中月 2024-09-11 15:51:29

你不能。当使用 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.

秋凉 2024-09-11 15:51:29

还有另一种在远程会话上使用配置文件的方法:

  1. 将所选配置文件复制到远程服务器的文档文件夹中。例如:

Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents

  1. 输入 PSSession

Enter-PSSession -ComputerName ComputerName

  1. 点获取您的个人资料

。 .\Profile.ps1

此解决方案的缺点:

  • 您必须将您的配置文件复制到您想要拥有配置文件的所有计算机上,
  • 您必须在每次进入 PSSession 时获取配置文件

此解决方案的优点:

  • 有不需要功能,您只需像往常一样输入 PSSession
  • 您不会通过更改默认会话配置来更改所有用户的配置文件

(感谢 Jeffery Hicks 提供有关点来源的提示

There is another way to use a profile on a remote session:

  1. Copy the chosen profile to the remote server(s) in your documents folder. For example:

Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents

  1. Enter the PSSession

Enter-PSSession -ComputerName ComputerName

  1. Dot source your profile

. .\Profile.ps1

Drawbacks of this solution:

  • you must copy once your profile to all the computers where you want to have a profile
  • you must dot source the profile every time you enter a PSSession

Advantages of this solution:

  • There is no need of function and you just enter a PSSession like you always do
  • You do not alter all users' profile by changing the default session configuration

(Thanks to Jeffery Hicks for the tip about the dot sourcing)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文