97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test simple WinRM script execution
|
|
Uses very small commands to avoid length limits
|
|
"""
|
|
|
|
import os
|
|
from winrm import Session
|
|
|
|
|
|
def test_simple_script_execution():
|
|
host = os.getenv('HOST', '192.168.88.108')
|
|
username = os.getenv('USER_NAME', 'Admin')
|
|
password = os.getenv('PASS', 'P@ssw0rd!')
|
|
|
|
print(f"Testing simple WinRM script execution on {host}")
|
|
|
|
try:
|
|
# Connect
|
|
session = Session(
|
|
f'http://{host}:5985/wsman',
|
|
auth=(username, password),
|
|
transport='ntlm'
|
|
)
|
|
|
|
# Test 1: Simple inline PowerShell
|
|
print("\n[Test 1] Running simple inline PowerShell:")
|
|
simple_ps = """
|
|
Write-Host "=== Simple WinRM Test ==="
|
|
Write-Host "Computer: $env:COMPUTERNAME"
|
|
Write-Host "User: $env:USERNAME"
|
|
Write-Host "PowerShell: $($PSVersionTable.PSVersion)"
|
|
Write-Host "Time: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
|
|
Write-Host "=== End Test ==="
|
|
"""
|
|
|
|
result = session.run_ps(simple_ps)
|
|
|
|
if result.status_code == 0:
|
|
print("✅ Simple PowerShell execution successful:")
|
|
output = result.std_out.decode('utf-8', errors='replace')
|
|
for line in output.split('\n'):
|
|
if line.strip():
|
|
print(f" {line.strip()}")
|
|
else:
|
|
print(f"❌ Simple PowerShell failed: {result.std_err.decode()}")
|
|
return False
|
|
|
|
# Test 2: Create small script file
|
|
print("\n[Test 2] Creating and executing small script file:")
|
|
|
|
# Create a very small script
|
|
create_script = """
|
|
$scriptContent = @'
|
|
Write-Host "Small script test"
|
|
Get-ComputerInfo -Property CsName, WindowsProductName | Format-List
|
|
Write-Host "Small script completed"
|
|
'@
|
|
|
|
$scriptContent | Out-File -FilePath 'C:\\Users\\Admin\\small_test.ps1' -Encoding UTF8
|
|
Write-Host "Small script file created"
|
|
"""
|
|
|
|
result = session.run_ps(create_script)
|
|
|
|
if result.status_code == 0:
|
|
print("✅ Script file created successfully")
|
|
|
|
# Execute the small script
|
|
exec_result = session.run_cmd('powershell -NoProfile -ExecutionPolicy Bypass -File C:\\Users\\Admin\\small_test.ps1')
|
|
|
|
if exec_result.status_code == 0:
|
|
print("✅ Script execution successful:")
|
|
output = exec_result.std_out.decode('utf-8', errors='replace')
|
|
for line in output.split('\n'):
|
|
if line.strip():
|
|
print(f" {line.strip()}")
|
|
return True
|
|
else:
|
|
print(f"❌ Script execution failed: {exec_result.std_err.decode()}")
|
|
return False
|
|
else:
|
|
print(f"❌ Script creation failed: {result.std_err.decode()}")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"❌ Test failed: {e}")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if test_simple_script_execution():
|
|
print("\n🎉 Simple WinRM script execution works!")
|
|
print("This proves WinRM can execute PowerShell scripts.")
|
|
print("The issue is with large script handling, not basic functionality.")
|
|
else:
|
|
print("\n❌ Basic WinRM functionality failed") |