gpt4 book ai didi

Powershell 远程处理配置文件

转载 作者:行者123 更新时间:2023-12-02 18:48:42 25 4
gpt4 key购买 nike

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

最佳答案

作者:JonZ 和 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.

如果您希望预配置 session (以加载自定义功能、管理单元、模块等),请将配置文件脚本添加到新的 session 配置(以便在启动脚本中初始化它们)远程 session 配置)。

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

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

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

要使用此预配置 session ,您需要从本地计算机键入:

Enter-PSSession -ComputerName $computername -ConfigurationName WithProfile

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

(其中 $computername 是您注册 pssessionconfiguration 的远程服务器的主机名)。

关于 PowerShell 远程处理的一个很好的来源是 Administrator's Guide to Powershell Remoting .

引用文献:
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 文档) https://learn.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
$Home[我的]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%\我的文档\WindowsPowerShell\profile.ps1

此配置文件仅适用于当前用户和 Microsoft.PowerShell shell。
%UserProfile%\我的文档\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

关于Powershell 远程处理配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2985032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com