Skip to content

Commit 3cd2308

Browse files
kalgizTravisEz13
authored andcommitted
Create the default PSSession configuration, not tied to a specific PowerShell version. (#6519)
Create the default PSSession configuration, not tied to a specific PowerShell version. When Enable-PSRemoting command is run, it creates 2 sessions configurations: first, the same as it was before with the name containing the current version expressed as: 'PowerShell.$PSVersionTable.GitCommitId' second with the default name 'PowerShell.6' so that administrators wouldn't have to guess which specific version is installed on the target. PR addresses the issue: #6470
1 parent aefc0d8 commit 3cd2308

3 files changed

Lines changed: 243 additions & 138 deletions

File tree

src/System.Management.Automation/engine/remoting/commands/CustomShellCommands.cs

Lines changed: 117 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,13 @@ function Register-PSSessionConfiguration
188188
}}
189189
}}
190190
if (([System.Management.Automation.Runspaces.PSSessionConfigurationAccessMode]::Local.Equals($accessMode) -or
191-
([System.Management.Automation.Runspaces.PSSessionConfigurationAccessMode]::Remote.Equals($accessMode)-and $disableNetworkExists)) -and
192-
!$haveDisableACE)
191+
([System.Management.Automation.Runspaces.PSSessionConfigurationAccessMode]::Remote.Equals($accessMode))) -and !$haveDisableACE)
193192
{{
194-
# Add network deny ACE for local access or remote access with PSRemoting disabled ($disableNetworkExists)
193+
# Add network deny ACE for local access or remote access with PSRemoting disabled.
195194
$sd.DiscretionaryAcl.AddAccess(""deny"", $networkSID, 268435456, ""None"", ""None"")
196195
$newSDDL = $sd.GetSddlForm(""all"")
197196
}}
198-
if ([System.Management.Automation.Runspaces.PSSessionConfigurationAccessMode]::Remote.Equals($accessMode) -and -not $disableNetworkExists -and $haveDisableACE)
197+
if ([System.Management.Automation.Runspaces.PSSessionConfigurationAccessMode]::Remote.Equals($accessMode) -and $haveDisableACE)
199198
{{
200199
# Remove the specific ACE
201200
$sd.discretionaryacl.RemoveAccessSpecific('Deny', $securityIdentifierToPurge, 268435456, 'none', 'none')
@@ -4796,16 +4795,119 @@ public sealed class EnablePSRemotingCommand : PSCmdlet
47964795

47974796
//TODO: CLR4: Remove the logic for setting the MaxMemoryPerShellMB to 200 MB once IPMO->Get-Command->Get-Help memory usage issue is fixed.
47984797
private const string enableRemotingSbFormat = @"
4799-
function Generate-PluginConfigFile
4798+
Set-StrictMode -Version Latest
4799+
4800+
function New-PluginConfigFile
48004801
{{
4802+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=""Medium"")]
48014803
param(
48024804
[Parameter()] [string] $pluginInstallPath
48034805
)
48044806
$pluginConfigFile = Join-Path $pluginInstallPath ""RemotePowerShellConfig.txt""
48054807
48064808
# This always overwrites the file with a new version of it (if it already exists)
4807-
Set-Content -Path $pluginConfigFile -Value ""PSHOMEDIR=$PSHOME""
4808-
Add-Content -Path $pluginConfigFile -Value ""CORECLRDIR=$PSHOME""
4809+
Set-Content -Path $pluginConfigFile -Value ""PSHOMEDIR=$PSHOME"" -ErrorAction Stop
4810+
Add-Content -Path $pluginConfigFile -Value ""CORECLRDIR=$PSHOME"" -ErrorAction Stop
4811+
}}
4812+
4813+
function Copy-PluginToEndpoint
4814+
{{
4815+
param(
4816+
[Parameter()] [string] $endpointDir
4817+
)
4818+
$resolvedPluginInstallPath = """"
4819+
$pluginInstallPath = Join-Path ([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Windows) + ""\System32\PowerShell"") $endpointDir
4820+
if (!(Test-Path $pluginInstallPath))
4821+
{{
4822+
$resolvedPluginInstallPath = New-Item -Type Directory -Path $pluginInstallPath
4823+
}}
4824+
else
4825+
{{
4826+
$resolvedPluginInstallPath = Resolve-Path $pluginInstallPath
4827+
}}
4828+
if (!(Test-Path $resolvedPluginInstallPath\{5}))
4829+
{{
4830+
Copy-Item -Path $PSHOME\{5} -Destination $resolvedPluginInstallPath -Force -ErrorAction Stop
4831+
if (!(Test-Path $resolvedPluginInstallPath\{5}))
4832+
{{
4833+
Write-Error ($errorMsgUnableToInstallPlugin -f ""{5}"", $resolvedPluginInstallPath)
4834+
return $null
4835+
}}
4836+
}}
4837+
return $resolvedPluginInstallPath
4838+
}}
4839+
4840+
function Register-Endpoint
4841+
{{
4842+
param(
4843+
[Parameter()] [string] $configurationName
4844+
)
4845+
#
4846+
# Section 1:
4847+
# Move pwrshplugin.dll from $PSHOME to the endpoint directory
4848+
#
4849+
# The plugin directory pattern for endpoint configuration is:
4850+
# '$env:WINDIR\System32\PowerShell\' + powershell_version,
4851+
# so we call Copy-PluginToEndpoint function only with the PowerShell version argument.
4852+
4853+
$pwshVersion = $configurationName.Replace(""PowerShell."", """")
4854+
$resolvedPluginInstallPath = Copy-PluginToEndpoint $pwshVersion
4855+
if (!$resolvedPluginInstallPath) {{
4856+
return
4857+
}}
4858+
4859+
#
4860+
# Section 2:
4861+
# Generate the Plugin Configuration File
4862+
#
4863+
New-PluginConfigFile $resolvedPluginInstallPath
4864+
4865+
#
4866+
# Section 3:
4867+
# Register the endpoint
4868+
#
4869+
$null = Register-PSSessionConfiguration -Name $configurationName -force -ErrorAction Stop
4870+
4871+
set-item -WarningAction SilentlyContinue wsman:\localhost\plugin\$configurationName\Quotas\MaxShellsPerUser -value ""25"" -confirm:$false
4872+
set-item -WarningAction SilentlyContinue wsman:\localhost\plugin\$configurationName\Quotas\MaxIdleTimeoutms -value {4} -confirm:$false
4873+
restart-service winrm -confirm:$false
4874+
}}
4875+
4876+
function Register-EndpointIfNotPresent
4877+
{{
4878+
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=""Medium"")]
4879+
param(
4880+
[Parameter()] [string] $Name,
4881+
[Parameter()] [bool] $Force,
4882+
[Parameter()] [string] $queryForRegisterDefault,
4883+
[Parameter()] [string] $captionForRegisterDefault
4884+
)
4885+
#
4886+
# This cmdlet will make sure default powershell end points exist upon successful completion.
4887+
#
4888+
# Windows PowerShell:
4889+
# Microsoft.PowerShell
4890+
# Microsoft.PowerShell32 (wow64)
4891+
#
4892+
# PowerShell Core:
4893+
# PowerShell.<version ID>
4894+
#
4895+
$errorCount = $error.Count
4896+
$endPoint = Get-PSSessionConfiguration $Name -Force:$Force -ErrorAction silentlycontinue 2>&1
4897+
$newErrorCount = $error.Count
4898+
4899+
# remove the 'No Session Configuration matches criteria' errors
4900+
for ($index = 0; $index -lt ($newErrorCount - $errorCount); $index ++)
4901+
{{
4902+
$error.RemoveAt(0)
4903+
}}
4904+
4905+
$qMessage = $queryForRegisterDefault -f ""$Name"",""Register-PSSessionConfiguration {0} -force""
4906+
if ((!$endpoint) -and
4907+
($force -or $pscmdlet.ShouldProcess($qMessage, $captionForRegisterDefault)))
4908+
{{
4909+
Register-Endpoint $Name
4910+
}}
48094911
}}
48104912
48114913
function Enable-PSRemoting
@@ -4836,70 +4938,16 @@ function Enable-PSRemoting
48364938
# first try to enable all the sessions
48374939
Enable-PSSessionConfiguration @PSBoundParameters
48384940
4839-
#
4840-
# This cmdlet will make sure default powershell end points exist upon successful completion.
4841-
#
4842-
# Windows PowerShell:
4843-
# Microsoft.PowerShell
4844-
# Microsoft.PowerShell32 (wow64)
4845-
#
4846-
# PowerShell Core:
4847-
# PowerShell.<version ID>
4848-
#
4849-
$errorCount = $error.Count
4850-
$endPoint = Get-PSSessionConfiguration {0} -Force:$Force -ErrorAction silentlycontinue 2>&1
4851-
$newErrorCount = $error.Count
4852-
4853-
# remove the 'No Session Configuration matches criteria' errors
4854-
for ($index = 0; $index -lt ($newErrorCount - $errorCount); $index ++)
4855-
{{
4856-
$error.RemoveAt(0)
4857-
}}
4858-
4859-
$qMessage = $queryForRegisterDefault -f ""{0}"",""Register-PSSessionConfiguration {0} -force""
4860-
if ((!$endpoint) -and
4861-
($force -or $pscmdlet.ShouldProcess($qMessage, $captionForRegisterDefault)))
4862-
{{
4863-
$resolvedPluginInstallPath = """"
4864-
#
4865-
# Section 1:
4866-
# Move pwrshplugin.dll from $PSHOME to the endpoint directory
4867-
#
4868-
$pluginInstallPath = Join-Path ""$env:WINDIR\System32\PowerShell"" $psversiontable.GitCommitId
4869-
if (!(Test-Path $pluginInstallPath))
4870-
{{
4871-
$resolvedPluginInstallPath = New-Item -Type Directory -Path $pluginInstallPath
4872-
}}
4873-
else
4874-
{{
4875-
$resolvedPluginInstallPath = Resolve-Path $pluginInstallPath
4876-
}}
4877-
if (!(Test-Path $resolvedPluginInstallPath\{5}))
4878-
{{
4879-
Copy-Item $PSHOME\{5} $resolvedPluginInstallPath -Force
4880-
if (!(Test-Path $resolvedPluginInstallPath\{5}))
4881-
{{
4882-
Write-Error ($errorMsgUnableToInstallPlugin -f ""{5}"", $resolvedPluginInstallPath)
4883-
return
4884-
}}
4885-
}}
4941+
Register-EndpointIfNotPresent -Name {0} $Force $queryForRegisterDefault $captionForRegisterDefault
48864942
4887-
#
4888-
# Section 2:
4889-
# Generate the Plugin Configuration File
4890-
#
4891-
Generate-PluginConfigFile $resolvedPluginInstallPath
4892-
4893-
#
4894-
# Section 3:
4895-
# Register the endpoint
4896-
#
4897-
$null = Register-PSSessionConfiguration -Name {0} -force
4898-
4899-
set-item -WarningAction SilentlyContinue wsman:\localhost\plugin\{0}\Quotas\MaxShellsPerUser -value ""25"" -confirm:$false
4900-
set-item -WarningAction SilentlyContinue wsman:\localhost\plugin\{0}\Quotas\MaxIdleTimeoutms -value {4} -confirm:$false
4901-
restart-service winrm -confirm:$false
4943+
# Create the default PSSession configuration, not tied to specific PowerShell version
4944+
# e. g. 'PowerShell.6'.
4945+
$powershellVersionMajor = $PSVersionTable.PSVersion.ToString()
4946+
$dotPos = $powershellVersionMajor.IndexOf(""."")
4947+
if ($dotPos -ne -1) {{
4948+
$powershellVersionMajor = $powershellVersionMajor.Substring(0, $dotPos)
49024949
}}
4950+
Register-EndpointIfNotPresent -Name (""PowerShell."" + $powershellVersionMajor) $Force $queryForRegisterDefault $captionForRegisterDefault
49034951
49044952
# PowerShell Workflow and WOW are not supported for PowerShell Core
49054953
if (![System.Management.Automation.Platform]::IsCoreCLR)

0 commit comments

Comments
 (0)