Powershell远程配置文件

gen*_*ion 7 powershell powershell-remoting

Enter-PSSession在本地计算机上使用打开远程PowerShell会话时,如何在远程计算机上的配置文件中使用该功能.

Kiq*_*net 17

通过JonZ和x0n:

将pssessions与默认会话配置一起使用时,不会运行任何配置文件脚本.

使用时启动远程交互式会话时Enter-PSSession,会加载远程配置文件.此外,仅$pshome加载机器级配置文件.

如果您想预先配置会话(加载自定义功能,管理单元,模块等),添加配置文件脚本到一个新的sessionconfiguration(在远程会话配置的启动脚本初始化它们).

注册-PSSessionConfiguration cmdlet创建并注册在本地计算机上一个新的会话配置.使用Get-PSSessionConfiguration查看现有会话配置.Get-PSSessionConfiguration和Register-PSSessionConfiguration都需要提升权限(使用"以管理员身份运行"选项启动PowerShell).

在目标计算机中,其中profile.ps1包含您的所有功能:

Register-PSSessionConfiguration -Name WithProfile -StartupScript $PsHome\Profile.ps1
Run Code Online (Sandbox Code Playgroud)

要使用此预配置会话,您可以从本地计算机键入:

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile
Run Code Online (Sandbox Code Playgroud)

要么

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile -Credential youradminuser@yourtargetdomain
Run Code Online (Sandbox Code Playgroud)

($computername您注册pssession配置的远程服务器的主机名在哪里).

PowerShell远程处理的一个很好的来源是Powershell Remoting管理员指南.

参考文献:
Powershell Remoting:使用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 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
$主页[我的] 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 \文档\ PowerShell的\ profile.ps1

此配置文件仅适用于当前用户和Microsoft.PowerShell shell.
$ ENV:USERPROFILE \文档\ PowerShell的\ Microsoft.PowerShell_profile.ps1


Dev*_*ach 5

杰森,就我而言,当我远程连接到另一台计算机时,我想让我的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
}
Run Code Online (Sandbox Code Playgroud)

您可以修改Invoke-Command -FilePath参数以获取您喜欢的任何文件.


x0n*_*x0n 0

你不能。当使用 Enter-pssession 启动远程交互会话时,将加载远程配置文件。此外,仅加载 $pshome 中的机器级配置文件。如果您希望远程功能可用,您必须在远程会话配置的启动脚本中初始化它们。查看远程服务器上的 get/set-pssessionconfiguration。