Does NW adapter powers off before Windows shutdown script is triggered? - windows

In my usecase i need to send API call upon Windows machine shutdown/reboot.
The script i created works as expected from the PowerShell console, but not when defined in gpedit.msc's shutdown scripts location.
I am assuming this is because WiFi adapter somehow gets down before the script gets executed. Added some debug prints and seems that it is a corect observation.
To prove it more i re-defined the same script in user login scripts location - and it worked as expected indeed.
My question is whether it is really correct that a WiFi adapter disconnects before the shutdown script steps into the process, and if yes - is there any way to outsmart it with PowerShell script?
(My theory is that trapping/delaying a WiFi adapter in a shutdown could adress the case, but even if i knew how to do that i smell the same issue here - the adapter will go down before the trap pops up...)
EDIT: the thing is definitely with the adapter power on shutdown call. If i define the User Logoff script - the script works even if i do reboot or shut down.

So despite i didn't find a way to intercept the NW adapter power supply on shutdown (as well as no comprehensive technical documentation of this feature), i definitely found out a way to acheive my usecase - define the user logoff scripts instead of shutdown scripts.

Related

Is there a way to monitor a Windows service and alert people when it hangs/stops?

We have a service running on a Windows Server 2003 machine. This service watches a particular folder on an FTP server, and when files appear there, it invokes one of a few different executables to process them.
I've been asked to find a way for staff to be alerted in some way when this service hangs or stops.
Can anyone suggest anything with just this much information? If not, what else would you need to know?
Seems we could write ANOTHER service to watch THIS service, but then there's a chance THAT one would stop ... so we haven't resolved anything.
About the only thing that I know if is writing another application or service that monitors if that service is running; something like that shouldn't have any unexpected behavior and stop, hopefully.
Another thing to do is go to the service in Windows, go to its properties, and then go to recovery options. From here, you can set the behavior of a service if it is to fail. The options in Windows 7 are to restart the service or computer, or run a program. This program could send some sort of notification. However, I don't know if any or all of these options exist in Server 2003. This would also not likely work if the service were to just hang, but a service watching it probably wouldn't either.
Also, if you have the source code, you can override some of the service-related methods such as OnStop() (for C#) to send a notification, but I don't believe this works with a failure.
My personal choice would be to set the recovery options just to restart the service on failure, unless it repeatedly fails, which there is also an option for. But just do what you think will work best for you; there isn't really a fail-safe method to do it.
UPDATE:
I did check, and Server 2003 does indeed have the same recovery options in the service manager. As the guys said above, you can deal with that, but it is only in C++ from what I have seen; there is also a command prompt way to do it:
sc failure [servicename] reset= 0 actions= restart/60000
I found that command here and you can look at it more in its MSDN documentation. You could call this command from C# or other languages if you are not using C++, or use it directly from the command prompt if you do not have the source code.
Use ChangeServiceConfig2() to define Failure Actions for your service. You could use that to invoke an external command to issue the alert (or do pretty much anything else you want) if the service terminates unexpectedly.
The SCM (the component which handles services) has built-in auto-restart logic that you can take advantage of to restart your service, as necessary. Additionally and/or alternatively, you can configure 'custom actions' to be associated with a failure of the service - the custom action can include launching a program of your own, which could then log the failure, and perhaps manually restart your service.
You can read more about such custom actions on MSDN by looking at the documentation of the structure used to configure such actions: SERVICE_FAILURE_ACTIONS. Once you fill that structure, you notify the SCM by calling the ChangeServiceConfig2 function.
Please don't ask "well, what happens if my failure handler program crashes" :)

Executing telnet commands on custom GINA

I have got a custom GINA (Windows Server 2003 R2) that is launching a modal window within WlxLoggedOnSAS and WlxLoggedOffSAS that contains all my workflow. It chains to MS GINA.
When I try to execute a remote script using telnet (like shutdown), it fails. Does any one know why this happens? And is there a way to handle this? Possibly handle an event in the GINA?
I have been hunting for a solution for a while and haven't found any.
I managed to figure this out. Just answering my own question for the benefit of anyone else running into the same scenario. Basically, I have classified GINA in 3 states -
Secure Access Seq (CAD) Screen : This is the one which prompts users to type in Ctrl-Alt-Del. Remote telnet commands will work here.
WlxLoggedOffSAS Screen : This is the login state where the custom GINA UI or MS GINA UI gets created. Generally, this thread is blocked. At this point, remote shutdown commands won't work. To deal with this situation, read on.
WlxLoggedOnSAS Screen : This is the desktop lock state where the custom GINA UI or MS GINA UI gets created. At this point, the shutdown command will work with the /f switch (force shutdown).
To get around state 2, you can do one of two things:
Use something like Windows Task Scheduler to initiate the restart.
Add a new method to your custom GINA DLL that essentially switches to the CAD screen (state 1). You can invoke that DLL method using rundll32.exe. One switched to state 1, just invoke the shutdown and it will work.

How can I re-use an existing CIFS session or explicitly terminate one programmatically (W2K8 to Netapp)?

I have a program that runs on Windows and scans shares on a Netapp filer over CIFS. When the scanner runs on W2K3 and exits, running "cifs sessions -c " on the Netapp filer shows that the session is gone. When the same scanner runs on W2K8 and exits, the above Netapp command shows the session to be still alive. The scanner application has already exited so there should be no traffic on the session anymore.
What I have done so far:
1) waited for (more than) cifs.idle_timeout number of seconds to see if the session goes away. It does not.
2) analyzed packet-traces for the communication between W2K3->Netapp and W2K8->Netapp. W2K3 trace shows TREE_CONNECT and TREE_DISCONNECT but W2K8 trace shows only the TREE_CONNECT request. I will verify this but I think the TREE_DISCONNECT on W2K3 is followed by a LOGOFF request (which causes the session to end I would believe?).
3) To see how things would look when Netapp is not in the picture -- from the W2K8-client machine, I accessed via windows-explorer \\C$. This causes a TREE_CONNECT request on the wire. Now I closed the explorer window and the W2K3-server machine closed the session even though there was no TREE_DISCONNECT on the wire generated by the W2K8-client.
So seems like Netapp is expecting something that W2K8 is not sending.
Question : Is there a specific API I can use in my scanner application to
i) send an explicit LOGOFF SMB request when I am done scanning?
ii) or, re-use an existing session b/w the W2K8-client and the Netapp filer for a new scan?
ii) anyone face this or similar scenarios?
Thanks in advance.
For the API - look at NetUseEnum and NetUseDel.
The disconnect you see is due to idle session (session with no open handles). I'm not sure if this is done by the server (which would indicate a difference between Netapp and Windows) or by the client (your scanner holds an open handle, maybe the current directory, while Explorer closes the handle when you close the window).
I'm not sure what you are trying to accomplish, but it might be easier using NetApp tools/APIs. For a lot of administration tasks the Data ONTAP PowerShell Toolkit can really simplify things:
PS C:\> Connect-NaController fas2040rre1
PS C:\> Get-NaCifsShare
MountPoint ShareName Description
---------- --------- -----------
/etc ETC$ Remote Administration
/vol/vol0/home HOME Default Share
... snip ...
/vol/cifs/backups backups
And if PowerShell isn't your thing, you can take a look at the Manageability SDK for a Web Services API to C/C++, Java, Perl and .Net. In that case you'd want to look at the cifs-share-list API's (slightly different between 7.x/8.x).

