Difference between revisions of "Winrm https basic auth with pywinrm"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "``` # Configure WinRM to use HTTPS and enable basic authentication Enable-PSRemoting -Force -SkipNetworkProfileCheck Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true...")
 
Line 1: Line 1:
 +
# WINRM Simple Example Using HTTPS/TLS & Basic Auth
 +
 +
Note we are creating self signed certificate called localhost & ignoring TLS cert validation for simplicity in this example. You do not want to do this in production. Since we aren't using encrypted messaging via NTLM/Kerberos we want to make sure AllowUnencrypted is set to false so TLS/HTTPS transport is required
 +
 +
## Simple setup
 +
 +
###
 
```
 
```
 
# Configure WinRM to use HTTPS and enable basic authentication
 
# Configure WinRM to use HTTPS and enable basic authentication
Line 15: Line 22:
 
Restart-Service WinRM
 
Restart-Service WinRM
  
 +
```
 +
 +
### Simple script to run hostname command via winrm https 5986
 +
```
 +
#!/usr/bin/env python3
 +
import winrm
 +
 +
destination = 'https://10.x.x.x:5986'
 +
username = 'myuser'
 +
password = 'mysecret'
 +
 +
session = winrm.Session(destination,
 +
                        auth=(username, password),
 +
                        # transport='certificate',
 +
                        transport='ssl',
 +
                        server_cert_validation='ignore'
 +
    )
 +
 +
result = session.run_ps('hostname')
 +
print(result.std_out)
 
```
 
```

Revision as of 17:02, 14 February 2024

WINRM Simple Example Using HTTPS/TLS & Basic Auth

Note we are creating self signed certificate called localhost & ignoring TLS cert validation for simplicity in this example. You do not want to do this in production. Since we aren't using encrypted messaging via NTLM/Kerberos we want to make sure AllowUnencrypted is set to false so TLS/HTTPS transport is required

Simple setup

#

# Configure WinRM to use HTTPS and enable basic authentication
Enable-PSRemoting -Force -SkipNetworkProfileCheck
Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true
Set-Item WSMan:\localhost\Service\AllowUnencrypted -Value $false

# Create a self-signed certificate (replace with a valid certificate in production)
$cert = New-SelfSignedCertificate -DnsName "localhost" -CertStoreLocation cert:\LocalMachine\My
$thumbprint = $cert.Thumbprint

# Configure WinRM listener to use HTTPS and the created certificate
New-Item -Path WSMan:\localhost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $thumbprint -Force

# Restart WinRM service to apply changes
Restart-Service WinRM

Simple script to run hostname command via winrm https 5986

#!/usr/bin/env python3
import winrm

destination = 'https://10.x.x.x:5986'
username = 'myuser'
password = 'mysecret'

session = winrm.Session(destination,
                        auth=(username, password),
                        # transport='certificate',
                        transport='ssl',
                        server_cert_validation='ignore'
    )

result = session.run_ps('hostname')
print(result.std_out)