forked from sqlrodbloke/sqlrodscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Remote_PS_Connectitvity.ps1
More file actions
38 lines (31 loc) · 1.09 KB
/
Test_Remote_PS_Connectitvity.ps1
File metadata and controls
38 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
param(
[Parameter(Mandatory=$true)]
[string]$ComputerName
)
Write-Host "Testing WinRM connectivity to $ComputerName..." -ForegroundColor Cyan
# Step 1: Test WinRM service
try {
Test-WSMan -ComputerName $ComputerName -ErrorAction Stop
Write-Host "WinRM service is responding." -ForegroundColor Green
}
catch {
Write-Host "WinRM test failed: $($_.Exception.Message)" -ForegroundColor Red
exit
}
# Step 2: Attempt remote session
Write-Host "`nAttempting remote PowerShell session..." -ForegroundColor Cyan
try {
$session = New-PSSession -ComputerName $ComputerName -ErrorAction Stop
Write-Host "Session created successfully." -ForegroundColor Green
# Step 3: Run a simple command remotely
$result = Invoke-Command -Session $session -ScriptBlock {
hostname
}
Write-Host "`nRemote command executed successfully." -ForegroundColor Green
Write-Host "Remote Hostname: $result"
# Cleanup
Remove-PSSession $session
}
catch {
Write-Host "Remote session failed: $($_.Exception.Message)" -ForegroundColor Red
}