intercept shutdown in windows using vbscript

I want to write a vbscript that will wait for windows shutdown and intercept to carry out proper action before shutting down. I want this to be in vbscript since am using it for administration.
Much easier than that, ever since Windows 2000 you can use the Group Policy setting (or Local Policy even if you're not in a domain) to set the Shutdown Script which will be called, as the name implies, when someone is trying to shutdown the computer. For more info, check out MSKB #198642.

how can I make a Windows service beep?

I've got a service that acts as a watchdog for several apps/servers. There are no user sessions on this machine. I'd like the watchdog to be capable of beeping on the internal speaker should something go wrong (that'd be my queue to go fix whatever it's complaining about)
when I try the Beep() API on Windows nothing happens - I suspect the problem is that the services session isnt permitted to make noises?
can I make this work? any other ideas for how to make the service alert me?
-CG
Call CreateFile on \device\beep, then send down IOCTL_BEEP_SET (see http://www.koders.com/c/fidFEC3527B9D951559D62722A9C0C603863106CA9B.aspx for details)
It may work if you allow it to interact with the desktop (an option configurable somewhere, I can't remember where).
But personally, I'd have it email me.
Though maybe you could have it use the task scheduling API to schedule a task for yourself, so next time you log on you can see it.
I don't know; you've got a few options. I'd avoid beeping though.
Try sending beep char "\a" to console. Not sure if it will work.
Beeping doesn't seem like a good idea - it might end up driving everyone mad....
I'd also agree about the "interact with desktop" option and you set this in the services parameters see A Windows Service without a template
I'd recommend creating a simple client application that polls that server to query for any problems and returns a set of status messages. Then an appropriate UI would be raised (e.g. balloon on the tray), an email sent, etc. containing any warning or failure messages.
This way you also know that the watchdog itself is running and has network connectivity - if the watchdog dies and/or machine locks up you wouldn't otherwise know.
It also avoids being thrown out of a window when the machine starts beeping continuously just after you go to lunch. [+1 to #mikej] :-)
The poll period should be around half (see Nyquist sampling rate) your minimum required response time.

Resources