-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysconfig.ps1
executable file
·152 lines (103 loc) · 4.55 KB
/
sysconfig.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
152
# This script is designed to automate the setup process for new machines.
# It will change the hostname, join a domain, and run the required installers
# UPDATED: 07/18/17
# [ namespace ]
[System.Reflection.Assembly]::LoadWithPartialName( 'Microsoft.VisualBasic' ) | Out-Null
# [ global variables ]
$LOG = "c:\SETUP_LOG.txt"
$PENDING = "c:\Pending_Installs"
$INSTALLED = "c:\Installed"
$NAME = (Get-WmiObject Win32_ComputerSystem).Name
$DOMAIN_NAME = "Some.Domain"
# [ functions ]
function changeHostname { # Promts the user for a host name then restarts the machine to set the change
$HOST_INFO = Get-WmiObject Win32_ComputerSystem # Stores current computer information
$HOST_OLD = $HOST_INFO.Name
$HOST_NEW = [Microsoft.VisualBasic.Interaction]::InputBox( "Enter Desired Computer Name" ) # prompts the user for a new name
$HOST_INFO.Rename( $HOST_NEW ) | Out-Null # changes the hostname of the device
log "Host name ( ${HOST_OLD} ) changed to ( ${HOST_NEW} )"
$RESPONSE = [Microsoft.VisualBasic.Interaction]::InputBox( "Do you want to restart now? [y/n]" )
if ( ( $RESPONSE -eq 'y' ) -or ( $RESPONSE -eq 'Y' ) ) {
log "Resetting to apply hostname change"
shutdown /r /t 0 /c "Shutting down to change the hostname."
}
}
function checkPending( $PENDING, $INSTALLED ) { # Displays installed software and pending installs
$PEND = dir $PENDING | Select-String -
function log( $EVENT ) { # Timestamps and records an event to a specified log file
if ( ! $EVENT ) { echo "$(Get-Date -Format d)" >> $LOG }
else { echo "`t$(Get-Date -Format t) -> ${EVENT}" >> $LOG }
}
function joinDomain { # Joins the domain
while ( -not ( (Get-WmiObject Win32_ComputerSystem).Domain -eq $DOMAIN_NAME ) ) {
Add-Computer -DomainName $DOMAIN_NAME # prompts for domain admin creds, then joins the domain
}
log "Hostname ( ${HOST_NAME} ) has been joined to ( ${DOMAIN_NAME} )"
}
function operations_prompt { # determines the operations to run
# 0. Resize
# 1. Change Hostname
# 2. Join Domain
# 3. Run Installers
$EXIT = 'false'
$OPT = '1. Resize the disk', '2. Change the hostname', '3. Join the domain', '4. Run the installers', "5. View Log (${LOG})", '6. Exit'
do {
echo $OPT
$RESPONSE = Read-Host 'Press the number that corralates'
switch ( $RESPONSE ) {
'1' { log "Resizing the disk."; resizeDisk }
'2' { log "Changing the hostname"; changeHostname }
'3' { log "Joining the domain"; joinDomain }
'4' { log "Runing installer group"; runInstallers "$INSTALLERS" }
'5' { viewLog }
'6' { log "***Script exiting***"; $EXIT = 'true' }
Default { echo "`nInvalid Response! ( ${RESPONSE} )`n`n" }
}
} until ( $EXIT -eq 'true' )
}
function resizeDisk { # Resizes the disk
try {
$MAX_SIZE = (Get-PartitionSupportedSize -DriveLetter c).sizeMax # determine the free space on the disk
Resize-Partition -DriveLetter c -Size $MAX_SIZE # extends root to the max size available
log "Partition resized to the maximum available ( ${MAX_SIZE} )"
}
catch {
log "ERROR[resizeDisk]: ${ERROR}"
echo "ERROR[Resize Disk]: Disk may already be at max size. See log for more." -fore white -back red
Break
}
}
function runInstallers ( $INSTALL_DIR ) { # locate installers in the given dir then executes them
$INSTALLER = ''
try {
$CMD = dir $INSTALL_DIR | Select-String -Pattern '*.CMD$', '*.bat' # search for .cmd and .bat files to exec
log ".CMD + .bat files found ($($CMD.length): `n${CMD})"
for ( $i = 0; $i -lt $CMD.length; $i++ ) {
$INSTALLER = $CMD[$i]
&( $INTSALL_DIR + $CMD[$i] )
log "${INSTALLER} ran"
}
}
catch {
log "ERROR[runInstallers - ${INSTALLER}]: ${ERROR}"
echo "ERROR[runInstallers]: An issue occured while trying to install ${INSTALLER}"
}
log ".MSI files found (${MSI}): `n ${MSI}"
$MSI = dir $INSTALL_DIR | Select-String -Pattern '*.msi$' # search for .msi files to exec
for ( $i = 0; $i -lt $MSI.length; $i++ ) {
&( $INSTALL_DIR + $MSI[$i] + " /qn")
log "${MSI[$i]} installed"
}
log ".exe files found (${EXE}): `n${EXE}"
$EXE = dir $INSTALL_DIR | Select-String -Pattern '*.exe$' # search for .exe files to exec
for ( $i = 0; $i -lt $EXE.length; $i++ ) {
&( $INSTALL_DIR + $EXE[$i] )
log "${EXE[$i]} installed"
}
}
function viewLog { cat "${LOG}" | out-host -paging }
# [ main ]
log
operations_prompt
echo "`n`nLog can be found at ${LOG}" -fore green
exit