PowerShell 远程配置文件。

12

当我在本地计算机上使用Enter-PSSession打开远程PowerShell会话时,如何在我的配置文件中使用函数?


有没有完整源代码的最终解决方案? - Kiquenet
5个回答

32

由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

此配置


此外,只有 $pshome 中的机器级别配置文件被加载。这种情况在某些时候有改变吗?在 Windows 8.1 和 Windows Server 2012 中,Enter-PSSession 不会在目标计算机上运行任何 Powershell 配置文件。 - Greenstone Walker
@GreenstoneWalker - 我也遇到了这个问题。使用 Win 2008 R2 / PS 3.0 服务器。 - smaglio81
1
如果您不想依赖于特定的Windows版本或语言,请使用:[Environment] :: GetFolderPath('MyDocuments') - Mark Toman

7

Jason, 在我的情况下,我希望当我远程连接到另一台计算机时,我的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 参数,以使用您所喜欢的任何文件。

1
这个可以工作,但是PowerShell有一些错误会导致它无法正常工作。首先,“Enter-PSSession -Session”没有等待的方式,这意味着一旦调用此语句,您就在同一个窗口中生成了一个新进程,而脚本中没有等待锁定,直到用户键入退出。Invoke-command -session并行运行而不是串行运行...所以这是竞争条件...最好使用其他方法来完成这个任务,永远不要使用“New-Session”...如果Microsoft有一种模式可以确保每个调用命令都是串行执行的... - pico
@pico,这不是用于脚本的。它应该模拟在Linux主机上使用ssh。另外,Invoke-Command是同步运行的,不是竞争条件。请注意,我仅使用New-Session一次,然后在同一个对象上使用Invoke和Enter。您需要多个会话才能实现并发。 j.ps1 内容: $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

1

0

不行。使用enter-pssession启动远程交互会话时,会加载远程配置文件。此外,只有$pshome中的机器级别配置文件被加载。如果您想要远程函数可用,您需要在远程会话配置的启动脚本中初始化它们。请查看远程服务器上的get/set-pssessionconfiguration。


这些事情的处理方式有所改变吗?我使用powershell 5.1,在C:\Windows\System32\WindowsPowerShell\v1.0\中创建了一个profile.ps1文件,只是为了设置一个别名。当在本地使用shell时,它可以正常工作。但是当使用Enter-PSSession连接到该计算机时,该别名无法正常工作... - Alex
@Alex 或许你需要重新启动服务器端的WinRM服务?重新启动即可,但如果这有点过重,可以使用“restart-service winrm”。或者,事情可能已经发生了变化,就像你所说的一样... - x0n

0

使用远程会话中配置文件的另一种方法:

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

Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\计算机名\C$\Users\MyAccountName\Documents

  1. 进入PSSession

Enter-PSSession -ComputerName 计算机名

  1. 点源你的配置文件

. .\Profile.ps1

这种解决方案的缺点:

  • 你必须复制你的配置文件到你想要拥有配置文件的所有计算机上
  • 每次进入PSSession都必须点源配置文件

这种解决方案的优点:

  • 不需要函数,你只需像往常一样进入PSSession
  • 通过更改默认会话配置,不会改变所有用户的配置文件

(感谢{{link1:Jeffery Hicks提供有关点源的提示))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接