Files
win-remote-toolkit/scripts/ssh/test-winrm-connection.sh
2026-04-24 17:44:02 +08:00

99 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
HOST="${HOST:-192.168.88.108}"
USER_NAME="${USER_NAME:-Admin}"
PASS="${PASS:-P@ssw0rd!}"
echo "=== Testing WinRM Connection to $HOST ==="
echo ""
# Test 1: Check if PowerShell Core is installed
echo "[1/4] Checking PowerShell Core availability..."
if command -v pwsh >/dev/null 2>&1; then
echo "✅ PowerShell Core found: $(pwsh --version)"
else
echo "❌ PowerShell Core not found"
echo "Install with: brew install powershell"
exit 1
fi
# Test 2: Test basic network connectivity to WinRM port
echo ""
echo "[2/4] Testing network connectivity to WinRM port 5985..."
if nc -z "$HOST" 5985; then
echo "✅ Port 5985 is reachable"
else
echo "❌ Cannot reach port 5985 on $HOST"
exit 1
fi
# Test 3: Test HTTP WinRM endpoint with curl
echo ""
echo "[3/4] Testing WinRM HTTP endpoint..."
WINRM_RESPONSE=$(curl -s -w "%{http_code}" -o /dev/null --max-time 10 \
"http://$HOST:5985/wsman" \
-H "Content-Type: application/soap+xml;charset=UTF-8" \
-H "SOAPAction: http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate" \
--data '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header/><soap:Body><wsen:Enumerate xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration"><wsen:Filter xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">SELECT * FROM Win32_Service</wsen:Filter></wsen:Enumerate></soap:Body></soap:Envelope>' 2>/dev/null || echo "000")
if [ "$WINRM_RESPONSE" = "401" ]; then
echo "✅ WinRM endpoint responding (401 = authentication required, expected)"
elif [ "$WINRM_RESPONSE" = "500" ]; then
echo "✅ WinRM endpoint responding (500 = server error, but WinRM is there)"
else
echo "⚠️ WinRM endpoint response: $WINRM_RESPONSE (may still work with PowerShell)"
fi
# Test 4: Test PowerShell remoting
echo ""
echo "[4/4] Testing PowerShell remoting session..."
pwsh -c "
try {
\$secpass = ConvertTo-SecureString '$PASS' -AsPlainText -Force
\$cred = New-Object System.Management.Automation.PSCredential('$USER_NAME', \$secpass)
Write-Host 'Attempting to create PowerShell session...'
\$sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck
\$session = New-PSSession -ComputerName '$HOST' -Port 5985 -Credential \$cred -UseSSL:\$false -SessionOption \$sessionOption -ErrorAction Stop
Write-Host '✅ PowerShell remoting session created successfully'
# Test running a simple command
\$result = Invoke-Command -Session \$session -ScriptBlock {
@{
ComputerName = \$env:COMPUTERNAME
UserName = \$env:USERNAME
PSVersion = \$PSVersionTable.PSVersion.ToString()
CurrentTime = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
}
}
Write-Host 'Remote session info:'
Write-Host ' Computer:' \$result.ComputerName
Write-Host ' User:' \$result.UserName
Write-Host ' PowerShell:' \$result.PSVersion
Write-Host ' Time:' \$result.CurrentTime
Remove-PSSession \$session
Write-Host '✅ Session closed successfully'
} catch {
Write-Host '❌ PowerShell remoting failed:' \$_.Exception.Message
Write-Host ''
Write-Host 'Common causes:'
Write-Host ' - AllowUnencrypted=false (current setting on target)'
Write-Host ' - Firewall blocking connections'
Write-Host ' - Network connectivity issues'
Write-Host ' - Authentication problems'
exit 1
}
"
echo ""
echo "🎉 All WinRM connectivity tests passed!"
echo ""
echo "WinRM is ready for use. You can now:"
echo " 1. Create WinRM-based versions of existing scripts"
echo " 2. Use PowerShell remoting for direct command execution"
echo " 3. Compare performance with SSH-based approach"