0
votes

I am writing a script to automate the backup of files to a USB flash memory. Not all memory sticks have the same drive ID. The script searches for a path to drives E:, F:, G: and H: and reports failure to find a drive if no flash drive is connected. If the flash drive is subsequently connected, the test still returns False.

Try this. Plug a USB memory stick, known to be drive E:, in and from the Powershell console test the path to E:. It returns True. Now remove the memory stick and test the path to drive E: and False is returned, as expected. Now plug the USB stick back in and repeat the test-path E: cmdlet and False is returned. Why?

1

1 Answers

0
votes

For me it works,

Test-Path d:\

return True, it just take some time.

You can use this to receive an event when a volume appear :

$query = "SELECT * FROM Win32_VolumeChangeEvent"
Register-WMIEvent -Query $query -Action {$global:a=$Args[0];$global:b=$Args[1];$global:c=$event;write-host "done"}
# Now plug your stick
# Then have a look to :
$b.NewEvent
__GENUS             : 2
__CLASS             : Win32_VolumeChangeEvent
__SUPERCLASS        : Win32_DeviceChangeEvent
__DYNASTY           : __SystemClass
__RELPATH           : 
__PROPERTY_COUNT    : 4
__DERIVATION        : {Win32_DeviceChangeEvent, __ExtrinsicEvent, __Event, __IndicationRelated...}
__SERVER            : 
__NAMESPACE         : 
__PATH              : 
DriveName           : D:
EventType           : 2
SECURITY_DESCRIPTOR : 
TIME_CREATED        : 130368370362465996
PSComputerName      : 

# Now unplug your stick
# Then have a look to :
$b.NewEvent
__GENUS             : 2
__CLASS             : Win32_VolumeChangeEvent
__SUPERCLASS        : Win32_DeviceChangeEvent
__DYNASTY           : __SystemClass
__RELPATH           : 
__PROPERTY_COUNT    : 4
__DERIVATION        : {Win32_DeviceChangeEvent, __ExtrinsicEvent, __Event, __IndicationRelated...}
__SERVER            : 
__NAMESPACE         : 
__PATH              : 
DriveName           : D:
EventType           : 3
SECURITY_DESCRIPTOR : 
TIME_CREATED        : 130368373636563263
PSComputerName      :