Can't install module as Administrator from script

Hi, everybody and my respect!
Once again I need help with my PoSh perversions)))

I’d like to benefit from using some PoSh module features in my script, so I need to check whether this module is installed in the system, and if not - install it. I don’t know, in advance, what user will run my script since there are two different parts of it: the first script checks the current system config, configs the system as needed, and the other script performes some certain tasks. Thus I don’t want to install the module in the CurrentUser scope and would like to install it for all users.
The solution for this is to start new PoSh process with the verb ‘RunAs’ but we have a web proxy and local administrators do not have permissions to access WWW via it. But my script to implement all this faces a problem.
Here is the patient:

if ('ModuleName' -notin (Get-Module -All).Name) {
    Start-Process powershell -ArgumentList:"-command & {
                                                           if (![System.Net.ServicePointManager]::SecurityProtocol.ToString().Contains('Tls12')) {
                                                               [System.Net.ServicePointManager]::SecurityProtocol += [System.Net.SecurityProtocolType]::Tls12
                                                           }
                                                           `$Args = @{
                                                                        Name = 'CredentialManager'
                                                                        Scope = 'AllUsers'
                                                                        Proxy = (Read-Host -Prompt:'Enter the Web Proxy address: ')
                                                                        ProxyCredential = (Get-Credential -Message:'Provide Web Proxy credentials:')
                                                                        Force = `$$true
                                                                    }
                                                           Install-Module @Args
                                                       }" -Verb runas
} 
else {
    'Yahoo!!!'
}

Running this script ends with warnings and a sudsequent error: “WARNING: Unable to resolve package source ‘https://www.powershellgallery.com/api/v2’.
PackageManagement\Install-Package : No match was found for the specified search criteria and module name…”

Pleeeeease?! What else am I missing?
Thanks a lot in advance!!!

I’d recommend another approach. If you have a software deployment solution in place you should use this to install all needed PowerShell modules on the computers you need them to be installed at. If not - you may install them in the way you install all other software you need to have on your computers.

In the script itself I’d use the #Requires statement

to make sure the script will not run when the needed modules are not available on this computer.

1 Like

Sorry everybody!!!
While going home I realised what was wrong: I’d provided proxy-address just as an ip…))) And should be ‘http://:’
That’s what happens when you’re not a developer and everyone does his best to distract you from coding)))))

Very thanks, Olaf! I recollect that there was something like that… Thanks for you reminded me of this!!!

A common reason for that error is an old version of TLS.

You can check the available versions with:

[System.Net.ServicePointManager]::SecurityProtocol

To enable TLS1.2 for the session you can use this command:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
1 Like

Sorry, but you’ve checked my code not attentively enough))) I make such check in the code… I’ve written higher what was my bad)))

:man_facepalming: yeah, saw the error and jumped to the conclusion without scrolling across and reading all your code.

Anyway, thanks for attention and answering!!! )))