mirror of
https://github.com/Grace-Solutions/Invoke-OpenSSHConfiguration.git
synced 2026-07-26 11:38:14 +00:00
Configure default SSH shell with precedence: PS7 > PS5 > unchanged
- Checks for PowerShell 7 at Program Files\PowerShell\7\pwsh.exe - Falls back to PowerShell 5 at System32\WindowsPowerShell\v1.0\powershell.exe - Leaves default shell unchanged if neither found - Sets HKLM\SOFTWARE\OpenSSH\DefaultShell registry value - Sets DefaultShellCommandOption to '-NoLogo -NoProfile -Command' - Idempotent: only updates registry if value differs
This commit is contained in:
@@ -928,6 +928,81 @@ Switch (Test-ProcessElevationStatus)
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Configure Default Shell (PowerShell 7 > PowerShell 5 > No Change)
|
||||||
|
$OpenSSHRegistryPath = 'HKLM:\SOFTWARE\OpenSSH'
|
||||||
|
|
||||||
|
#Check for PowerShell 7 first, then PowerShell 5
|
||||||
|
$PowerShell7Path = [System.IO.FileInfo]"$($Env:ProgramFiles)\PowerShell\7\pwsh.exe"
|
||||||
|
$PowerShell5Path = [System.IO.FileInfo]"$($Env:SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe"
|
||||||
|
|
||||||
|
$DefaultShellPath = $Null
|
||||||
|
$DefaultShellCommandOption = $Null
|
||||||
|
|
||||||
|
Switch ($True)
|
||||||
|
{
|
||||||
|
{($PowerShell7Path.Exists -eq $True)}
|
||||||
|
{
|
||||||
|
$DefaultShellPath = $PowerShell7Path.FullName
|
||||||
|
$DefaultShellCommandOption = '-NoLogo -NoProfile -Command'
|
||||||
|
$WriteLogMessage.Invoke(0, @("PowerShell 7 detected at `"$($DefaultShellPath)`"."))
|
||||||
|
}
|
||||||
|
|
||||||
|
{($PowerShell5Path.Exists -eq $True)}
|
||||||
|
{
|
||||||
|
$DefaultShellPath = $PowerShell5Path.FullName
|
||||||
|
$DefaultShellCommandOption = '-NoLogo -NoProfile -Command'
|
||||||
|
$WriteLogMessage.Invoke(0, @("PowerShell 5 detected at `"$($DefaultShellPath)`"."))
|
||||||
|
}
|
||||||
|
|
||||||
|
Default
|
||||||
|
{
|
||||||
|
$WriteLogMessage.Invoke(1, @("No PowerShell installation detected. Leaving default shell unchanged."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Switch ($Null -ine $DefaultShellPath)
|
||||||
|
{
|
||||||
|
{($_ -eq $True)}
|
||||||
|
{
|
||||||
|
#Ensure OpenSSH registry key exists
|
||||||
|
Switch (Test-Path -Path $OpenSSHRegistryPath -ErrorAction SilentlyContinue)
|
||||||
|
{
|
||||||
|
{($_ -eq $False)}
|
||||||
|
{
|
||||||
|
$WriteLogMessage.Invoke(0, @("Creating OpenSSH registry key. Please Wait..."))
|
||||||
|
$Null = New-Item -Path $OpenSSHRegistryPath -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#Get current values
|
||||||
|
$CurrentDefaultShell = Get-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShell' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'DefaultShell' -ErrorAction SilentlyContinue
|
||||||
|
$CurrentDefaultShellCommandOption = Get-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShellCommandOption' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'DefaultShellCommandOption' -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
#Set DefaultShell if different
|
||||||
|
Switch ($CurrentDefaultShell -ine $DefaultShellPath)
|
||||||
|
{
|
||||||
|
{($_ -eq $True)}
|
||||||
|
{
|
||||||
|
$WriteLogMessage.Invoke(0, @("Setting default shell to `"$($DefaultShellPath)`". Please Wait..."))
|
||||||
|
$Null = Set-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShell' -Value $DefaultShellPath -Type String -Force -ErrorAction SilentlyContinue
|
||||||
|
$WriteLogMessage.Invoke(0, @("Default shell configured."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#Set DefaultShellCommandOption if different
|
||||||
|
Switch ($CurrentDefaultShellCommandOption -ine $DefaultShellCommandOption)
|
||||||
|
{
|
||||||
|
{($_ -eq $True)}
|
||||||
|
{
|
||||||
|
$WriteLogMessage.Invoke(0, @("Setting default shell command option to `"$($DefaultShellCommandOption)`". Please Wait..."))
|
||||||
|
$Null = Set-ItemProperty -Path $OpenSSHRegistryPath -Name 'DefaultShellCommandOption' -Value $DefaultShellCommandOption -Type String -Force -ErrorAction SilentlyContinue
|
||||||
|
$WriteLogMessage.Invoke(0, @("Default shell command option configured."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Service Management (Automatic)
|
#region Service Management (Automatic)
|
||||||
$SSHDService = Get-Service -Name 'sshd' -ErrorAction SilentlyContinue
|
$SSHDService = Get-Service -Name 'sshd' -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user