-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract-WorkstationsBasedOnLastNumber.ps1
148 lines (99 loc) · 4.58 KB
/
Extract-WorkstationsBasedOnLastNumber.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
#############################################################################
#
# Extract-WorkstationsBasedOnLastNumber
#
# by Steven Wight 19/11/2021
#
#############################################################################
####################################
# Initialising Variables etc..
####################################
#Grab AD Module
Import-Module ActiveDirectory
#Set Domain for AD
$Domain = "POSHYT"
#initialise Error Counter
$ErrorCount = 0
#initialise number array
$Numbers = @(9,8,7,6,5,4,3,2,1,0)
#initialise Letter array
$MachineTypeLetters = @('S','D','P','V')
#Set Naming convention filter string
$NamingConventionFilter = "POSHYTUK"
#set output path folder
$OutputFolder = "\\POSHYTUK0123S.POSHYT.corp\PowerShell_Scripts\Output"
####################################
# Functions
####################################
Function ErrorCountCheck{ # Remind me if something went wrong encase I come back with cup of tea and error has moved off console...
If ($ErrorCount -ge 1) { #Notify if there has been errors (will be apparent if there was tho)
Write-host -ForegroundColor Red "####################################################"
Write-Host -ForegroundColor Red "# The script execution completed, but with errors. #"
Write-host -ForegroundColor Red "####################################################"
Pause
}Else{
Write-host -ForegroundColor Green "#############################################"
Write-Host -ForegroundColor Green "# Script execution completed without error. #"
Write-host -ForegroundColor Green "#############################################"
Write-host ""
Pause
}
}
Function IAonError{ # if a try/catch goes man down s**tpants
Write-host -ForegroundColor Red "#########"
Write-host -ForegroundColor Red "# ERROR #"
Write-host -ForegroundColor Red "#########"
Write-host ""
#Increment the error counter
$ErrorCount += 1
#print error message and save error in variable (Encase we want to output to error log etc...)
$ErrorMessage = $_.Exception.Message
Write-host -ForegroundColor Red $ErrorMessage
}
####################################
# Main Loop
####################################
foreach($Number in $Numbers) { #For each number in numbers array
Write-Host ""
Write-Host -ForegroundColor DarkYellow "###############################################"
Write-Host -ForegroundColor DarkYellow "# Extracting Number: $Number #"
Write-Host -ForegroundColor DarkYellow "###############################################"
Write-Host ""
#Clear variables for this run (you never know)
$Machines = $outfile = $FilterString = $null
#Create outfile Path
$outfile = "$($OutputFolder)\$($Number)_Machines_$(get-date -f yyyy-MM-dd-HH-mm).csv"
foreach($MachineTypeLetter in $MachineTypeLetters){ #For each Letter in Letters array
Write-Host ""
Write-Host "Extracting Machines that end in Letter: $MachineTypeLetter"
Write-Host ""
#Build Filter String for Get-ADComputer
$FilterString = "Name -like '*$($NamingConventionFilter)*' -and Name -like '*$($Number)$($MachineTypeLetter)'"
Try{ #try and get machines for AD
#Get the machines hostnames from AD as per $FilterString and Add to the $Machines array alog with the rest under this number
$Machines += (Get-ADComputer -filter $FilterString -server $Domain -ErrorAction Stop | Select Name | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1)
}catch{ # if it goes pete tong while trying to get the machines from AD
IAonError
} #End of Try ..Catch
} #End of foreach Letter
Write-Host ""
Write-Host -ForegroundColor DarkYellow "###############################################"
Write-Host -ForegroundColor DarkYellow "# Extracting Number: $Number #"
Write-Host -ForegroundColor DarkYellow "###############################################"
Write-Host ""
Try{ #Output Hostnames for this number
$Machines | Set-Content -Path $outfile -ErrorAction Stop
}catch{ #if it goes Pete Tong creating the output file..
IAonError
} #End of Try ..Catch
} #end of Foreach number
####################################
# End of Main Loop
####################################
####################################
# Check Error Count
####################################
ErrorCountCheck
####################################
# End of Script
####################################