Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Remoting Profiles

How do I use a function in my profile on the remote machine when using Enter-PSSession on my local machine to open a remote PowerShell session.

like image 671
general exception Avatar asked Jun 06 '10 17:06

general exception


People also ask

What permissions are needed for PowerShell remoting?

What permissions are needed to run PowerShell on a remote machine? A. To run PowerShell on a remote box the credential used must be a local administrator if connecting via the default session configuration. This can be seen by running Get-PSSessionConfiguration (along with Remote Management Users).

How many PowerShell profiles are there?

There are, in fact, six different profiles. The Windows PowerShell console and the Windows PowerShell ISE have their own profiles. In addition, there are profiles for the current user and profiles for all users. The table that follows lists the six profiles and their associated locations.

How do I view a PowerShell profile?

The $PROFILE automatic variable stores the paths to the PowerShell profiles that are available in the current session. To view a profile path, display the value of the $PROFILE variable. You can also use the $PROFILE variable in a command to represent a path.


3 Answers

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.

like image 24
Devon Dieffenbach Avatar answered Oct 14 '22 19:10

Devon Dieffenbach


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://docs.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://docs.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

like image 84
Kiquenet Avatar answered Oct 14 '22 19:10

Kiquenet


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.

like image 28
Justin Avatar answered Oct 14 '22 20:10

Justin