diff --git a/Invoke-OpenSSHConfiguration.ps1 b/Invoke-OpenSSHConfiguration.ps1 index 814545f..d26de00 100644 --- a/Invoke-OpenSSHConfiguration.ps1 +++ b/Invoke-OpenSSHConfiguration.ps1 @@ -928,6 +928,81 @@ Switch (Test-ProcessElevationStatus) } #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) $SSHDService = Get-Service -Name 'sshd' -ErrorAction SilentlyContinue