Windows XP Shutdown scripts

Shovel

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,350
Hello there, I know it's still Christmas, but I'm having a brief "ideas burst" before bed, so I'm posting techie stuff anyway.

I'm after an equivilent of the "Startup" folder, but for Shutdown activities - and specifically Logoff activities.

While I'm at home, I have a USB flash drive that I use for storing my Outlook data file and various bits and bobs. It's formatted using NTFS, so it's not a good idea to use the hot swapping features of flash drives to avoid file system damage, and the portabilty is purely between home and uni - not regularly around the home.

I want to write a script to mount the drive when I log in (easily enough from the startup folder) and I want a reverse script to unmount it (to make it safe to remove) when I log off. I just don't know where to look for place such a script.

Can anyone help?
 

Tom

I am a FH squatter
Joined
Dec 22, 2003
Messages
17,336
Are there command line options for whatever software you use to mount the device? If so, perhaps you could create a batch file to unmount it, and then initiate the shutdown command for windows.
 

DarkHalf

Fledgling Freddie
Joined
Dec 25, 2003
Messages
6
so its just a normal mass storage controller compliant usb flash drive? these things are automatically flushed and safe to remove, when you shut your computer down, so they dont need to be unmounted. if you need to unmount it by force in windows, why not use the built in windows stop feature?
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
If you go to start > run, type gpedit.msc

This should open the local group policy.

Under the User Configuration > Window Settings > Scripts(Logon/Logoff).

There are key/values for logon and logoff scripts. Doubleclick on Logoff and then add, and browse to your script or batch file.

I think this should then run when you logoff.

If you're on a domain, domain policy may over ride this setting, and if the no-override option is set on the domain policy, you won't be able to change it without changing the domain policy.

There's a corresponding Scripts (Startup/Shutdown) under Windows Settings under the Computer Configuration section. This can be used to run scripts during startup before the user has logged on, and presumably after the user has logged out, but the Operating system is still putting itself to bed.

Hope this helps.
 

DarkHalf

Fledgling Freddie
Joined
Dec 25, 2003
Messages
6
good point, but it doesnt unmount it. i spose, having the command that "mounted" (using that term loosely, coz Shovel could be a network map for all we know) it in the first place, would make it easy to invert.
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
I didn't really focus on why he wanted to use a shutdown script, but you're right in that if they're in the usb port, then XP will see them and mount them automatically. I guess there might be a way of stopping them do that.

I've had a few bad experiences with these USB keys, where I waited goodly time and made sure that the light had stopped flashing, and Windows has still not 'allowed' me to remove the key. Or I take it somewhere else and put it in and it has the files I thought I'd deleted on the other computer, not what I thought I'd copied on :D
 

DarkHalf

Fledgling Freddie
Joined
Dec 25, 2003
Messages
6
thats the write behind cache and what not tho init? i have a usb 2 external laptop hard drive caddy with a 6 gig in. been using it for about a year, and never had one bit of trouble with it. generally i copy stuff on, close the explorer window that was pointing (or referencing the drive) and then click the safely remove and bing, it does it.
occasionally the iritable windows subsystem stops me, ie: that stupid bloddy "let me preview all your media files, wow look im great, i showed that all of the first frames are blank" feature means the files are locked open and hence the FS wont dereference it. its a pain in the arse waiting for the thread to either complete its previewing, fail, or in the worst case, crash, and take out all your explorer processes!

ack, i digress.

oh, and Shovel if your actually talking about having mounted this thing over a network or something (outside chance) you can use the "net use /delete {share UNC}" command to be rid of it.

i spose you could probably use some WSScript automation to bring up the window (some rundll32 combo no doubt) and then click the stop button for you, with the appropriate waits and script glue to go in there.
 

fatbusinessman

Fledgling Freddie
Joined
Dec 22, 2003
Messages
810
Try this:

