注意:正在考虑更改远程端点 PowerShell [Core] 的目标默认情况下 - 从 7.0 开始仍然是 Window PowerShell :见this GitHub issue。
本地指定的远程会话配置决定了远程机器上将使用什么 PowerShell 版本,可能还有版本:
Ad hoc,您可以使用Invoke-Command、New-PSSession、Enter-PSSession 等远程 cmdlet 的 -ConfigurationName 参数来指定明确的会话配置。
-
永久,通过配置,您可以通过$PSSessionConfigurationName preference variable设置默认会话配置(链接的帮助主题还讨论了其他远程-session相关的偏好变量,即$PSSessionApplicationName和$PSSessionOption)
- 默认情况下,客户端连接到远程机器上的会话配置
microsoft.powershell(见下文)。因此,您也可以选择更改此配置的定义在远程目标机器上,但请注意,这意味着所有客户端使用默认值将使用重新定义的配置 - 请参阅底部了解如何实现此重新定义。
在远程操作的目标计算机上,Get-PSSessionConfiguration cmdlet 列出了客户端可以用来连接的所有已注册会话配置,您可以使用Register-PSSessionConfiguration 和Unregister-PSSessionConfiguration 进行管理:
警告:Get-PSSessionConfiguration 必须在提升会话中运行(以管理员身份),并且由于错误在 Windows PowerShell 5.1 中,您可能需要先运行以下虚拟命令:$null = Get-Command Test-WSMan,以确保定义了 wsman: 驱动器。
名称以 'microsoft.powershell' 为前缀的会话配置属于 Windows PowerShell。
前缀 'PowerShell.' 指的是 PowerShell Core。
$PSSessionConfigurationName 在 both 版本中默认为 'http://schemas.microsoft.com/powershell/Microsoft.PowerShell',这意味着 Windows PowerShell 默认针对远程计算机 即使您'从 PowerShell Core 运行:
Microsoft.PowerShell 部分指的是(64 位)Windows PowerShell 会话配置,由Get-PSSessionConfiguration(小写)列出。
http://schemas.microsoft.com/powershell/ 前缀是可选的,可以省略;请注意,在前缀中使用 https: 确实 not 工作,并且将 not 自动切换到基于 SSL 的传输;对于后者,需要explicit configuration。请注意,如果您的所有远程处理都发生在 Windows 域中,则不需要基于 HTTPS/SSL 的远程处理。
以远程计算机上的 PowerShell Core (PowerShell v6+) 为目标:
# Connect to computer $comp and make it execute $PSVersionTable
# in PowerShell Core v7.x, which tells you what PowerShell edition
# and version is running.
Invoke-Command -ComputerName $comp -ConfigurationName PowerShell.7 { $PSVersionTable }
-
要在给定的客户端机器上在默认情况下持续定位 PowerShell Core,请将以下内容添加到您的
$PROFILE 文件中:李>
# When remoting, default to running PowerShell Core v7.x on the
# the target machines:
$PSSessionConfigurationName = 'PowerShell.7'
-
要让给定远程服务器机器的所有客户端以默认情况下持久为目标,您必须重新定义服务器的
microsoft.powershell会话配置,这需要管理员权限;您可以调整以下 sn-p:
# Run WITH ELEVATION (as administrator) and
# ONLY IF YOU UNDERSTAND THE IMPLICATIONS.
$ErrorActionPreference = 'Stop'
# The configuration whose definition you want to make the new default.
$newDefaultConfigSource = 'PowerShell.7'
# Standard registry locations and names.
$defaultConfigName = 'Microsoft.PowerShell'
$configXmlValueName = 'ConfigXml'
$configRootKey = 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN\Plugin'
# Rename the current default configuration XML to "ConfigXml.OLD" to keep a backup.
Rename-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName -NewName "$configXmlValueName.OLD"
# Get the configuration XML from the configuration that should become the new default.
# Modify it to replace the source configuration name with the default configuration name.
$xmlText = (Get-ItemPropertyValue $configRootKey\$newDefaultConfigSource $configXmlValueName) -replace
('\b{0}\b' -f [regex]::Escape($newDefaultConfigSource)), $defaultConfigName
# Save the modified XML as the default configuration's config XML.
Set-ItemProperty $configRootKey\$defaultConfigName $configXmlValueName $xmlText
# Restart the WinRM service for changes to take effect.
Restart-Service WinRM