Trong Posted 20 hours ago Posted 20 hours ago I have written a function to run the ps command and get the result back, but the semicolon problem has been driving me crazy, as the command sent to ps quotes seems to have problems and cannot run the command correctly! no matter I try to add 2 double quotes or use single quotes, it still does not work properly. expandcollapse popup; *** Add a program to Windows Defender Exclusions using PowerShell *** #RequireAdmin #include <AutoItConstants.au3> ; *** Example Usage *** ; Get the list of running processes and filter for processes named "notepad" Local $commandToRun = "Get-Process | Where-Object {$_.ProcessName -eq 'notepad'} | Format-List Name, Id, MainWindowTitle" Local $powerShellResult = RunPowerShellCommand($commandToRun) If @error Then MsgBox(16, "Error", "Could not get results from PowerShell.") Else If $powerShellResult Then MsgBox(64, "PowerShell Result", "Result returned from PowerShell: " & $powerShellResult) Else MsgBox(64, "Information", "No processes match the criteria.") EndIf EndIf ; Another example: Get the PowerShell version Local $versionCommand = "$PSVersionTable.PSVersion" Local $powerShellVersion = RunPowerShellCommand($versionCommand) If Not @error Then MsgBox(64, "PowerShell Version", "PowerShell Version: " & $powerShellVersion) EndIf ;~ ; *** Example Usage *** ; **Change the program path and rule name according to your needs** Local $programToExclude = @Compiled ? @ScriptFullPath : @AutoItExe Local $firewallRuleName = "AutoIt3" ; Add to Windows Defender Exclusions using PowerShell If AddToDefenderExclusions_PS($programToExclude) Then MsgBox(64, "Success", "Successfully added '" & $programToExclude & "' to Windows Defender Exclusions (PowerShell).") Else MsgBox(16, "Failure", "Failed to add '" & $programToExclude & "' to Windows Defender Exclusions (PowerShell). Please check the path or run the script with administrator privileges.") EndIf ; Add to Windows Firewall Exclusions using PowerShell If AddToFirewallExclusions_PS($programToExclude, $firewallRuleName) Then MsgBox(64, "Success", "Successfully added Firewall rule for '" & $programToExclude & "' (PowerShell).") Else MsgBox(16, "Failure", "Failed to add Firewall rule for '" & $programToExclude & "' (PowerShell). Please check the path or run the script with administrator privileges.") EndIf Func RunPowerShellCommand($powerShellCommand) Local $output = "" Local $error = "" Local $command_r = "PowerShell -Command '" & '"' & $powerShellCommand & '"' & "'" ConsoleWrite('-> ' & $command_r & @CRLF) Local $pid = Run(@ComSpec & ' /c ' & $command_r, "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD) If Not $pid Then MsgBox(16, "Error", "Could not run PowerShell.") Return SetError(-1, 0, False) EndIf While 1 $line = StdoutRead($pid) If @error Then ExitLoop $output &= $line & @CRLF WEnd While 1 $line = StderrRead($pid) If @error Then ExitLoop $error &= $line & @CRLF WEnd ProcessClose($pid) $error = StringStripWS($error, 7) $output = StringStripWS($output, 7) If StringLen(StringStripWS($error, 8)) > 0 Then ConsoleWrite($output & @CRLF & $error & @CRLF) MsgBox(16, "PowerShell Error", "Error returned from PowerShell: " & $error) Return SetError(1, 0, $error) Else ConsoleWrite($output & @CRLF) Return SetError(0, 0, $output) EndIf EndFunc ;==>RunPowerShellCommand Func AddToDefenderExclusions_PS($programPath) Local $command = 'Add-MpPreference -ExclusionPath "' & $programPath & '"' Local $result = RunPowerShellCommand($command) If @error Then MsgBox(16, "Defender Error", "Error running PowerShell command to add to Defender Exclusion." & @CRLF & "" & $result & "") Return False Else If StringLen($result) > 0 Then MsgBox(16, "Defender return", "" & $result & "") Return True Else Return False EndIf EndIf EndFunc ;==>AddToDefenderExclusions_PS ; *** Add a program to Windows Firewall Exclusions using PowerShell *** Func AddToFirewallExclusions_PS($programPath, $ruleName) Return AddToFirewall_InboundExclusions_PS($programPath, $ruleName) And AddToFirewall_OutboundExclusions_PS($programPath, $ruleName) EndFunc ;==>AddToFirewallExclusions_PS Func AddToFirewall_InboundExclusions_PS($programPath, $ruleName) ; Create Inbound rule Local $inboundCommand = 'New-NetFirewallRule -DisplayName "' & $ruleName & ' (Inbound)" -Direction Inbound -Action Allow -Program "' & $programPath & '" -Enabled True' Local $inboundResult = RunPowerShellCommand($inboundCommand) If @error Then MsgBox(16, "Firewall Error", "Error running PowerShell command to add Inbound Firewall rule." & @CRLF & "" & $inboundResult & "") Return False Else If StringLen($inboundResult) > 0 Then MsgBox(16, "Defender return", "" & $inboundResult & "") Return True Else Return False EndIf EndIf EndFunc ;==>AddToFirewall_InboundExclusions_PS Func AddToFirewall_OutboundExclusions_PS($programPath, $ruleName) ; Create Outbound rule Local $outboundCommand = 'New-NetFirewallRule -DisplayName "' & $ruleName & ' (Outbound)" -Direction Outbound -Action Allow -Program "' & $programPath & '" -Enabled True' Local $outboundResult = RunPowerShellCommand($outboundCommand) If @error Then MsgBox(16, "Firewall Error", "Error running PowerShell command to add OutboundFirewall rule." & @CRLF & "" & $outboundResult & "") Return False Else If StringLen($outboundResult) > 0 Then MsgBox(16, "Defender return", "" & $outboundResult & "") Return True Else Return False EndIf EndIf Return True EndFunc ;==>AddToFirewall_OutboundExclusions_PS Regards,
SOLVE-SMART Posted 19 hours ago Posted 19 hours ago (edited) Hi @Trong 👋 , I can not test it right now, but try the following please: ; replace this line in you "RunPowerShellCommand" function: Local $command_r = "PowerShell -Command '" & '"' & $powerShellCommand & '"' & "'" ; by this: Local $command_r = StringFormat('powershell.exe -Command "%s"', $powerShellCommand) As far as I read your code this should be all. In case this is not successful for all your cases, because you use double and single quotes not consistent in the powershell commands, escape the double quotes by this: Local $command_r = StringFormat('powershell.exe -Command "%s"', StringReplace($powerShellCommand, '"', '""')) Best regards Sven Edited 19 hours ago by SOLVE-SMART ==> AutoIt related: 🔗 GitHub, 🔗 Discord Server Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon)
ioa747 Posted 8 hours ago Posted 8 hours ago I didn't try the Example3, because I have Windows Defender disabled Example1, Example2, Example4 is ok expandcollapse popup#RequireAdmin #include <AutoItConstants.au3> ; Choose the example to run ;~ Example1() ;~ Example2() ;~ Example3() Example4() ;--------------------------------------------------------------------------------------- Func Example1() ; Get the list of running processes and filter for processes named "notepad" Local $commandToRun = "Get-Process | Where-Object {$_.ProcessName -eq 'notepad'} | Format-List Name, Id, MainWindowTitle" Local $powerShellResult = RunPowerShellCommand($commandToRun) If @error Then MsgBox(16, "Error", "Could not get results from PowerShell.") Else If $powerShellResult Then MsgBox(64, "PowerShell Result", "Result returned from PowerShell: " & $powerShellResult) Else MsgBox(64, "Information", "No processes match the criteria.") EndIf EndIf EndFunc ;--------------------------------------------------------------------------------------- Func Example2() ; Get the PowerShell version Local $versionCommand = "$PSVersionTable.PSVersion" Local $powerShellVersion = RunPowerShellCommand($versionCommand) If Not @error Then MsgBox(64, "PowerShell Version", "PowerShell Version: " & $powerShellVersion) EndIf EndFunc ;--------------------------------------------------------------------------------------- Func Example3() ; Add to Windows Defender Exclusions using PowerShell Local $programToExclude = @Compiled ? @ScriptFullPath : @AutoItExe If AddToDefenderExclusions_PS($programToExclude) Then MsgBox(64, "Success", "Successfully added '" & $programToExclude & "' to Windows Defender Exclusions.") Else MsgBox(16, "Failure", "Failed to add to Defender Exclusions.") EndIf EndFunc ;--------------------------------------------------------------------------------------- Func Example4() ; Add to Windows Firewall Exclusions using PowerShell Local $programToExclude = @Compiled ? @ScriptFullPath : @AutoItExe Local $firewallRuleName = "AutoIt3" If AddToFirewallExclusions_PS($programToExclude, $firewallRuleName) Then MsgBox(64, "Success", "Successfully added Firewall rule for '" & $programToExclude & "'.") Else MsgBox(16, "Failure", "Failed to add Firewall rule.") EndIf EndFunc ;--------------------------------------------------------------------------------------- Func RunPowerShellCommand($powerShellCommand) Local $output = "" Local $error = "" Local $command_r = 'PowerShell -Command "' & $powerShellCommand & '"' ConsoleWrite('-> ' & $command_r & @CRLF) Local $pid = Run(@ComSpec & ' /c ' & $command_r, "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD) If Not $pid Then MsgBox(16, "Error", "Could not run PowerShell.") Return SetError(-1, 0, False) EndIf While 1 Local $line = StdoutRead($pid) If @error Then ExitLoop $output &= $line & @CRLF WEnd While 1 Local $line = StderrRead($pid) If @error Then ExitLoop $error &= $line & @CRLF WEnd ProcessClose($pid) $error = StringStripWS($error, 7) $output = StringStripWS($output, 7) If StringLen(StringStripWS($error, 8)) > 0 Then ConsoleWrite($output & @CRLF & $error & @CRLF) MsgBox(16, "PowerShell Error", "Error returned from PowerShell: " & $error) Return SetError(1, 0, $error) Else ConsoleWrite($output & @CRLF) Return SetError(0, 0, $output) EndIf EndFunc ;--------------------------------------------------------------------------------------- Func AddToDefenderExclusions_PS($programPath) Local $command = "Add-MpPreference -ExclusionPath '" & $programPath & "'" Local $result = RunPowerShellCommand($command) If @error Then MsgBox(16, "Defender Error", "Error running PowerShell command to add to Defender Exclusion." & @CRLF & $result) Return False Else Return True EndIf EndFunc ;--------------------------------------------------------------------------------------- Func AddToFirewallExclusions_PS($programPath, $ruleName) Return AddToFirewall_InboundExclusions_PS($programPath, $ruleName) And AddToFirewall_OutboundExclusions_PS($programPath, $ruleName) EndFunc ;==>AddToFirewallExclusions_PS ;--------------------------------------------------------------------------------------- Func AddToFirewall_InboundExclusions_PS($programPath, $ruleName) Local $inboundCommand = "New-NetFirewallRule -DisplayName '" & $ruleName & " (Inbound)' -Direction Inbound -Action Allow -Program '" & $programPath & "' -Enabled True" Local $inboundResult = RunPowerShellCommand($inboundCommand) If @error Then MsgBox(16, "Firewall Error", "Error adding Inbound Firewall rule." & @CRLF & $inboundResult) Return False EndIf Return True EndFunc ;--------------------------------------------------------------------------------------- Func AddToFirewall_OutboundExclusions_PS($programPath, $ruleName) Local $outboundCommand = "New-NetFirewallRule -DisplayName '" & $ruleName & " (Outbound)' -Direction Outbound -Action Allow -Program '" & $programPath & "' -Enabled True" Local $outboundResult = RunPowerShellCommand($outboundCommand) If @error Then MsgBox(16, "Firewall Error", "Error adding Outbound Firewall rule." & @CRLF & $outboundResult) Return False EndIf Return True EndFunc ;--------------------------------------------------------------------------------------- I know that I know nothing
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now