While you've got your flash disk active, right-click on the disk's drive icon and select Properties. Then select the Hardware tab. Somewhere in the list should be an entry for your flash disk. Select that, then click Properties again.
In the device properties, select the Policies tab, and ensure that the Optimize for quick removal option is selected. This should prevent write caching to the disk, and allow you to hot-swap it without filesystem corruption (unless you do something stupid like remove it while writing to it).
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
fatbusinessman said:
Try this:

While you've got your flash disk active, right-click on the disk's drive icon and select Properties. Then select the Hardware tab. Somewhere in the list should be an entry for your flash disk. Select that, then click Properties again.
In the device properties, select the Policies tab, and ensure that the Optimize for quick removal option is selected. This should prevent write caching to the disk, and allow you to hot-swap it without filesystem corruption (unless you do something stupid like remove it while writing to it).

That's a very handy tip ... thanks ... I've checked my PC here and it was optimised for quick removal as you suggested ... do you know if there's a similar setting under 2000? It's on my 2000 laptop at work that me and some colleagues have been having problems. Actually, don't worry I've just remembered the other half's laptop is still on W2K so I can check it out here.
 

fatbusinessman

Fledgling Freddie
Joined
Dec 22, 2003
Messages
810
Quige said:
That's a very handy tip ... thanks ... I've checked my PC here and it was optimised for quick removal as you suggested ... do you know if there's a similar setting under 2000?
Yes, there is - Windows 2000 does include an option to enable/disable write caching, but in the true 2000 tradition, doesn't explain it.

No problem, glad to help. :)
 

Shovel

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,350
fatbusinessman said:
Try this:

While you've got your flash disk active, right-click on the disk's drive icon and select Properties. Then select the Hardware tab. Somewhere in the list should be an entry for your flash disk. Select that, then click Properties again.
In the device properties, select the Policies tab, and ensure that the Optimize for quick removal option is selected. This should prevent write caching to the disk, and allow you to hot-swap it without filesystem corruption (unless you do something stupid like remove it while writing to it).
I believe that I can't do that, due to being formatted with NTFS (When I formatted it insisted that I switch off the "Optimise for quick removal" mode). It's also worth noting that the drive operates faster with this disabled - since there's an Outlook data file on here, that's preferable.

Thanks for the suggestions so far. To clarify a little further:
* The drive is local, there's no network messing here.
* This particular script needs to take place on log off, rather than shut down - apologies for not making that clear. It's 'cause I was interested to know about shutdown scripts as well and didn't clarify. Sorry.
* It's Windows XP Home Edition, so I've not got gpedit I'm afraid.

With regards to scripts: I've managed to loose the command I was thinking of when refering to "Mount" and "Unmount".

EDIT: Found it. It's a command called MOUNTVOL, you can get help on it by typing mountvol /? ("help mountvol" doesn't work). This, I believe can be used to mount/unmount the volume - just needs to be placed at log off.

I'm going to poke the task scheduler, see if that'll do it.

EDIT2: OK: The scheduler has an "On Logon" option, but not a Logoff equivilent.
 

fatbusinessman

Fledgling Freddie
Joined
Dec 22, 2003
Messages
810
Shovel said:
I believe that I can't do that, due to being formatted with NTFS (When I formatted it insisted that I switch off the "Optimise for quick removal" mode). It's also worth noting that the drive operates faster with this disabled - since there's an Outlook data file on here, that's preferable.

Is there a specific reason why you formatted it with NTFS?
 

Shovel

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,350
Compression, mainly.

Anyway, grandparents are here, time for Christmas Part 2. ;)
 

DarkHalf

Fledgling Freddie
Joined
Dec 25, 2003
Messages
6
Fair enough, then the only other way i can think of doing it would be to make a vb program that ran on logon, which didnt actually do anything, however when the program recieves the terminate signal (form.unload or form.terminate) then it shells to the unmount command you have to use.

thoughts?
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
Shovel said:
* It's Windows XP Home Edition, so I've not got gpedit I'm afraid.

