Monday, December 12, 2011

Get all active leases on all DHCP servers


echo off

for /F "eol== tokens=7" %%d in ('netsh dhcp show server') do if NOT %%d==directory call :S1 %%d

GOTO :END

:S1
echo ============================================
echo Scopes on %2
echo ============================================

for /F "eol== tokens=1" %%i in ('netsh dhcp server \\%2 show scope') do if NOT %%i==Scope if NOT %%i==Total if NOT %%i==Command netsh dhcp server \\%2 scope %%i show clients 1

:END

Powershell one-liner to find large files

PS> get-childitem -Path "H:\MyPath" -recurse | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt 134217728}| sort-Object -property length -Descending | export-csv -path "S:\LargeHFiles.csv"



134217728 is bytes, so 134217728/1024/1024 = 128Mb

How to digitally sign your powershell script

First open up the Certificates snap-in for your user account in MMC
Have a look in your Personal Certificates store to see if you have a Code Signing certificate - if not, import one if you have one from a trusted CA, or request one from the CA on your AD domain ... if a Code Signing certificate is not available you will need to get your CA to publish a Code Signing certificate template.

Once you have a Code signing cert in your Personal store:

PS>$cert=Get-ChildItem -Path cert:\CurrentUser\my -CodeSigningCert
PS>Set-AuthenticodeSignature -FilePath MyScript.ps1 -certificate $cert

How to find out all the active clients serviced by your DHCP server

At the cmd line:

for /F "eol== tokens=1" %i in ('netsh dhcp server show scope') do if NOT %i==Scope if NOT %i==Total
if NOT %i==Command netsh dhcp server scope %i show clients 1

--------------------------------------------------------------
In a script:

for /F "eol== tokens=1" %%i in ('netsh dhcp server show scope') do if NOT %%i==Scope if NOT %%i==Total
if NOT %%i==Command netsh dhcp server scope %%i show clients 1