-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ComputerOnlineStatus.ps1
More file actions
84 lines (71 loc) · 3.13 KB
/
Copy pathGet-ComputerOnlineStatus.ps1
File metadata and controls
84 lines (71 loc) · 3.13 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNOPSIS
Prüft den Online-Status von Computern aus Active Directory und listet deren IP-Adressen auf.
.DESCRIPTION
Dieses Skript durchsucht Active Directory nach Computern, prüft deren Erreichbarkeit mittels Ping und löst deren IP-Adressen auf.
Es unterscheidet dabei zwischen Servern und Clients und ermöglicht die Anzeige zusätzlicher Informationen wie die Beschreibung aus AD.
.PARAMETER Filter
Der LDAP-Filter, der verwendet wird, um Computer aus Active Directory zu suchen. Standardmäßig werden alle Windows-Computer gesucht.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[string]$Filter = "OperatingSystem -like '*Windows*'",
[int]$ThrottleLimit = 30
)
$null = & ipconfig /flushdns
# Konstante steuert tatsächlichen Neustart
$DoWhatIf = $true # $true = nur Simulation / $false = echter Neustart
$OnlineServer = [System.Collections.Concurrent.ConcurrentBag[object]]::new()
# Server finden
# $Server = Get-ADComputer -Filter {OperatingSystem -like "*Server*"} -Properties OperatingSystem | Where Name -notlike "*cx*" |
# Select-Object -ExpandProperty Name
# Clients finden
# $Server = Get-ADComputer -Filter {OperatingSystem -notlike "*Server*"} -Properties OperatingSystem | Where Name -notlike "*cx*" |
# Select-Object -ExpandProperty Name
# Server CX finden
# $Server = Get-ADComputer -Filter {OperatingSystem -like "*Server*"} -Properties OperatingSystem | Where Name -like "*cx*" |
# Select-Object -ExpandProperty Name
# Alle
$Server = Get-ADComputer -Filter { OperatingSystem -like "*Windows*" } -Properties OperatingSystem |
Select-Object -ExpandProperty Name
# Parallel prüfen und ggf. neustarten
$Server | ForEach-Object -Parallel {
$Name = $_
$DoWhatIf = $using:DoWhatIf
$OnlineServer = $using:OnlineServer
if (Test-Connection -ComputerName $Name -Count 1 -TimeoutSeconds 1 -Quiet) {
try {
$IP = (Resolve-DnsName -Name $Name -ErrorAction Stop | Where-Object { $_.Type -eq 'A' } | Select-Object -First 1 -ExpandProperty IPAddress)
}
catch {
$IP = "unbekannt"
}
$OnlineServer.Add([pscustomobject]@{
Server = $Name
IPAddress = $IP
Timestamp = (Get-Date)
Online = $true
})
Write-Host "$Name ($IP) ist online."
# Invoke-Command -ComputerName $Name -ScriptBlock {
# param($DoWhatIf)
# Restart-Computer -Force -WhatIf:$DoWhatIf
# } -ArgumentList $DoWhatIf
}
else {
Write-Host "$Name ist offline."
$OnlineServer.Add([pscustomobject]@{
Server = $Name
IPAddress = $IP
Timestamp = (Get-Date)
Online = $false
})
}
} -ThrottleLimit 30
# Ausgabe
# Online
$Online = $OnlineServer | Where-Object Online -eq $true | Sort-Object Server
# Offline
$Offline = $OnlineServer | Where-Object Online -eq $false | Sort-Object Server
$Online | Out-GridView -Title "Online Computer am $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
$Offline | Out-GridView -Title "Offline Computer am $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"