Tag Archives: powershell

Powershell: Encrypt and decrypt strings with the host key

Hi,

each Windows machine has it’s own host key. This can be used to encrypt data which are only decrypted by this computer.

Continue reading Powershell: Encrypt and decrypt strings with the host key

Powershell: Convert 18-digit LDAP/Win32 Epoch timestamp to DateTime object

Hi,

LDAP use a 18 digit timestamp format. It’s an epoch timestamp.
Continue reading Powershell: Convert 18-digit LDAP/Win32 Epoch timestamp to DateTime object

Powershell: Get the certificate of a webserver

Hi,

like openssl s_client you can also use powershell to view/get the certificate of a webserver.
Continue reading Powershell: Get the certificate of a webserver

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$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.