File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ param (
2+ [Parameter (Mandatory = $true )]
3+ [string ]$ComputerName
4+ )
5+
6+ Write-Host " Testing WinRM connectivity to $ComputerName ..." - ForegroundColor Cyan
7+
8+ # Step 1: Test WinRM service
9+ try {
10+ Test-WSMan - ComputerName $ComputerName - ErrorAction Stop
11+ Write-Host " WinRM service is responding." - ForegroundColor Green
12+ }
13+ catch {
14+ Write-Host " WinRM test failed: $ ( $_.Exception.Message ) " - ForegroundColor Red
15+ exit
16+ }
17+
18+ # Step 2: Attempt remote session
19+ Write-Host " `n Attempting remote PowerShell session..." - ForegroundColor Cyan
20+
21+ try {
22+ $session = New-PSSession - ComputerName $ComputerName - ErrorAction Stop
23+ Write-Host " Session created successfully." - ForegroundColor Green
24+
25+ # Step 3: Run a simple command remotely
26+ $result = Invoke-Command - Session $session - ScriptBlock {
27+ hostname
28+ }
29+
30+ Write-Host " `n Remote command executed successfully." - ForegroundColor Green
31+ Write-Host " Remote Hostname: $result "
32+
33+ # Cleanup
34+ Remove-PSSession $session
35+ }
36+ catch {
37+ Write-Host " Remote session failed: $ ( $_.Exception.Message ) " - ForegroundColor Red
38+ }
You can’t perform that action at this time.
0 commit comments