Files
win-remote-toolkit/scripts/winrm/generate_winrm_wrappers.py
2026-04-24 17:44:02 +08:00

154 lines
5.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Generate WinRM wrapper scripts for existing PowerShell scripts
Creates Python WinRM alternatives to SSH-based scripts
"""
import os
import sys
from pathlib import Path
import json
def create_winrm_wrapper(ps_script_name, description=""):
"""Create a Python WinRM wrapper for a PowerShell script"""
script_name_base = ps_script_name.replace('.ps1', '')
wrapper_name = f"{script_name_base}-winrm.py"
template = f'''#!/usr/bin/env python3
"""
WinRM version of {ps_script_name}
{description}
"""
import os
import sys
from winrm_executor import WinRMExecutor
from pathlib import Path
def main():
# Get connection details
host = os.getenv('HOST', '192.168.88.108')
username = os.getenv('USER_NAME', 'Admin')
password = os.getenv('PASS', 'P@ssw0rd!')
print(f"=== WinRM Execution: {{username}}@{{host}} - {ps_script_name} ===")
# Create WinRM executor
executor = WinRMExecutor(host, username, password)
try:
# Test connection
print("[*] Establishing WinRM connection...")
if not executor.connect():
print("❌ Failed to establish WinRM connection")
print("\\nTroubleshooting:")
print(" 1. Ensure target machine is powered on and reachable")
print(" 2. Verify WinRM service is running")
print(" 3. Check firewall and network connectivity")
sys.exit(1)
print("✅ WinRM connection established")
# Execute PowerShell script
script_path = Path(__file__).parent / "{ps_script_name}"
if not script_path.exists():
print(f"❌ PowerShell script not found: {{script_path}}")
sys.exit(1)
print(f"[*] Executing {ps_script_name} via WinRM...")
print("=" * 60)
success = executor.upload_and_execute_script(script_path)
print("=" * 60)
if success:
print(f"🎉 {{script_path.name}} completed successfully via WinRM!")
else:
print(f"{{script_path.name}} failed")
sys.exit(1)
finally:
executor.close()
if __name__ == "__main__":
main()
'''
return wrapper_name, template
def main():
current_dir = Path(__file__).parent
# List of PowerShell scripts and their descriptions
scripts = {
'recon.ps1': 'System reconnaissance and inventory',
'activate-windows.ps1': 'Activate Windows using KMS method',
'remove-windows-ai.ps1': 'Remove Windows AI components',
'win11debloat.ps1': 'Remove Windows bloatware and disable telemetry',
'verify-ai-removed.ps1': 'Verify Windows AI removal',
'verify-debloat.ps1': 'Verify Windows debloating changes',
'install-thunderbird.ps1': 'Install Thunderbird email client',
'setup-thunderbird.ps1': 'Configure Thunderbird IMAP/SMTP',
'install-telegram.ps1': 'Install Telegram Desktop',
'install-nirsoft.ps1': 'Install NirSoft diagnostic tools',
'nirsoft-dump.ps1': 'Collect system diagnostic data',
'firewall-allow-ssh.ps1': 'Configure firewall for SSH access',
'apply-ui-to-user.ps1': 'Apply UI settings to user',
'configure-ime.ps1': 'Configure input method settings',
'rename-hide-accounts.ps1': 'Rename and hide user accounts',
'install-cports.ps1': 'Install CurrPorts network tool',
'remove-apps.ps1': 'Remove specified applications'
}
print("=== Generating WinRM Wrapper Scripts ===")
print()
created_count = 0
for ps_script, description in scripts.items():
ps_path = current_dir / ps_script
if ps_path.exists():
wrapper_name, wrapper_content = create_winrm_wrapper(ps_script, description)
wrapper_path = current_dir / wrapper_name
# Only create if it doesn't exist or user confirms overwrite
if wrapper_path.exists():
response = input(f"⚠️ {wrapper_name} exists. Overwrite? [y/N]: ")
if response.lower() != 'y':
print(f"⏭️ Skipping {wrapper_name}")
continue
with open(wrapper_path, 'w', encoding='utf-8') as f:
f.write(wrapper_content)
# Make executable
wrapper_path.chmod(0o755)
print(f"✅ Created: {wrapper_name}")
created_count += 1
else:
print(f"⚠️ PowerShell script not found: {ps_script}")
print()
print(f"🎉 Generated {created_count} WinRM wrapper scripts")
print()
print("Usage examples:")
print(" python3 recon-winrm.py")
print(" HOST=pc-74269 python3 install-thunderbird-winrm.py")
print(" USER_NAME=SomeUser python3 apply-ui-to-user-winrm.py")
print()
print("Benefits of WinRM approach:")
print(" ✅ Native Windows remote management")
print(" ✅ Better PowerShell integration")
print(" ✅ Structured error handling")
print(" ✅ Automatic UTF-8 BOM handling for Chinese Windows")
print(" ✅ Detailed logging and progress reporting")
if __name__ == "__main__":
main()