Preheat the oven to 350 degrees F. Spray an 8-by-8-inch flameproof baking dish with cooking spray.
In a skillet over medium heat, melt the butter. Add the onions and saute until translucent, 3 to 4 minutes. Add the flour and stir until combined. Cook 1 to 2 minutes to remove the raw flour taste. Add the garlic, 1 teaspoon salt, 1 teaspoon pepper, the cayenne and mustard powder. Whisk in the milk. Allow to cook and thicken slightly, about 3 minutes. Remove from the heat.
Line the baking dish with a crisscrossed layer of country ham, then a layer of slightly overlapping potatoes, then about 2/3 cup sauce. Repeat the layering until all the ingredients are used. You should have a total of 3 sets of layers, ending with the sauce on top. Top with the Gruyere and cover with foil. Bake until the potatoes are tender, about 1 hour.
Remove the foil and spoon off any fat that has pooled in the baking dish, if needed. Switch the oven to broil and broil until light golden brown, 3 to 5 minutes. Garnish with green onions before serving.