Disable sleep while downloading or uploading through PowerShell.

I have a machine at home that acts as a server hosting several apps and services. It is used a couple of hours a day total usually in succession to stream media, download files and so on.

The server is set to go to sleep and suspend to RAM to save power after 5 minutes of idling. It works great as all of the apps so far have been usage aware and prevent sleep while they are being used.

Recently I installed an application that downloaded and uploaded files that was not usage aware. This meant that in the middle of a download or upload if everything else was idle but that app the server would go to sleep.

The logical solution, in my mind, was to get an application that disabled sleep while network card traffic was above a certain threshold.



I spent an hour googling around and found nothing that suited my needs even remotely, and once I got over my disappointment I decided to create a solution using my novice PowerShell skills.


The script that sets sleep based on network ability is below. It's not as good as it could be but it works for me.

You will need Powershell 2.0 to run it.

Checks NIC activity, if it is less than 50KB/s, sets sleep timeout to 5 min, if more than 50KB/s disables sleep. Checks again 140 seconds later. That's it.


Do a get-counter in powershell and replace my NIC "counter" name with yours or it wont work.

##############################
##### nicsleep.ps1
##############################

function checknicactivity

{

$counter = 1

do
{
$niccounter = Get-Counter -counter "\network interface(realtek rtl8102e_rtl8103e family pci-e fast ethernet nic [ndis 6.20])\bytes total/sec"
$c = $niccounter.countersamples | where {$_.path -match "ndis 6.20"}
$d = $c.cookedvalue

if ($d -gt 51200){powercfg -change -standby-timeout-ac 0}
elseif ($d -lt 51200){powercfg -change -standby-timeout-ac 5}

Start-Sleep -s 140

}
while ($counter -gt 0)


}

checknicactivity


#####################






If you want to run this with no Powershell visible shell one way is to call it through vbscript. This assumes both files are located in C:\Scripts






''''''''''''''''''''''''''''''

Dim objShell
Set objShell=CreateObject("WScript.Shell")

'enter the PowerShell expression
'you need to use short filenames and paths
strExpression="C:\Scripts\nicsleep.ps1"

strCMD="powershell -nologo " & strExpression

'Uncomment next line for debugging
'WScript.Echo strCMD

'use 0 to hide window
objShell.Run strCMD,0


'''''''''''''''''''''''''''''''''''''''''''


One thing I struggled with was deciding if I wanted to run the script one off as a scheduled task every x seconds or just run it once and have the loop keep it open forever (it autostarts with windows for me).

To me it seemed more logical to keep the PS process open rather than keep launching a new one every 2 minutes.


It's quick and dirty and it works for me, there is probably better ways to do it. If you have a suggestion let me know.

Legacy mailboxes and Exchange 2007

If for some reason you manage to create a 2007 mailbox using 2003 AD to drive the process you will end up with a Legacy mailbox on 2007 that will not function properly. To get around it you can either delete the mailbox and start over properly or run a command from the 2007 console.

That command is:

Set-Mailbox user@email.com –ApplyMandatoryProperties

*note - that last part is case sensitive.


Running this will fix a wrongly created mailbox and give it the right properties so it functions correctly.

Windows 7 and Sysprep Issues Part 2

If you are sysprepping a completed image you will most likely have some sort of Anti Virus software installed. If you were like me you were you were frustrated as to why sysprep kept failing and you had to keep redoing your work. After some trial and error I narrowed it down to our AV (Kaspersky) and then found the pretty simple solution.

Sysprep rips out a number of registry entries to generalize the system. Kaspersky, just like most other AV system, protects the registry from certain changes especially ones that try to change anything about Kaspersky.

The fix was really simple, just go into the Kaspersky settings, go to Options, and un-check "Enable Self Defense". Other AV vendors have similar functionality. So if you are having issues with sysprep try to do it with everything but your AV installed. If it works try to find the AV option that will let you disabled the protection needed to sysprep.

You are now free to sysprep away.... Just remember to enable self defense when you redeploy.

The "New" NTBACKUP

If you've migrated from Server 2003 to Server 2008 you might have noticed that NTBACKUP has changed. Some would say for the worse since you can not specify only certain directories or files to be backed up. It can only do full drive copies. The GUI is also makes it impossible to do a scheduled backup to a network location. But worry not as it is possible with a little batch file that you can then run as a scheduled task. I've posted an example below.


wbadmin start backup -backupTarget:"\\server1\placeIwantbackuptogo" -include:C:,E:,X: -allCritical -vssFull -quiet



That will start a full backup of C, E, and X to the specified network location. Make sure you have space on your destination host for this. If you are up to date on your patches (which you should be) whne you do a restore you can do a regular restore as well as an "Exchange Aware" restore that will only restore the relevant Exchange 2007 files from the backup to the original or new location you specify.

Windows 7 and Sysprep Issues

I'm not sure exactly what Microsoft has done with Sysprep but it just does not work standalone with Windows 7. Certain services running can cause it to fail mid prep. The only working way to Sysprep a Windows 7 box is to use the AIK2010 which in itself is much more burdensome than only using Sysprep. I used the AIK2010 to Sysprep a machine, I was promptly greeted with a Sysprep computer setup error which forced the box into a reboot loop.

If you are planning to do a Windows 7 deployment make sure you do proper Sysprep and AIK testing and not just take for granted that Sysprep will just work like it always has.