r/PowerShell • u/ExecutiveGamer92 • Oct 15 '26
Question Computer Lab Firewall Rule Toggle Script
I work for an MSP and we have a client that has a computer lab for students to use. I was tasked with creating a script that would allow the teacher to enable and disable the workstations ability to communicate with the internet.
I pretty much have most of the script working, but I run into an issue when trying to make it so the user can enable or disable all the workstations at once. Attempted various methods so that when the user types All, the invoked command is sent to all the workstations listed; Get-Content is just the latest method I have tried.
When my boss asked to look at it, he said that I should just have the full command as shown at the bottom for each switch.
Any help on this would be great!
EDIT: Additional Information.
We don't have an AD environment setup for this client, and even with my limited knowledge I understand that AD would probably make this a lot easier.
IP Addresses are used due to the Asset ID of the workstations being used as the Host Name. They are also used because the workstations have static addresses and any replacements will get the same addresses.
#First Pull Username and Password from SecretVault for Account on all workstations
$remoteCred = Get-Secret -Name "Alias"
#Prompt to ask user if they are enabling or disabling workstations.
$prompt = Read-Host -Prompt "Enter E for enable and D for disable"
#Determine which command to use based on provided answer
if ($prompt -eq "E") {
$command = "Disable-NetFirewallRule -DisplayName 'NoInternet'"
}else {
$command = "Enable-NetFirewallRule -DisplayName 'NoInternet'"
}
#Use while to re-prompt user if they are enabling or disabling multiple workstations.
while ($true) {
$pcName = Read-Host -Prompt "Enter the name of the Computer you want to change. Or enter All if you wish to change all. Enter exit when finished"
#Use switch to base workstation name on user imput. All will use variable for all workstations.
switch ($pcName) {
"SW1" { $name = "192.168.100.211" }
"SW2" { $name = "192.168.100.212" }
"SW3" { $name = "192.168.100.213" }
"SW4" { $name = "192.168.100.214" }
"SW5" { $name = "192.168.100.215" }
"SW6" { $name = "192.168.100.216" }
"SW7" { $name = "192.168.100.217" }
"SW8" { $name = "192.168.100.218" }
"SW9" { $name = "192.168.100.219" }
"SW10" { $name = "192.168.100.220" }
"SW11" { $name = "192.168.100.221" }
"SW12" { $name = "192.168.100.222" }
"SW13" { $name = "192.168.100.223" }
"SW14" { $name = "192.168.100.224" }
"SW15" { $name = "192.168.100.225" }
"SW16" { $name = "192.168.100.226" }
"All" { $name = '(Get-Content -Path "C:\Users\userx\Desktop\Scripts\Workstations.txt")'}
"Exit" {
exit }
Default { Write-Host "Please try again." }
}
Invoke-Command -ComputerName $name -ScriptBlock { $command } -Credential $remoteCred
if ($pcName -eq "Exit" -or "All") { break }
}
#Invoke-Command -ComputerName "$name" -ScriptBlock {Disable-NetFirewallRule -DisplayName "NoInternet"} -Credential $remoteCred
2
u/PinchesTheCrab Oct 15 '26
A few suggestions:
I'd be curious if this works:
3
u/BlackV Oct 15 '26 edited Oct 15 '26
this seems not to make sense
$pcName($pcName = Read-Host)$name($name = "192.168.100.219")invoke-commandto$name(not$pcname) (Invoke-Command -ComputerName $name)$commandSeems odd
$nameand$pcnamerelate to each other?Invoke-Command -ComputerName $pcName?"NoInternet"firewall rule is blocking access to those IPs?it looks like
would do the job for you
Or a simple
4
u/ka-splam Oct 15 '26
I think you have the enable/disable logic the wrong way around.
I don't love the idea of having the computer names in two places (script and text file) where they can get out of sync. Do they have working DNS for the computer names? (And if not, do they have working PowerShell remoting passing credentials to IPs?).
Lots of things you could do, I suggest you learn about hashtables and then use that to map the computer names to IPs, and then you can use a lookup to get one of them or
.Valuesto get all of them.And then when you know about hashtables you can build one for the parameters to Invoke-Command, and splat it.
But /u/BlackV's suggestion of Out-Gridview is nicer
2
u/thehuntzman Oct 15 '26
I'd this an AD environment? I feel like hard-coding IP's would make this more complicated in the long run to maintain if you can just Invoke-Command to the hostname of the machine resolved via DNS using kerberos authentication under the context of the teacher's account.
2
u/xXFl1ppyXx Oct 15 '26 edited Oct 15 '26
i've always wanted to find a use for this: "Prompt for Choice" !
Feels somewhat hacky but i rarely get the chance to play with prompt for choice
this would actually be a useful case for Lists that Output the position when adding the Item, but from the top of my head i couldnt find one....
1
u/purplemonkeymad Oct 15 '26
Your switch is configuration data not code, so you probably don't want it in the script. A simple storage solution if you are only looking at a few dozen entries is to just store it as a csv. It's easy to edit for non techs and supported by powershell ie:
Then you can:
You can also extend this if you want to include your asset ids, etc.