-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-PCsIn.ps1
More file actions
74 lines (66 loc) · 2.28 KB
/
Get-PCsIn.ps1
File metadata and controls
74 lines (66 loc) · 2.28 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
function get-pcsin{
<#
.Description
Retrives hosts running Windows operating systems in a specified network range
.Parameter
Network start address. Normally starts at .0 of the network range.
.Example
Get-pcsIn -Network 192.168.1.0
Scans from 192.168.1.0 to 192.168.1.254
#>
param ($network)
Begin{}
Process{
#declare arrays for storing information
$pcs = @()
$printers = @()
$vmhosts = @()
#declare variables and initialize counters
$previous = 0
$last = 1
$ip = $network
#declare Regular Expression to match WMI OS Caption for pcs
$regex = [Regex]"Windows*"
Write-Host "Scanning network $ip..."
#loop through IP addresses in network up to .254
while ($previous -lt 255){
Write-Verbose ("{0}: Checking network availability" -f $ip)
#show progress bar and current IP address for progress of network scan
Write-Progress -Activity "Scanning network" -status "Scanning $ip" -percentComplete ($last/255*100)
#if a response is received on the IP retrieve WMI information
If (Test-Connection -ComputerName $ip -Count 1 -Quiet) {
Try{
#get the operating system info from WMI
$OS = Get-WmiObject -ComputerName $ip Win32_OperatingSystem -ErrorAction Stop
} Catch {
$OS = New-Object PSObject -Property @{
Caption = $_.Exception.Message
Version = $_.Exception.Message
}
}
#check to see if the OS is Windows Server
if ($OS.caption -match $regex){
#create a server object containing properites of the host
$object = New-Object PSObject -Property @{
IPaddr = $ip
Name = $OS.__SERVER
OSVersion = $os.caption
}
#add the server object to the array for computers
$pcs += $object
Write-Host "Windows machine found at $ip..."
}
}
#update the 'current' IP address
$ip = $ip -replace "$previous$", "$last"
#increment counters
$last++
$previous++
}
Write-Host "Scanning completed."
#write arrays of information to files
$pcs| Export-CSV C:\_netscanreports\pcs.csv -notypeinformation
}
}
$net = Read-Host "Enter a network in the form of 192.168.1.0:"
get-pcsin $net