Powershell Remote Access Profiles

How to use the function in my profile on a remote computer when used Enter-PSSessionon my local computer to open a remote PowerShell session.

+6
source share
5 answers

You can not. When you start a remote interactive session with enter-pssession, the remote profile is loaded. In addition, only the machine level profile is loaded into $ pshome. If you need remote functions, you will have to initialize them when you run the remote session configuration script. Look at get / set-pssessionconfiguration on the remote server.

0
source

By JonZ and x0n:

pssessions , .

Enter-PSSession . , $pshome.

, ( , , ..), ( ).

Register-PSSessionConfiguration . 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 youradminuser@yourtargetdomain

( $computername - , pssession).

PowerShell Remoting 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 [My] \WindowsPowerShell\Profile.ps1

,
$ [] \Profile.ps1

, -
$ PsHome\Microsoft.PowerShell_profile.ps1

,
$ PsHome\Profile.ps1

, - ISE
$ [] \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

.
% 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

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\Documents\PowerShell\profile.ps1

Microsoft.PowerShell.
$ ENV: USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

+15

, , 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
}

Invoke-Command -FilePath, .

+5

:

  1. . :

Copy-Item -Path $profile.CurrentUserAllHosts -Destination \\computername\C$\Users\MyAccountName\Documents

  1. PSSession

Enter-PSSession -ComputerName ComputerName

  1. Dot

..\Profile.ps1

:

  • ,
  • You must put an end to the profile each time you enter a PSSession.

The advantages of this solution:

  • The function is not needed and you just log into the PSSession, as always
  • You do not change the profile of all users by changing the default session configuration

(Thanks to Jeffrey Hicks for the tip on point sources )

0
source

All Articles