0

I am working on the powershell script that will remotely turn on/off specific firewall rule on remote machine in local subnet. Here is the code:

$pc="PC1"
if (Test-Connection -ComputerName $pc -Count 1 -quiet) {
$RuleName = "{F5E13AFE-90F7-4EFA-B547-14B3262D8507}"
ICM -ComputerName PC1 -ScriptBlock {
Param($FWRule)
IF(Get-NetFirewallRule $FWRule | where{$_.Enabled -eq $True}){Disable-NetFirewallRule $FWRule}
ELSE{Enable-NetFirewallRule $FWRule}
IF(Get-NetFirewallRule $FWRule | where{$_.Enabled -eq $True}){Write-Host "Internet access is blocking now`n" -ForegroundColor Red}
ELSE{Write-host "You have an access to Internet`n" -ForegroundColor Green}
$value = read-host "Script completed,`npress [Enter] to exit"
Switch ($value){
'Enter' {'Koniec' ; exit}
}
} -ArgumentList $RuleName }
else {Read-Host "Computer"$PC "is offline - script cannot be execute`npress [Enter] to exit"

Here is the problem. When user execute script on target machine , script will take long time to execute or will fail. How can I implement basic debugging system to know what is actually going on? Maybe the structure of this script is wrong or should I rewrote this script?

I will add that: User have admin right on target machine. In our AD domain we have gpos which allows winrm traffic.

  • I'd suggest you re-write is and test each segment one at a time to make sure you are getting what you'd think. You've got some real issues, IMHO, in this script. The hand is because you are using remote commands to yourself. You cannot do this with workstations in most cases. Try this on your own computer and see what I mean. --- Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock { Get-Date } --- it will just hang --- then try that same command on a server --- it will be successful. – postanote Nov 16 '18 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.