Friday, 30 December 2011

List invalid DHCP Lease Times

Had need to find all scopes where the Lease Time was not 8 days. Note that all Lease Times are counted in seconds...
8 Days is 691200 Seconds (60sec * 60min * 24hours * 8days = 691200 Seconds).

for /F "tokens=5,10" %a in ('netsh dhcp server \\dhcpsrv1 dump ^| findstr /I /C:"optionvalue 51" ^| findstr /I /V /C:691200 ^| findstr /I /V /C:BOOTP') do @echo Scope Name ^: %a - Lease Time in sec^: %b  

Gives a output like this:  
Scope Name : 10.20.31.0 - Lease Time in sec: "86400" 
Scope Name : 10.21.73.0 - Lease Time in sec: "31536000" 
Scope Name : 10.25.20.0 - Lease Time in sec: "31536000" 
Scope Name : 10.25.21.0 - Lease Time in sec: "31536000" 
Scope Name : 10.25.22.0 - Lease Time in sec: "31536000" 
Scope Name : 10.25.24.0 - Lease Time in sec: "31536000" 
Scope Name : 10.35.120.0 - Lease Time in sec: "31536000" 
Scope Name : 10.35.198.0 - Lease Time in sec: "31536000" 
Scope Name : 10.36.24.0 - Lease Time in sec: "31536000" 
Scope Name : 10.36.32.0 - Lease Time in sec: "31536000" 
Scope Name : 10.150.8.0 - Lease Time in sec: "31536000" 
Scope Name : 10.200.0.0 - Lease Time in sec: "31536000"
# End of Script

Thursday, 29 December 2011

Cleanup Cache for a failing Deployment

This is a quite specific scenario which quite few companies may run into. A company is using their own Wrapper-Script for all installations and ran into some problems where all files did not exist in the Deploy Cache.
Which caused failed installations (missing files etc).

To reset the deployment on a client, clean up the DeployCache and make the client download all the files again run this script;

Echo off  
echo %date% %time% > c:\temp\clean.txt  
Echo Executing user: >> c:\temp\clean.txt 
whoami >> c:\temp\clean.txt    

if exist c:\temp\progress.txt del c:\temp\progress.txt 
reg query "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache" /s /v Progress | findstr /c:HKEY > c:\temp\progress.txt  
if not exist c:\temp\progress.txt goto noprog    

setlocal enabledelayedexpansion  

for /F "Delims=\ Tokens=1-20" %%a in (c:\temp\progress.txt) do ( 
dir "C:\Windows\SpecopsDeploy\DownloadCache\%%g" >> c:\temp\clean.txt 
reg query "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache" /s /v Progress >> c:\temp\clean.txt   

echo.  Echo Clean starts >> c:\temp\Clean.txt  
rmdir /Q /S "C:\Windows\SpecopsDeploy\DownloadCache\%%g"  

echo reg delete "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache\%%g" /VA /F >> c:\temp\clean.txt 
reg delete "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache\%%g" /VA /F 
reg delete "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache\%%g" /F  

echo.  
Echo Clean Done >> c:\temp\Clean.txt  
Echo. 
Echo Showing remaining dirs in DownloadCache >> c:\temp\clean.txt  
dir "C:\Windows\SpecopsDeploy\DownloadCache\" >> c:\temp\clean.txt 
Echo. 
Echo.  
Echo Showing remaining RegKeys with Progress: >> c:\temp\clean.txt  
reg query "HKLM\SOFTWARE\SpecopsSoft\Specops Deploy\Client Side Extension\Download Cache" /s /v Progress >> c:\temp\clean.txt   
)   

if exist c:\temp\progress.txt del c:\temp\progress.txt 
goto end  

:noprog 
Echo Nothing to do. >> c:\temp\clean.txt  

:end 
# End of Script

It's quite easy to copy the script to the PC and then execute the script with either Sysinternals PSExec or with Specops GpUpdate Pro.

Wednesday, 28 December 2011

Remove option 66 & 67 from multiple DHCP Scopes

A customer wanted to remove DHCP Option 66 & 67 from more than 200 DHCP Scopes and use IP Helpers instead, which is the best practice.

Small script that takes care of the scopes;
of course, replace dhcp.server.domain.com with the FQDN of your DHCP Server.

@Echo off 
netsh dhcp server \\dhcp.server.domain.com dump | findstr /i /c:"com Add Scope" > c:\temp\Allscopes.txt   
setlocal ENABLEDELAYEDEXPANSION   
for /F "Tokens=1-10" %%a in (c:\temp\Allscopes.txt) do (    

REM NOTE: IT's just showing what would be done. No actual change is done right now!!  
REM NOTE: Remove ECHO from these two lines to make the script active.   

echo netsh DHCP Server \\dhcp.server.domain.com Scope %%f Delete OptionValue 66 
echo netsh DHCP Server \\dhcp.server.domain.com Scope %%f Delete OptionValue 67  
)  
del c:\temp\Allscopes.txt 

# End of Script

Tuesday, 27 December 2011

Remove AD Computer accounts

I had a need to Automatically remove some computers from the AD on a regular basis. This command will find all computers matching the naming pattern (D*CLIENT*) and who has no Description on the Computerobject.

Import-Module ActiveDirectory  
Get-ADComputer -filter {name -like '*D*CLIENT*' -and Description -notlike '*'} | Remove-ADObject -recursive -Confirm:$false 


# End of Script

I had to use Remove-ADObject instead of Remove-ADComputer because the accounts had a SpecopsDeploy child object (Leaf), which Remove-AdComputer were unable to remove.