[玩转系统] 使用 Windows 终端的 PowerShell 远程处理配置文件
作者:精品下载站 日期:2024-12-14 07:57:16 浏览:15 分类:玩电脑
使用 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 终端中创建远程配置文件的技术和代码,现在支持跨平台。在这里阅读相关内容。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag