Weboldal keresés

Platformok közötti PowerShell-profilok Windows terminállal


A hét elején megosztottam a Windows terminálprofil létrehozására vonatkozó technikáimat, amely egy távoli PowerShell-munkamenetet nyit meg. De a PowerShell 7 segítségével nem Windows-os gépekhez is tudok csatlakozni SSH használatával. Szóval miért nem bővítem ki a kódomat, hogy lehetővé tegye a csatlakozást egy Linux-boxhoz? Mielőtt bármit is kipróbálna, amit meg fogok osztani, győződjön meg arról, hogy manuálisan tud létrehozni és beírni egy PSS-sessiont a Linux céljára SSH használatával. Ha ezt megteheti, létrehozhat egy Windows terminálprofilt.

A kapcsolati parancsfájl

Módosítottam a meglévő szkriptemet a távoli munkamenet létrehozásához. Mivel a powershell helyett a pwsh parancsot kell meghívnom, létrehozhattam volna egy második szkriptet is a megfelelő paraméterekkel. De akkor szkripteket kellene karbantartanom. Ezért ehelyett módosítottam a wtpsremote.ps1 szkriptemet, és paraméterkészleteket használtam.

Frissítettem a szkriptet, hogy bőbeszédű üzeneteket is tartalmazzon, amelyek segítenek a hibaelhárításban. És jobb hibakezelés.

#requires -version 5.1

<#
.Synopsis
Create a remote PowerShell session
.Description
This script is intended to be called from a Windows Terminal profile to create
a PowerShell remoting session. If you are using the ssh parameter set, you 
will need to invoke this script with pwsh.
.Link
New-PSSession
.Link
Enter-PSSession
#>

[cmdletbinding(DefaultParameterSetName = "Computer")]
Param(
    [Parameter(Position = 0, Mandatory, HelpMessage = "Specify remote computer name",ParameterSetName="Computer")]
    [ValidateNotNullOrEmpty()]
    [string]$Computername,
    [Parameter(ParameterSetName="Computer")]
    [pscredential]$Credential,
    [Parameter( HelpMessage = "A profile script to run in the remote session")]
    [ValidateScript( {Test-Path $_})]
    [string]$RemoteProfile,
    [Parameter(HelpMessage = "Specify a remote endpoint other than the default Microsoft.powershell")]
    [string]$ConfigurationName,
    [Parameter(HelpMessage = "Specify an alternate port number")]
    [int32]$Port,
    [Parameter(ParameterSetName="Computer")]
    [switch]$UseSSL,
    [Parameter(Position = 0, Mandatory,ParameterSetName = "ssh")]
    [ValidateNotNullorEmpty()]
    [string]$Hostname,
    [Parameter(ParameterSetName = "ssh")]
    [string]$Username,
    [Parameter(ParameterSetName = "ssh")]
    [string]$Subsystem,
    [Parameter(ParameterSetName = "ssh")]
    [string]$KeyFilePath
)

Write-Verbose "Starting $($MyInvocation.MyCommand)"
Write-Verbose "Using parameter set $($pscmdlet.parametersetname)"
if ($PSCmdlet.ParameterSetName -eq "ssh") {
    Write-Verbose "adding SSHTransport parameter"
    [void]($PSBoundParameters.add("SSHTransport",$True))
}
#remove this this parameter if specified since it can't be used to create a new PSSession
if ($PSBoundParameters.ContainsKey("RemoteProfile")) {
    Write-Verbose "Removing RemoteProfile bound parameter"
    [void]($PSBoundParameters.Remove("RemoteProfile"))
}

#Create a new remoting session splatting the parameters passed to this script
Try {
    Write-Verbose "Using these parameters to create the PSSession"
    $PSBoundParameters | Out-String | Write-Verbose
    $remote = New-PSSession @PSBoundParameters -errorAction Stop
    $remote | Out-String | Write-Verbose
}
Catch {
    Write-Verbose "Failed to create remoting session"
    Throw $_
}
#run the remote profile script
if ($RemoteProfile) {
    Write-Verbose "Running remote profile script $RemoteProfile"
    Invoke-Command -Session $remote -FilePath $RemoteProfile -HideComputerName |
    Select-Object -property * -ExcludeProperty runspaceID
}

#open the pssession
Write-Verbose "Entering session"
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

A Windows terminálprofil

Ezzel a szkripttel létrehozhatok egy Windows terminál profilt a Linux munkamenetemhez.

{
    "acrylicOpacity": 0.8,
    "closeOnExit": true,
    "colorScheme": "Campbell",
    "commandline": "C:\\Program Files\\PowerShell\\7-preview\\preview\\pwsh-preview.cmd -nologo -noprofile -noexit -file c:\\scripts\\wtpsremote.ps1 -hostname fred-company-com -username jeff -remoteprofile c:\\scripts\\lxprofile.ps1",
    "cursorColor": "#FFFFFF",
    "cursorShape": "underscore",
    "fontFace": "Cascadia Code",
    "fontSize": 14,
    "guid": "{5668446e-620a-448f-874b-db766831b2ab}",
    "historySize": 9001,
    "icon": "ms-appx:///ProfileIcons/{574e775e-4f2a-5b96-ac1e-a2962a402336}.png",
    "name": "Fred",
    "padding": "0, 0, 0, 0",
    "snapOnInput": true,
    "startingDirectory": "C:\\",
    "tabTitle": "PSRemote: Fred",
    "backgroundImage": "C:\\users\\jeff\\Pictures\\smoking-penguin.jpg",
    "backgroundImageAlignment": "center",
    "backgroundImageStretchMode": "none",
    "backgroundImageOpacity": 0.3,
    "useAcrylic": true
}

Végül frissíteni fogom ezt a profilt, hogy a pwsh utolsó elérési útját használja.

A Linux Profile Script

Továbbra is felajánlom a távoli profilszkript futtatásának lehetőségét. Ez még mindig egy PSSession. Ez az a szkriptfájl, amelyet Linux-kapcsolataimhoz használok.

# this is for Linux based computers
 $os = $PSVersionTable.OS
 $lsb = lsb_release -d
 $osver = ($lsb -split ":")[1].Trim()
 $elevated = "N/A"
 $user = [System.Environment]::UserName
 $computer = [System.Environment]::MachineName
 #object properties will be displayed in the order they are listed here
 [pscustomObject]@{
     User            = $user
     Elevated        = $elevated
     Computername    = $computer
     OperatingSystem = $os
     OSVersion       = $osver
     PSVersion       = $PSVersionTable.PSVersion.ToString()
     Edition         = $PSVersionTable.PSEdition
     PSHost          = $host.Name
     WSMan           = $PSVersionTable.WSManStackVersion.ToString()
     ExecutionPolicy = (Get-ExecutionPolicy)
     Culture         = [System.Globalization.CultureInfo]::CurrentCulture.NativeName
 }
 $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

Ugyanezt a prompt fogalmat használom annak bemutatására, hogy mennyi ideig fut a munkamenet. Íme a végeredmény

Ezen a ponton használhatom a Windows Terminált minden PowerShell-távirányítási szükségletemhez, és mindent, amire szükségem van, egy helyen. Remélem, elmondja, mit gondol ezekről a frissítésekről.

Kapcsolódó cikkek