Can you get at the same policy by going run > mmc > File > Add/Remove Snap-in > Add > Local Group Policy or Group Policy Object Editor

I'm sorry I have XP Pro, never had a look at XP Home, so maybe you aren't allowed any access to local settings via an mmc and policy.

Awaiting Xmas pt II myself on arrival of mother :)
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
You might want to have a look at this site - though I couldn't see the particular entry for logoff scripts.

Group policy registry entries for xp home
http://www.j79zlr.com/gphome.php

If I set a machine shutdown script called test.bat on my PC, it puts this in the registry;

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\0]
"GPO-ID"="LocalGPO"
"SOM-ID"="Local"
"FileSysPath"="C:\\WINDOWS\\System32\\GroupPolicy\\Machine"
"DisplayName"="Local Group Policy"
"GPOName"="Local Group Policy"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\0\0]
"Script"="C:\\test.bat"
"Parameters"=""
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Startup]

If I do a logoff script for a user it puts this in;

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\S-1-5-21-777546997-2006763630-3948032881-1108\Scripts\Logoff\0]
"GPO-ID"="LocalGPO"
"SOM-ID"="Local"
"FileSysPath"="C:\\WINDOWS\\System32\\GroupPolicy\\User"
"DisplayName"="Local Group Policy"
"GPOName"="Local Group Policy"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\S-1-5-21-777546997-2006763630-3948032881-1108\Scripts\Logoff\0\0]
"Script"="C:\\test.bat"
"Parameters"=""
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

where the long number is my user SID.
 

Xavier

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,542
Why not just remap the folders in the outlook data file path for your profile? Surely that's much simpler?


There's also an application called PI Protector which moves your entire profile to the USB key, insert the USB device and when it autoruns your documents, temporary internet files (and settings) and email run from the USB key. I've tried it with a test profile on my 1Gb USB2 key, and it seems pretty robust.
 

Shovel

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,350
Quige said:
Can you get at the same policy by going run > mmc > File > Add/Remove Snap-in > Add > Local Group Policy or Group Policy Object Editor

I'm sorry I have XP Pro, never had a look at XP Home, so maybe you aren't allowed any access to local settings via an mmc and policy.

Awaiting Xmas pt II myself on arrival of mother :)
It appears, but when I choose to add it, it informs that I can't because it's Home Edition. I might have a looksie for those Reg keys though - thanks. Do you know of an easy way to get my SID out of the system?
 

fatbusinessman

Fledgling Freddie
Joined
Dec 22, 2003
Messages
810
I have to admit that I still can't really see the point of such a script - couldn't you just leave the drive plugged in all the time if you're only going to be transporting data with it very occasionally? There doesn't really seem to be any point in setting up an add/remove script for it...
 

Quige

Fledgling Freddie
Joined
Dec 22, 2003
Messages
118
Shovel said:
It appears, but when I choose to add it, it informs that I can't because it's Home Edition. I might have a looksie for those Reg keys though - thanks. Do you know of an easy way to get my SID out of the system?

At a dos prompt try;

C:\>whoami /user /sid

On mine this comes back with

[User] = "YGGDRASIL\quige" S-1-5-21-777546997-2006763630-3948032881-1108

You should then be able to tie this up with the registry.
 

Xavier

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,542
is whoami installed in xp home by default? In win2k it's a resource kit tool and XP Pro it's not a standard component...
 

Shovel

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,350
Xavier said:
is whoami installed in xp home by default? In win2k it's a resource kit tool and XP Pro it's not a standard component...

I can download whoami as required.

Fatty: The point of the script is because I don't want it available for my family to accidently format/overwrite/break by unplugging it. Therefore I just want it to automatically unmount when I log off, rather than having to do it manually. You know, the whole "Computers are our servants, not vice versa" thing.

I will give VBS a look - I've never done any before, but it certainly sounds like a pretty reasonable option. I'd forgotten all about its existance I must say - here was me thinking Windows had no proper scripting language. Heh.
 

Users who are viewing this thread

Top Bottom