Tag Archives: powershell

Powershell: Invoke-WebRequest aborts with httpcode 301/308 permanent redirect

Hi,

the powershell command-let Invoke-WebRequest does not follow an http redirect.

The solution is to call Invoke-WebRequest recursivly with the redirect URL from the location header field when http code 301 or 308 is returned.

$sURL = "https://visualstudio.com/"
$sRedirectTo=$null
do {
    $oWebResponse = $null
    try
    {
        $oWebResponse = Invoke-WebRequest $sURL 
    }
    catch 
    {
        $oWebResponse=$_.Exception.Response
        write-host ("Exception: {0}" -f $_.Exception.Message)
    }
	if($oWebResponse.StatusCode -ne 200)
	{
		$aRedirectTo=$oWebResponse.Headers.GetValues("location")
		if($aRedirectTo.Count -ge 1)
		{
			$sRedirectTo=$aRedirectTo[0]
		}
		else
		{
			write-host ("No location URL in header")
			break
		}
		if (-not [string]::IsNullOrEmpty($sRedirectTo))
		{
			write-host ("Redirect to {0}" -f ($sRedirectTo))
			$sURL = $sRedirectTo
		}
		else
		{
			write-host ("location URL Null")
			break
		}
	}
    else
    {
        Write-Output ("OK Location = {1} HttpCode = {0}" -f $oWebResponse.StatusCode,$sRedirectTo )
        break
    }
	
} 
while ($true)

Michael

Powershell: Change language/culture settings for the current session/window.

Hi,

different language settings on your clients causes some administration issues when you are working with times (formats) or group names for example.
Continue reading Powershell: Change language/culture settings for the current session/window.

Powershell: Execution policy shortly explained

Hi,

here is a short explantation about the Powershell execution policies.
Continue reading Powershell: Execution policy shortly explained

Powershell: Install a nupkg module offline

Hi,

the powershell gallery offers a lot of modules for online installation. It’s also possible to download a nupkg file of that module and install it offline.
Continue reading Powershell: Install a nupkg module offline

Powershell: Define a x509 certificate in a script

Hi,

the best way to store a certificate in a powershell script is in an byte array. But how to get such a byte array as string from an existing certificate?
Continue reading Powershell: Define a x509 certificate in a script