o365soa / soa

Public SOA modules and information
39 stars 18 forks source link

The functions that checks multiple PowerShell sessions running stops this SOAPrerequisites script from running #43

Closed BlurredBlue closed 1 year ago

BlurredBlue commented 2 years ago

Hi! It was my first time to run the SOAPrerequisite script. In our servers we have a GPO that blocks basic auth. Basic auth is needed by this ExchangeOnline which this script connects to. Basic auth was enabled back in the GPO so the script could run.

Soon after, the SOAPrerequisite script started showing this error: "There are multiple PowerShell windows open. This can cause issues with PowerShell modules being loaded, blocking uninstallation and updates. Close all open PowerShell windows and start with a clean PowerShell window running as Administrator."

I checked task manager if there are other PS sessions being run on the server - none. I used PS commands like Get-PSsession | Remove-PSsession, exit-PSSession. Restarted the server many times.

But each time, I ran the script, I get the same "multiple PoweShell windows open" error.

I reviewed task manager - when Powershell is started, there is a console (CMD) session and the actual PowerShell session.

It appears that enabling basic auth creates 2 sessions of PowerShell - one in legacy mode and one in the latest PowerShell mode.

I reviewed the script and commented out the lines of code that gets a count of PowerShell sessions and the warning associated with it, saved the script and run it - everything worked fine.

This is a short term solution for me so I can run the script - I am sure there is a better way to update the script.

Lines of code I commented out to make the script work in a host that has basic auth enabled:

Function Get-PowerShellCount { <# Returns count of PowerShell windows opened

>

$Processes = Get-Process -Name PowerShell
Return $Processes.Count

}

Check administrator and multiple PowerShell windows

If($(Get-IsAdministrator) -eq $False -and $ModuleCheck -eq $True -and $DoNotRemediate -eq $false) {
    Throw "PowerShell must be run as Administrator in order to allow for any changes when running Install-SOAPrerequisites"
}
If($AllowMultipleWindows) {
    Write-Important
    Write-Host "Allow multiple windows has been specified. This should not be used in general operation. Module remediation may fail!"
} 
Else 
{
    If($(Get-PowerShellCount) -gt 1 -and $ModuleCheck -eq $True -and $DoNotRemediate -eq $false) {
        Throw "There are multiple PowerShell windows open. This can cause issues with PowerShell modules being loaded, blocking uninstallation and updates. Close all open PowerShell windows and start with a clean PowerShell window running as Administrator."
    }

Thank you.

BlurredBlue

sbueffel commented 1 year ago

Multiple windows will keep a module from being uninstalled if the module is loaded in the other window. (If it isn't loaded in the other window or no modules need to be uninstalled, there will be no issue.) There is a switch, AllowMultipleWindows, that can be used to continue when multiple PS processed are running.

Beginning with 2.8.3, the prerequisites installation no longer removes multiple versions of a module, so multiple windows are allowed.