I've written a shell script that will exclude a folder during Microsoft Defender Antivirus scanning. I don't have a lot of experience with shell scripting, and this is what I come up with:
Set-MpPreference -ExclusionPath D:\vpostest
echo "Exclusion Success"
Read-Host -Prompt "Press Enter to exit"
The first line of the code is where I mention the exclusion, and the following is just some kind of prompt to notify the user that the exclusion is a success (it is still a work in progress).
Once I run the script, I receive an error:
Set-MpPreference : Operation failed with the following error: 0x%1!x!
At C:\Users\zainur.ariffin\Desktop\Powershell test\autoExclusion.ps1:1 char:1
+ Set-MpPreference -ExclusionPath D:\vpostest
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (MSFT_MpPreference:root\Microsoft\...FT_MpPreference) [Set-MpPreference], CimException
+ FullyQualifiedErrorId : HRESULT 0xc0000142,Set-MpPreference
If I'm not mistaken, I've read that the following error indicates that Microsoft Defender Antivirus was not enabled. I have already enabled this feature and restarted my computer, and even check with 'Get-MpComputerStatus'. However, the same error keeps appearing. Why is this happening?
This is for anyone who have the same problem as me
So I found out that the reason why the script file doesn't run properly is because I didn't run powershell as administrator. But how to run the shell script file as administrator?
I have tried to create a shortcut from the script file, and try to enable 'run as administrator' from the properties, but the option is greyed out. Meaning I couldn't choose that option so I have to find another way.
So I come to a conclusion that I need to create a script file that will grant admin privileges to the script that I want. This is because it's not possible to grant administrative privileges to a script within the script itself. The following is the command I use to grant admin privileges:
The "
$scriptFile" need to be the script file that you intended to use. I hope this is useful for people who have the same problem as me.