-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ServerInventory.ps1
151 lines (117 loc) · 5.35 KB
/
Get-ServerInventory.ps1
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Get a list of servers in the domain
$Allservers = Get-ADComputer -Filter {OperatingSystem -Like "*server*"} -Property Name | Select-Object -ExpandProperty Name
$ErrorPath = $PSScriptRoot
$failedDNSServers = @()
$servers = @()
foreach ($server in $Allservers) {
if (-not (Test-Connection -ComputerName $server -Count 1 -Quiet)) {
$failedDNSServers += $server
}
else {
$servers += $server
}
}
$creds = get-credential
$failedM1Servers = @()
$failedM2Servers = @()
# Iterate through each server
foreach ($server in $servers) {
# Create a script block for each server
$scriptBlock = {
# Get the computer name
$computerName = $env:COMPUTERNAME
# Get the IP address
$ipAddress = ((Test-Connection -ComputerName $computerName -Count 1).IPv4Address.IPAddressToString)
# Get the DNS server addresses
$dnsServers = Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses
# Specify the centralized network share path
$centralizedPath = "*Insert Network Share Here*"
# Get the operating system version
$osVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version
# Get the list of installed software
$installedSoftware = Get-WmiObject -Class Win32_Product | Select-Object Name, Version
# Create a custom object with computer name, OS version, and installed software
$result = foreach ($software in $installedSoftware) {
[PSCustomObject]@{
"SoftwareName" = $software.Name
"SoftwareVersion" = $software.Version
}
}
# Get CPU usage percentage
$cpuUsage = Get-WmiObject -Class Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average
# Get CPU information including sockets and cores
$cpus = Get-WmiObject -Class Win32_Processor
$cpuSockets = $cpus | Select-Object -Property SocketDesignation -Unique
$cpuCores = $cpus | Measure-Object -Property NumberOfCores -Sum | Select-Object -ExpandProperty Sum
# Get memory information
$memory = Get-WmiObject -Class Win32_OperatingSystem
$memoryTotal = $memory.TotalVisibleMemorySize
$memoryFree = $memory.FreePhysicalMemory
# Get disk information
$disks = Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | Select-Object DeviceID, Size, FreeSpace
$diskUsage = foreach ($disk in $disks) {
$diskID = $disk.DeviceID
$diskSize = $disk.Size
$diskFree = $disk.FreeSpace
$diskFreePercentage = ($diskFree / $diskSize) * 100
[PSCustomObject]@{
DeviceID = $diskID
FreePercentage = $diskFreePercentage
}
}
$systemReport = [PSCustomObject]@{
"CPUUsage" = $cpuUsage
"CPUSockets" = $cpuSockets.Count
"CPUCores" = $cpuCores
"MemoryTotal" = $memoryTotal
"MemoryFree" = $memoryFree
"DiskUsage" = $diskUsage
"ComputerName" = $computerName
"IPAddress" = $ipAddress
"DNSServers" = $dnsServers -join ","
"OSVersion" = $osVersion
}
$firewallStatus = Get-NetFirewallProfile
$sysevents = Get-WinEvent -LogName System -FilterXPath "*[System[(Level=2)]]" -MaxEvents 100
$appevents = Get-WinEvent -LogName Application -FilterXPath "*[System[(Level=2)]]" -MaxEvents 100
#Export
New-Item -ItemType Directory -Path "$centralizedPath\$computerName"
$systemReport | Export-Csv -Path "$centralizedPath\$computerName\System.csv" -NoTypeInformation
$sysevents | Export-Csv -Path "$centralizedPath\$computerName\SystemEvents.csv" -NoTypeInformation
$appevents | Export-Csv -Path "$centralizedPath\$computerName\ApplicationEvents.csv" -NoTypeInformation
$firewallStatus | Out-File -FilePath "$centralizedPath\$computerName\firewall_status.txt"
$result | Export-Csv -Path "$centralizedPath\$computerName\InstalledSoftware.csv" -NoTypeInformation
# Output a success message
Write-Host "Installed software list exported to $centralizedPath\$computerName\"
}
#Method 1
try {
Invoke-Command -ComputerName $server -ScriptBlock $scriptBlock -Credential $creds -ErrorAction Stop
}
Catch {
Write-host "Error connecting to $server "
$failedM1Servers += [PSCustomObject]@{
Server = $server
Error = $_.Exception.Message
}
}
#Method2
try {
Invoke-CommandAs -ComputerName $server -ScriptBlock $scriptBlock -Credential $creds -AsUser $creds -ErrorAction Stop
}
Catch {
Write-host "Error connecting to $server "
$failedM2Servers += [PSCustomObject]@{
Server = $server
Error = $_.Exception.Message
}
}
}
# Specify the path and file name for the output file
$exportM1Path = "$ErrorPath\failed_M1_servers.txt"
$exportM2Path = "$ErrorPath\failed_M2_servers.txt"
$exportDNSPath = "$ErrorPath\failed_DNS_servers.txt"
# Write the failed servers and their error messages to the output file
$failedM1Servers | Export-Csv -Path $exportM1Path -NoTypeInformation
$failedM2Servers | Export-Csv -Path $exportM2Path -NoTypeInformation
$failedDNSServers | Export-Csv -Path $exportDNSPath -NoTypeInformation