WinRM-Environment Module – Make your remote PSSession environment the same as your local session. Plus the ability to edit files *within* the PowerShell console.

I’ve worked on this module on and off for the past month or so and it’s finally in good enough shape that I feel comfortable sharing.

Link to Module:

https://github.com/pldmgg/misc-powershell/tree/master/MyModules/WinRM-Environment

Available functions:

‘Test-RemotePaths’, ‘Send-LocalObjects’, ‘Initialize-ModulesInRemoteSession’, ‘Initialize-PSProfileInRemoteSession’, ‘Send-ItemToRemoteHost’, ‘Receive-ItemFromRemoteHost’, ‘Edit-FileWinRM’, ‘New-InteractivePSSession’

Here are some highlights:

New-InteractivePSSesion

The New-InteractivePSSession creates and enters a new interactive PSSession connected to a Remote Server and optionally forward aspects of your local environment to your remote session.

This function includes parameters to forward/load the following to/in your New Remote PSSession:

  • Variables

  • A PowerShell Profile

  • Currently Loaded Modules

  • Files and/or Directories (and their contents)

This way, you’ll be able to get a seamless experience PSRemoting from one machine to another.

TODO: Add the ability to digest an environment variable that contains a hashtable of default parameters and their values.

.EXAMPLE

$Params = @{ RemoteHost = "Win12Chef" RemoteHostUser = "testadmin" LocalVarsToForwardToRemoteSession = "ArrayOfBeerNames","FoodPriceHashtable" ItemsToForwardToRemoteHost = "$HOMEtempdir1","$HOMEtempdir2info.txt" DestinationDirectory "C:Userszeroadmin" ModulesToForwardToRemoteHost "NTFSSecurity" } New-InteractivePSSession @Params 

Edit-FileWinRM

The Edit-FileWinRM function is capable of editing files on the LocalHost or a RemoteHost, where the terms LocalHost and RemoteHost are context sensitive. In other words, LocalHost can be thought of as $env:COMPUTERNAME and Remote Host can be thought of as a machine other than $env:COMPUTERNAME (precluding the unlikely circumstance in which $env:COMPUTERNAME is manually changed for some strange reason. That being said, the function is NOT dependent on the value of $env:COMPUTERNAME).

If you use the function within a Remote PSSession, you will edit the file in the console using Vim for Windows. If you don’t have Vim for Windows, it will be downloaded from http://www.vim.org/download.php and unzipped in $HOMEDownloads. Then, vim.exe will be copied to $HOMEvim.exe and a default .vimrc config file will be generated at $HOME.vimrc. Then vim.exe will be run. Nothing is installed.

If you use the function outside of a Remote PSSession, you can specify whatever Editor you would like to use that is installed on the local system.

If you don’t specify an Editor at all, the function looks for some of the most popular Editors where they can be expected to be found on the local filesystem. It’ll use the first one that it finds.

TODO: Make this function capable of digesting a hashtable environment variable listing ordered Editor preferences.

.EXAMPLE

# Edit File present on the Local Host WITHOUT being in a PSSession (i.e. you're just in the normal PowerShell Console) Edit-FileWinRM -FilePath $HOME.gitconfig -Editor "C:Program Files (x86)Microsoft VS CodeCode.exe" 

.EXAMPLE

# Edit File present on a Remote Host WITHOUT being in a PSSession (i.e. you're just in the normal PowerShell Console) $Params = @{ FilePath = "C:Userszeroadmin.gitconfig" Editor = "C:Program Files (x86)Microsoft VS CodeCode.exe" RemoteHost = "Win12Chef" Credentials = $MyCreds } Edit-FileWinRM @Params 

.EXAMPLE

# Edit File present on the Local Host (i.e. $env:COMPUTERNAME) WHILE IN a PSSession Edit-FileWinRM -FilePath $HOME.gitconfig 

.EXAMPLE

# Edit File present on a Remote Host (i.e. tertiary target) WHILE IN a PSSession $Params = @{ FilePath = "C:Userszeroadmin.gitconfig" RemoteHost = "Exchange01" Credentials = $MyCreds } Edit-FileWinRM @Params 

Send-ItemToRemoteHost

The Send-ItemToRemoteHost uploads one or more files or directories from the LocalHost to the RemoteHost, where the terms LocalHost and RemoteHost are context sensitive. In other words, LocalHost can bethought of as $env:COMPUTERNAME and RemoteHost can be thought of as a machine other than $env:COMPUTERNAME (precluding the unlikely circumstance in which $env:COMPUTERNAME is manually changed for some strange reason. That being said, the function is NOT dependent on the value of $env:COMPUTERNAME).

This function was created to adjudicate Send-Item’s and Copy-Item’s undesirable default behavior, which is to OVERWRITE the item if it already exists on the Remote Host. This function checks for the existence of the path(s) on the Remote Host and prompts you for confirmation if they already exist. You can override this behavior by using the -ForceSend parameter, which will automatically overwrite the item on the Remote Host without prompt.

.EXAMPLE

$Items = @("C:UserstestadminDocumentsSpreadsheets","C:UserstestadminDocumentsinvoice.pdf") Send-ItemToRemoteHost -PSSession $FileSharingSession -ItemsToSendToRemoteHost $Items -DestinationDirectory "C:UserszeroadminDocuments" 

Receive-ItemFromRemoteHost

The Receive-ItemFromRemoteHost function downloads one or more files or directories from a RemoteHost to the LocalHost, where the terms LocalHost and RemoteHost are context sensitive. In other words, LocalHost can be thought of as $env:COMPUTERNAME and RemoteHost can be thought of as a machine other than $env:COMPUTERNAME (precluding the unlikely circumstance in which $env:COMPUTERNAME is manually changed for some strange reason. That being said, the function is NOT dependent on the value of $env:COMPUTERNAME).

.EXAMPLE

$Items = @("C:UserszeroadminDocumentsSpreadsheets","C:UserszeroadminDocumentsinvoice.pdf") Receive-ItemFromRemoteHost -PSSession $FileSharingSession -ItemsToReceiveFromRemoteHost $Items -DestinationDirectory $HOMEDownloads 

Send-LocalObject

The Send-LocalObjects function loads variables from the Local PSSession in the Remote PSSession.

.EXAMPLE

$LocalVarsToForwardToRemoteSession = @("ArrayOfBeerNames","FoodPriceHashtable") Send-LocalObjects -PSSession $FileSharingSession -LocalVarsToForward $LocalVarsToForwardToRemoteSession 

Other Notable Functions Include Initialize-ModulesInRemoteSession and Initialize-PSProfileInRemoteSession

FYI, every function has help text with additional examples. This module should work on PowerShell versions older than 5.0, but it needs more testing on the older versions. I should be publishing this module to the PSGallery at some point this week. Also, some additional functionality in this module will become available if you have my UserSessionEx module loaded in your current session prior to using WinRM-Environment.

Link to UserSessionEx Module:

https://github.com/pldmgg/misc-powershell/tree/master/MyModules/UserSessionEx

As always, advice and criticism is always welcome. Hope this can help folks out!

submitted by /u/fourierswager
[link] [comments]

Leave a Reply