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.
相关问题
- How to Debug/Register a Permanent WMI Event Which
- How can I do variable substitution in a here-strin
- How to use a default value with parameter sets in
- Does powershell have a method_missing()?
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- EscapeDataString having differing behaviour betwee
- PowerShell Change owner of files and folders
- Command line escaping single quote for PowerShell
- Is there a simple way to pass specific *named* Pow
- How do I access an element with xpath with a names
- How to 'Grant Permissions' Using Azure Act
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.
By JonZ and x0n:
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:To use this preconfigured session you would type, from the local computer:
or
(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
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
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:
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.