0
votes

I'm researching whether it's possible to host an always-running Powershell remoting process.

By default, PSRemoting will start a process as the authenticated user. However, using custom PS Session Configurations, it's possible to set a "run as account" that will ensure that the remote process (on the server, so to speak) always runs as a "known" user, regardless of the identify of the connecting client.

The reason for me researching this, is that I notice that Ansible runs quite slowly against windows servers, and I suspect this is due to the fact that the remoting process on the server gets spun up with each command Ansible sends. I'd like to see if it's possible to have an always-running process that is "ready" in order to speed up executions.

This is as far as I've gotten:

$ansiblecred = get-credential
New-PSSessionConfigurationFile `
    -path "C:\sessionconfig.pssc" -SessionType Default `
    -RequiredGroups @{ And = 'Administrators' }
Unregister-PSSessionConfiguration -Name ansible -force
Register-PSSessionConfiguration `
    -Path "C:\sessionconfig.pssc" -Name ansible `
    -RunAsCredential $ansiblecred -AccessMode Remote `
    -UseSharedProcess -ThreadOptions ReuseThread
restart-service winrm


$remotecred = Get-Credential

$i = 0
while ($i -lt 10)
{
    #This is slow because the remoting session is setup/teard down every time
    Invoke-Command -ComputerName "localhost" -Credential $remotecred -Authentication Basic -ScriptBlock {$env:computername} -ConfigurationName ansible
    $i ++
}

Even tho I'm connecting to the session with a different credential, the actual process runs as the "service" credential, so that part's good.

However, it seems to be still spinning up and down the process on each execution.

Just for clarification: The client here is not regular Powershell, it's client which will interact directly with the wsman service over http. So while I appreciate all responses, suggestions based around client-side Powershell code (such as new-pssession, invoke-command etc) are not gonna help :-|

Any pointers would be appreciated here, I'm trying to get to a place where the remoting process simply lives regardless of sessions being executed or not. Is this possible?

1
Try starting a PowerShell process with powershell.exe -Server as the service account. If that process is reused as you expect, you could develop a small service that starts the server and restarts it if needed. - Patrick Meinecke

1 Answers

0
votes

Create a session outside and use it in the loop.

$Session = New-PSSession -ComputerName $Server -ConfigurationName MyConfiguration

While(1){
Invoke-Command -Session $Session -Credential $remotecred -Authentication Basic -ScriptBlock {'my code'}
sleep 10

}