Powershell will not allow ServerCertificateValidationCallback change - powershell

I'm try to make a call with Invoke-RestMethod but the server I'm connecting to has a self-signed certificate.
After some searching I found that I need to run the following to make it work:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = ${true}
I did this a couple of days ago and then my Invoke-RestMethod call started working.
I then rebooted my server and tried to execute the same again but now I get the following error:
PS C:\Users\Administrator> [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $True
Exception setting "ServerCertificateValidationCallback": "Cannot convert value "True" to type
"System.Net.Security.RemoteCertificateValidationCallback". Error: "Invalid cast from 'System.Boolean' to
'System.Net.Security.RemoteCertificateValidationCallback'.""
At line:1 char:1
+ [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Blockquote
I've tried setting my execution policy to unrestricted but I still have the same problem.
I'm running on Windows Server 2012 with PSVersion 3.0.
I've no idea why it worked before. I'm pretty sure there was nothing special i had done previously.
I would appreciate any suggestions!

I finally found a website that had the following command:
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Note that 'System.' is missing.
Running this command worked as expected.

Related

Powershell version 2 Configuration System Failed to Initialize

I am creating a script using PowerShell to make a http request to an endpoint. I know starting from PowerShell version 3, I can use Invoke-RestMethod or Invoke-WebRequest, but the script is required to be compatible with PowerShell version 2.
As I have googled and read many forums, the most common solution is to use the .Net class System.Net.WebRequest. I am executing the below lines:
$Uri = "https://url"
$Web = [System.Net.WebRequest]::Create($Uri)
And I am facing below error:
Create : Exception calling "Create" with "1" argument(s): "Configuration system failed to initialize"
At line:1 char:32
+ [System.Net.WebRequest]::Create <<<< ($Uri)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Can you please help what might caused this problem?

Connect-MsolService : An error occurred while making the HTTP request to https://provisioningapi.microsoftonline.com/provisioningwebservice.svc

Why I'm getting this error randomly .,Powershell script sometimes working properly sometime not working throwing below error.
Connect-MsolService : An error occurred while making the HTTP request to
https://provisioningapi.microsoftonline.com/provisioningwebservice.svc. This could be due to the fact that the server
certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the
security binding between the client and the server.
At D:\O365License\Licensing.ps1:28 char:1
+ Connect-MsolService -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], CommunicationException
+ FullyQualifiedErrorId :
System.ServiceModel.CommunicationException,Microsoft.Online.Administration.Automation.ConnectMsolService
You might be suffering from a .NET related issue with TLS 1.2. Try running this before you run the Connect-MsolService command.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

error when trying to install chocolatey via powershell : cannot connect to remote server

I am trying to install chocolatey by following official documentation (https://chocolatey.org/install), this is what I am doing :
1-I am opening a cmd.exe running as administrator
2-I am running the following command : powershell and then switch into powershell mode
3-I am running the following command : $PSVersionTable.PSVersion and then get the following result : major:5 minor:1 build:16299 revision:785
4-I am running the following command : Get-ExecutionPolicy and then get the following result : RemoteSigned
5-I am running the following command : Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('http
s://chocolatey.org/install.ps1')) and get the following error :
Exception calling "DownloadString" with "1" argument(s): "Unable to connect to the remote server"
At line:1 char:51
+ ... ess -Force; iex ((New-Object System.Net.WebClient).DownloadString('ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I am working in a company (our os is windows 10 enterprise), and when trying or any colleague machine it's working perfectly and there is no (from what we have seen) any configuration difference between them and me.
Thanks in advance
There is nothing wrong with the code you tried. Basically, it is telling you that you are unable to connect, which might be a restriction or policy. Try the following:
$req = [System.Net.HttpWebRequest]::Create("https://www.google.com")
$req.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$req.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Then run the comment again. It has something to do with the credentials used in windows 10 for the request.

DownloadString Path error

I'm using the Invoke-Expression cmdlet in PowerShell to load modules.
The following code works as intended.
$url="http://pastebin.com/raw/FuxtpN69"
IEX (New-Object System.Net.Webclient).DownloadString($url)
But when I try using variables to split the domain and the page.
$u="http://pastebin.com"
$rl="/raw/FuxtpN69"
$url="$u$rl"
IEX (New-Object System.Net.Webclient).DownloadString($url)
I get the following path error:
Exception calling "DownloadString" with "1" argument(s): "The given path's format is not supported."
At line:8 char:53
+ IEX (New-Object System.Net.Webclient).DownloadString <<<< ($url)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Can someone help me? Thanks in advance.
I think you are running PowerShell as a different account (for example an administrator).
Maybe that account's proxy settings are different that your normal account's?
See the credentials part first,
$webClient.UseDefaultCredentials = $true
Then, Try setting
$client.Proxy = $null
before downloading and see if it helps.

How can I run an Azure powershell cmdlet through a proxy server with credentials?

When I run the following Powershell cmdlet (from the Azure Management Tools Snapin):
get-osversions -subscriptionId **** -certificate (get-item cert:\CurrentUser\MY\******)
I get the following error message:
Get-OSVersions : The remote server returned an unexpected response: (407) Proxy Authenti
cation Required.
At line:1 char:15
+ get-osversions <<<< -subscriptionId * -certificate
(get-item cert:\CurrentUser\MY*****)
+ CategoryInfo : CloseError: (:) [Get-OSVersions], ProtocolException
+ FullyQualifiedErrorId : Microsoft.Samples.AzureManagementTools.PowerShell.HostedS
ervices.GetOSVersionsCommand
Get-OSVersions : Object reference not set to an instance of an object.
At line:1 char:15
+ get-osversions <<<< -subscriptionId * -certificate
(get-item cert:\CurrentUser\MY***)
+ CategoryInfo : CloseError: (:) [Get-OSVersions], NullReferenceException
+ FullyQualifiedErrorId : Microsoft.Samples.AzureManagementTools.PowerShell.HostedS
ervices.GetOSVersionsCommand
It seems that the internet proxy server here is denying the script the access it requires.
I've had a good look around on the internet and it seems that there is no easy way around this problem since this cmdlet does not have a valid "-credentials" or proxy server parameter.
I know there is a 'Get-Credential' cmdlet but I don't think it helps. How would you pass the credential to the Azure cmdlet?
Can anyone think of any way to get around this issue?..
...other than using a different non-proxied internet connection?
I'm stumped.
Many Thanks for your time.
Easier:
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
A few customers I know were successful using the method outlined here (Supporting Basic Auth proxies). If you need other proxy types, it follows the same pattern. The nice thing about this is it does not require changing the cmdlets.