none
What am I missing? RRS feed

  • Question

  • Hello All, this is my first post here. I'm a self-taught PSer and I'm having a problem understanding why a command won't execute.

    Here's the code:

        Function DirTreeToFile {
        Param (
               [string] $SourceDrivePath 
              )
    
        Clear-Host
    
        $VolLabel = Get-Volume $SourceDrivePath.Substring(0,1) 
        $VolLabel = $VolLabel.FileSystemLabel
    
        if ($VolLabel -eq "") {
          $VolLabel = "NoVolLabel"
        }
    
        Write-Host "Source: $SourceDrivePath"
    
        Get-ChildItem -Path "$SourceDrivePath\"  -Recurse -Force  
    
        Write-Host "Done!"
        }
    
        #Query for finding all device arrival events
    
        $query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType=2"            
    
        # Define a PowerShell ScriptBlock that will be executed when an event occurs
    
        $Action = { DirTreeToFile  $Event.SourceEventArgs.NewEvent.DriveName }
    
        # Create the event registration
        # NOTE: Event Registration only good while the PS Shell is OPEN!!!!!!!!!
    
        Register-WmiEvent -Query $Query -Action $Action -SourceIdentifier USBFlashDrive;

    The WMI event registers properly and when I insert a USB drive the function is called and the debug statements print out the correct drive letter and then the Done! message. However, the Get-ChildItem command provides no output? I'm at a loss. Any Ideas what is causing this? Am I missing something obvious or is this some kind of weird situation? Thanks.

    Note: Cross Posted here: http://windowssecrets.com/forums/showthread.php/168017-It-s-head-scratching-time-again!?p=996446#post996446




    Thursday, April 16, 2015 7:22 PM

All replies

  • Under even ideal conditions this is all out-of-scope or, at the least, impossible.


    \_(ツ)_/

    Thursday, April 16, 2015 10:59 PM
  • Well maybe not quite impossible as this code modified from code by Cliff.H in the referenced post in my original post shows.

    $Query = "SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2"
    # Create the event registration
    # NOTE: Event Registration only good while the PS Shell is OPEN!!!!!!!!!
    Register-WmiEvent -Query $Query -SourceIdentifier USBFlashDrive
    
    Do {
    
    	$event = Wait-Event -SourceIdentifier USBFlashDrive
    	$drive = $event.SourceEventArgs.NewEvent.TargetInstance.Caption
    	$VolLabel = $event.SourceEventArgs.NewEvent.TargetInstance.VolumeName
    
        if ($VolLabel -eq "") {
          $VolLabel = "NoVolLabel"
        }
    
        $DestDrivePath = "G:\Test\$VolLabel.txt"   #Replace drive\dir with your values
    
    	Get-ChildItem -Path $drive -Recurse -Force | `
           Out-File -FilePath "$DestDrivePath" -Force -Width 75
    	Remove-Event -SourceIdentifier USBFlashDrive
    
    } While ( $True )
    
    # NOTE: Press CTRL+C to break out of the loop, then execute the following command
    #	Unregister-Event -SourceIdentifier USBFlashDrive
    # OR, close the PS Host

    Saturday, April 18, 2015 8:30 PM
  • You cannot read the drive during the event.  The drive will only be visible after the event action is complete.

    The second code handles the event by polling which is completely different from your original question.

    If the second method does what you want then use it.  The first method the action requests information that is out of scope and so impossible..


    \_(ツ)_/


    • Edited by jrv Saturday, April 18, 2015 9:51 PM
    Saturday, April 18, 2015 9:48 PM
  • Ok, that makes more sense. Now originally, I had the registered event calling an external file (placed in the OP code as a function) and passing the drive letter. Just what classifies as during the event? How long does it last? Why wouldn't it work when calling a second program?

    # Define a WMI event query, that looks for new instances of Win32_LogicalDisk where DriveType is "2"
    # http://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx
    $Query = "select * from __InstanceCreationEvent within 5 where TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2";
    
    # Define a PowerShell ScriptBlock that will be executed when an event occurs
    $Action = { & G:\BEKDocs\Scripts\DirTreeToFile.ps1  $event.SourceEventArgs.NewEvent.TargetInstance.Caption;  };
    
    # Create the event registration
    # NOTE: Event Registration only good while the PS Shell is OPEN!!!!!!!!!
    Register-WmiEvent -Query $Query -Action $Action -SourceIdentifier USBFlashDrive;
    
    $Query = "select * from __InstanceCreationEvent within 5 where TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 3";
    
    # Define a PowerShell ScriptBlock that will be executed when an event occurs
    #$Action = { & G:\BEKDocs\Scripts\DirTreeToFile.ps1  $event.SourceEventArgs.NewEvent.TargetInstance.Caption;  };
    
    # Create the event registration
    # NOTE: Event Registration only good while the PS Shell is OPEN!!!!!!!!!
    #Register-WmiEvent -Query $Query -Action $Action -SourceIdentifier USBHDDrive;
    

    Called File:

    Param (
           [string] $SourceDrivePath 
          )
    
    Clear-Host
    
    $FileTypeToList = "*"
    $VolLabel = Get-Volume $SourceDrivePath.Substring(0,1) 
    $VolLabel = $VolLabel.FileSystemLabel
    
    if ($VolLabel -eq "") {
      $VolLabel = "NoVolLabel"
    }
    
    $DestDrivePath = "G:\Test\$VolLabel.txt"   #Replace drive\dir with your values
    
    
    Write-Host "Source: $SourceDrivePath - Volume Label: $VolLabel  Dest: $DestDrivePath"
    Write-Host "Filter: *.$FileTypeToList"
    
    Get-ChildItem -Path "$SourceDrivePath\"  -Recurse -Force  
    
    Write-Host "Done!"

    I'd really like to understand what is going on so I don't make the same mistakes in the future.

    Thanks for your assistance.

    Saturday, April 18, 2015 11:58 PM
  • Doesn't matter.  In a function or out a function.  In a zoo or without a clue.  In a box or in your sox. 

    Not in a box. Not with a fox.
    Not in a house. Not with a mouse.
    I would not eat them here or there.
    I would not eat them anywhere.
    I would not eat green eggs and ham.
    I do not like them, Sam-I-Am.

    When the scope changes ot when you are in a delegate the changes that happen are not visible.  You must rejoin the parent to see the changes.  When you poll  you are doing it from the parent so the change is visible.

    Sorry - best I can do on short notice.

    (besides it is hard to think about this while listening to "Rhapsody in Blue")


    \_(ツ)_/

    Sunday, April 19, 2015 12:07 AM