I have the following script to perform an availability check for a password-protected localhost site:
Function CheckAvailability ($Url, $Credentials)
{
$WebClient = New-Object System.Net.WebClient
[System.Net.CredentialCache]$CredentialCache = New-Object System.Net.CredentialCache
$CredentialCache.Add($Url, "Basic", $Credentials)
$WebClient.Credentials = $CredentialCache
If ($WebClient.DownloadString($Url))
{
Return $True
}
Else
{
Return $False
}
}
Function Main ()
{
$Url = "http://localhost/resource"
$Credentials = New-Object System.Net.NetworkCredential("admin","password","") #UserName, Password, Domain
CheckAvailability($Url, $Credentials)
}
Main
If I run the script, I get the following error
Cannot find an overload for "Add" and the argument count: "3". At D:\WebSiteTest.ps1 + $CredentialCache.Add($Url, "Basic", $Credentials) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest
I've read that the method add and it should support 3 or 4 parameters (https://learn.microsoft.com/en-us/dotnet/api/system.net.credentialcache.add?view=netframework-4.8). Can someone tell me, what the issue is here? I am running Powershell Core 6.2.3.
I appreciate any suggestions.