system.management

Catch USB plug and unplug event System.InvalidCastException

浪尽此生 提交于 2019-11-28 06:00:19
问题 I'm trying to detect USB device insertion and remove with WinForm desktop C# application: public Form1() { InitializeComponent(); USB(); } then: private void USB() { WqlEventQuery weqQuery = new WqlEventQuery(); weqQuery.EventClassName = "__InstanceOperationEvent"; weqQuery.WithinInterval = new TimeSpan(0, 0, 3); weqQuery.Condition = @"TargetInstance ISA 'Win32_DiskDrive'"; var m_mewWatcher = new ManagementEventWatcher(weqQuery); m_mewWatcher.EventArrived += new EventArrivedEventHandler(m

Create PSCredential without a password

自闭症网瘾萝莉.ら 提交于 2019-11-28 02:47:29
问题 How to create a instance of PSCredential that has no password? (Without manually filling out a Get-Credential dialog with no password, this is for unattended running.) Things I tried: $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) Error: Cannot process argument because the value of argument "password" is null $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) Error: ConvertTo