使用 Windows 终端的 PowerShell 远程处理配置文件
我已经跳入深渊并完全致力于将 Windows Terminal 作为我的默认 PowerShell 环境。我喜欢拥有一个界面,其中包含用于不同终端配置文件的选项卡。 Windows 终端使我可以轻松打开 PowerShell 7、Windows PowerShell、Ubuntu 实例甚至没有配置文件的 PowerShell 会话的选项卡。我需要的最后一个部分是启动 Windows 终端配置文件并连接到远程计算机的简单方法。想看看我是怎么做到的吗?
首先,我在 Windows 终端设置中创建了一个新的配置文件。我假设你知道如何做到这一点或者能够弄清楚。如果您复制并粘贴现有配置文件,请不要忘记设置新的 GUID。对于我的第一个远程配置文件,我想创建一个到我的测试域控制器 DOM1 的 PowerShell 远程会话。这是我针对该部分的 Windows 终端配置文件。
{
"acrylicOpacity": 0.5,
"background": "#0a52e2",
"closeOnExit": true,
"colorScheme": "Jeff",
"commandline": "powershell.exe -nologo -noprofile -noexit -file c:\\scripts\\wtpsremote.ps1 dom1",
"cursorColor": "#FFFFFF",
"cursorShape": "underscore",
"fontFace": "Cascadia Code",
"fontSize": 14,
"guid": "{b89c512a-cd2b-4fb4-a6d2-3366cee85970}",
"historySize": 9001,
"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
"name": "DOM1",
"padding": "0, 0, 0, 0",
"snapOnInput": true,
"startingDirectory": "%USERPROFILE%",
"tabTitle": "PSRemote: DOM1.Company.Pri",
"useAcrylic": false
},
魔法位集中在命令行设置上。在我的个人资料中,我告诉 Windows 终端启动 Windows PowerShell,不带徽标、没有个人资料,并且不要退出。然后我为该脚本提供一个文件和一个参数。我最初对 -Command 很感兴趣,但最终发现简单地调用脚本文件更容易。 wtpsremote.ps1 文件创建远程会话。
#requires -version 5.1
#wtpsremote.ps1
[cmdletbinding()]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Specify remote computer name")]
[ValidateNotNullOrEmpty()]
[string]$Computername,
[pscredential]$Credential,
[Parameter( HelpMessage = "A profile script to run in the remote session")]
[ValidateScript( {Test-Path $_})]
[string]$RemoteProfile = "C:\scripts\RemoteProfile.ps1",
[Parameter(HelpMessage = "Specify a remote endpoint other than the default Microsoft.powershell")]
[string]$ConfigurationName,
[int32]$Port,
[switch]$UseSSL
)
#remove this this parameter if specified since it can't be used to create a new PSSession
[void]($PSBoundParameters.Remove("RemoteProfile"))
#Create a new remoting session splatting the parameters passed to this script
$remote = New-PSSession @PSBoundParameters
#run the remote profile script
if ($RemoteProfile) {
Invoke-Command -Session $remote -FilePath $RemoteProfile -HideComputerName |
Select-Object -property * -ExcludeProperty runspaceID |
Format-Table -AutoSize
}
#open the pssession
Enter-PSSession -session $remote
$msg = @"
Reminder:
Remove the `$remote pssession after you exit and before you close the profile tab.
PS C:\> `$remote | Remove-PSSession
PS C:\> exit
"@
#you may need to adjust colors based on your profile settings
Write-host $msg -ForegroundColor Magenta -BackgroundColor Gray
该脚本采用许多与创建新 PSSession 相关的参数,它会执行此操作,然后连接到它。剧本更进一步。
通常,当您启动 PowerShell 远程处理会话时,不会运行配置文件脚本。但此脚本允许您指定可以远程运行的本地文件,本质上是作为配置文件脚本。这是我使用的默认值。
#requires -version 5.1
#this is a remote profile script
#create a new prompt function for the remote session
#how long has this session been running?
$initiated = Get-Date
$remotePrompt = {
#display the session runtime without the milliseconds
$ts = ((Get-Date) - $initiated).ToString().split(".")[0]
Write-Host ">$ts< " -ForegroundColor yellow -nonewline
"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}
Set-Item -Path function:\prompt -Value $remotePrompt -Force
Set-Location C:\
Clear-Host
#display some information about the remote computer
Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object @{Name="Computer";Expression={$_.CSName}},
@{Name="OS";Expression = {$_.Caption}},
@{Name="PSVersion";Expression = {$PSVersionTable.PSVersion}},
@{Name="User";Expression = {Whoami.exe}}
配置文件脚本定义了远程会话的提示,该提示将显示您已连接多长时间。它还显示有关远程计算机的摘要。您可以添加您可能需要的任何其他内容,例如定义变量、别名或导入模块。
回到wtpsremote.ps`,最后一步是显示提醒。如果您只是关闭配置文件选项卡,远程处理会话将保持断开状态,直到远程计算机将其清理或重新启动。要成为一名优秀的网络公民,完成后请输入“exit”离开远程会话。这会将您转储回运行 Windows PowerShell 的终端配置文件中。您应该删除 psession,然后可以退出或关闭配置文件选项卡。
这是打开此 Windows 终端配置文件时得到的结果。
当我使用终端时,提示会更新以显示我已连接多长时间。完成后,我可以退出。
此时,我将删除 psession 并退出选项卡。
我的连接脚本还允许您指定备用凭据,甚至备用端点。以下是 SRV1 的配置文件,它使用特定凭据连接并连接到 PowerShell 7 端点。
{
"acrylicOpacity": 0.5,
"background": "#808285",
"closeOnExit": true,
"colorScheme": "Jeff",
"commandline": "powershell.exe -nologo -noprofile -noexit -file c:\\scripts\\wtpsremote.ps1 -computername srv1 -credential company\\administrator -configurationname PowerShell.7.0.0-rc.2",
"cursorColor": "#FFFFFF",
"cursorShape": "underscore",
"fontFace": "Cascadia Code",
"fontSize": 14,
"guid": "{173fd06c-883a-404b-97bd-9f955a0e85f0}",
"historySize": 9001,
"icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
"name": "SRV1",
"padding": "0, 0, 0, 0",
"snapOnInput": true,
"startingDirectory": "%USERPROFILE%",
"tabTitle": "PSRemote: SRV1.Company.Pri",
"useAcrylic": false
},
我发现这些远程选项卡非常有用,希望您也会如此。享受!
更新
我更新了在 Windows 终端中创建远程配置文件的技术和代码,现在支持跨平台。在这里阅读相关内容。