Maybe you can use this fast hacked but realy easy C# Code. I would suggest to put that into an own "Service" which will be deployed (e.g. over GPO) on the target machines.
So what does the code:
It creates an eventhandler on "NetworkInformation.NetworkAddressChanged" which always triggers, if something on the network interfaces changes (e.g. WIFI has been deactivated, IP Address changed, Laptop put on ethernet).
Then it checks if at least one ethernet network interface is up: if so, start the "Windows Update" service - if not, stop the "Windows Update" servic
namespace WindowsUpdateLANOnly
{
class Program
{
static void Main(string[] args)
{
//NetworkInformation from reference: System.Net.NetworkInformation
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
Console.WriteLine("Listening for address changes. Press any key to exit.");
Console.ReadLine();
}
private static void AddressChangedCallback(object sender, EventArgs e)
{
//handle only events from the ethernet network interface
var adapters = NetworkInterface.GetAllNetworkInterfaces().Where(p => p.NetworkInterfaceType == NetworkInterfaceType.Ethernet);
//check if we have at least one ethernet network interface online
bool isOnLAN = false;
foreach (NetworkInterface ethernetNetwork in adapters)
{
Console.WriteLine(" {0} is {1}", ethernetNetwork.Name, ethernetNetwork.OperationalStatus);
if (ethernetNetwork.OperationalStatus == OperationalStatus.Up)
{
isOnLAN = true;
}
}
//deactivate or activate the windows update service
WindowsUpdateService(isOnLAN);
}
private static void WindowsUpdateService(bool start)
{ //ServiceController from reference: System.ServiceProcess
ServiceController sc = new ServiceController("Windows Update");
Console.WriteLine("Try to {0} the windows update service", start?"Activate":"Deactivate");
if (start) //should start
{
if(sc.Status.Equals(ServiceControllerStatus.Stopped) ||
sc.Status.Equals(ServiceControllerStatus.StopPending))
{
sc.Start();
//var timeout = new TimeSpan(0, 0, 5); // 5 seconds
//sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
Console.WriteLine("Windows update service started");
}
}
else //should stop
{
if (sc.Status.Equals(ServiceControllerStatus.Running) ||
sc.Status.Equals(ServiceControllerStatus.StartPending))
{
sc.Stop();
//var timeout = new TimeSpan(0, 0, 5); // 5 seconds
//sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Console.WriteLine("Windows update service stopped");
}
}
}
}
}
What do you think?