自动执行脚本

时间:2012-12-21 10:20:24

标签: windows powershell

我希望在没有任何软件的情况下插入或插入USB设备时在Windows中执行脚本。我需要一个可以做到这一点的脚本。我在互联网上搜索但没有找到有用的东西。请帮帮我。

2 个答案:

答案 0 :(得分:2)

您可以订阅WMI活动:

$query = "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND TargetInstance.DriveType=2"

Register-WmiEvent -Query $query -SourceIdentifier RemovableDiskDetection -Action {

    $class = $eventArgs.NewEvent.__CLASS
    $device = $eventArgs.NewEvent.TargetInstance.DeviceID

    switch($class)
    {
        __InstanceCreationEvent {
            Write-Host "Inserted, device id: $device"
        }

        __InstanceDeletionEvent {
            Write-Host "Removed, device id: $device"
        }
    }
}

答案 1 :(得分:-1)

您正在寻找autorun.inf

但是,在Windows Vista及更高版本中禁用了自动运行脚本的功能,以阻止病毒传播。