2010-06-06 116 views
5

在本地計算機上使用Enter-PSSession打開遠程PowerShell會話時,如何在遠程計算機的配置文件中使用某個功能。Powershell遠程配置文件

+0

任何具有完整源代碼的最終解決方案? – Kiquenet 2013-02-21 08:51:06

回答

-1

你不能。使用enter-pssession啓動遠程交互式會話時,會加載遠程配置文件。此外,只加載$ pshome中的機器級配置文件。如果您想要遠程功能可用,您必須在遠程會話配置的啓動腳本中初始化它們。查看遠程服務器上的get/set-pssession配置。

+0

這些事情的處理方式是否發生了變化?我使用的是PowerShell 5.1,我在C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \中創建了一個profile.ps1,只是爲了設置一個別名,這個別名在本地使用shell時正在工作。但是,當使用Enter-PSSession連接到該機器時,別名不起作用... – Alex 2017-07-31 21:45:04

+0

@Alex也許您可能需要重新啓動服務器端的WinRM服務?重啓應該可以做到,但是如果這有點重量級的話,那麼「restart-service winrm」應該可以工作。或者,如您所說,事情可能已經發生變化...... – x0n 2017-08-07 01:56:55

15

通過JonZ和x0n:

當您使用默認的會話配置PSSession中,沒有配置文件腳本運行。

當與Enter-PSSession開始遠程交互式會話時,會加載遠程配置文件。此外,只加載$pshome中的機器級配置文件。

如果您想預先配置會話(加載自定義功能,管理單元,模塊等),添加配置文件腳本到一個新的sessionconfiguration(用於初始化它們的啓動腳本遠程會話配置)。

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 [email protected] 

(其中$computername是re的主機名mote服務器,您在其中註冊了pssession配置)。

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

六種不同的Windows PowerShell配置文件路徑和使用

當前使用R,當前主機 - 控制檯
$ 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外殼。
%WINDIR%\ SYSTEM32 \ WindowsPowerShell \ V1.0 \ Microsoft.PowerShell_profile.ps1

此配置文件僅適用於當前用戶,但會影響所有的炮彈。
%USERPROFILE%\我的文檔\ WindowsPowerShell \ profile.ps1

此配置文件僅適用於當前用戶和Microsoft.PowerShell外殼。
%USERPROFILE%\我的文檔\ WindowsPowerShell \ Microsoft.PowerShell_profile.ps1

+0

「此外,只有$ pshome中的計算機級配置文件已加載。」這在某些時候是否改變了?在Windows 8.1和WIndoes Server 2012中,Enter-PSSession不在目標機器上運行任何Powershell配置文件。 – 2014-05-14 23:17:27

+0

@GreenstoneWalker - 我也看到了。使用Win 2008 R2/PS 3.0服務器。 – smaglio81 2014-08-07 18:41:10

+0

如果您不想依賴特定的Windows版本或語言,請使用:[[Environment] :: GetFolderPath('MyDocuments')' – mtman 2016-11-25 20:42:08

4

傑森, 在我的情況,我想有我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 
} 

您可以修改調用命令-FilePath參數把你喜歡的任何文件。