Another quick post to show how you can quickly deploy fully working DHCP server with multiple scopes in a matter of seconds. In my case it was a single server with 90 very different scopes and doing this manually would be just soo boring and long that’s unreal. Unfortunately Windows Server 2012 wasn’t an option so no PowerShell love but Windows Server 2008 R2 is still pretty decent and using netsh wasn’t as painful as it seemed. To get us started we need to install the DHCP Server role and start the required service (dhcpserver):
<strong>start /w ocsetup DHCPServer
sc config dhcpserver start= auto
net start dhcpserver</strong>
Next step is to authorize DHCP server in the enterprise so we can actually use it to dish out IP addresses:
<strong>netsh dhcp add server %COMPUTERNAME% ip_address_goes_here</strong>
Other useful commands here would include:
To deauthorize the server –
<strong>netsh dhcp delete server %COMPUTERNAME% ip_address_goes_here</strong>
and to list all authorized servers –
<strong>netsh dhcp show server</strong>
If you need to shutdown computers per OU in Active Directory here is one way of doing it:
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, Location from 'LDAP://<strong>XXXXXXXXXXXXXXXXXXXXXXXXXXXX</strong>' " _
& "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
'Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
'Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
txtPCName = objRecordSet.Fields("Name").Value
txtshutdown = "shutdown -t 15 -s -f -m \\" & txtPCName
set objShell = CreateObject("WScript.Shell")
The only change you have to make for this script to work is to provide an LDAP string/path to the OU in AD where you’d like to shut the PCs.
In my company we had to come up with a solution to comply with the “green” policy enforced upon us. Initially I have tried the built-in shutdown command and feeding in computer names using Excel and CONCATENATE function. Issue with that approach was that my script was trying to reach out to each machine to check if its on the network and pingable taking too long to complete. Script above doesn’t have the same issues – it literally fires up shutdown request simultaneously to all machines on the specified OU – much quicker and definitely more effective!
If you guys have other ideas or have other solutions please let me know in comments below!