开发者

Powershell Remoting Profiles

How do I use a function in my prof开发者_JAVA技巧ile on the remote machine when using Enter-PSSession on my local machine to open a remote PowerShell session.


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


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.


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