PowerShell script in Intune Device Configuration Proile RRS feed

  • Question

  • Hi all,

    I am trying to run a PowerShell scripts under the Device configuration-PowerShell scripts.

    What the PowerShell script do is it will autoscan a USB device for malware, viruses once it is plugged to a laptop.

    The script works when I run it locally.

    Then I put it in the PowerShell scripts profile under the Device configuration in Microsoft Azure Intune.

    It seem like it is not working at all as I have tested it on a laptop used for testing. 

    This is the script command:

    function UsbMountWatcher {
        $alarm = New-Object System.Management.EventQuery
        $alarm.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"
        New-Object System.Management.ManagementEventWatcher $alarm
    $pathtompcmdrun = $env:PROGRAMFILES + "\Windows Defender\MpCmdRun.exe"
    $watcher = UsbMountWatcher
    while ($true) {
        $event = $watcher.WaitForNextEvent()
        $driveletter = $event.Properties["DriveName"].Value.ToString() + "\"
        &$pathtompcmdrun "-Scan" "-File" $driveletter "-DisableRemediation"
        Write-Output $LASTEXITCODE

    Please take a look and perhaps help me to amend it so that when I run it in the Intune Device Configuration profile, it will

    autoscan  the USB thumbdrive when it is plug into a laptop.

    By the way, the script is downloaded from the web.

    Friday, October 11, 2019 2:30 AM

All replies

  • Since this is an Intune Specific issue you will have to post it in an Intune forum or contact MSSupport for assistance,

    As you noted the script works as a script.


    Friday, October 11, 2019 2:36 AM