get-job state blocked

Tagged: ,

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 years, 2 months ago.

  • Author
    Posts
  • #25583
    Profile photo of Fady Ghalayiny
    Fady Ghalayiny
    Participant

    Hello everyone I'm trying to build a script in part of the script it will search for a specific filename in drive and will delete it if found I created a job
    start-job -scriptblock { Get-ChildItem d:\ -Include $filename -Recurse -Force | Remove-Item }
    any idea what could be missing I checked technet they only mentioned that blocked means it's waiting user intervention any help please

    Best

  • #25584
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Best guess, you've got a directory that's matching your $filename as well, the directory contains subfolders or files, and you haven't used the -Recurse parameter, which results in a prompt for the user.

    If you only want this to target files rather than directories, you can do this one of two ways:

    # On Powershell 3.0 or later, just use the -File switch to GetChildItem:
    Get-ChildItem d:\ -Include $filename -Recurse -Force -File | Remove-Item 
    
    # In PowerShell 2.0, filter with Where-Object
    Get-ChildItem d:\ -Include $filename -Recurse -Force | Where-Object { -not $_.PSIsContainer } | Remove-Item 
    
  • #25585
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You may need to add

    -force -confirm:$false

    To Remove-Item, also,

  • #25586
    Profile photo of Fady Ghalayiny
    Fady Ghalayiny
    Participant

    start-job -scriptblock { Get-ChildItem -path e:\ -Include example.txt -Recurse -Force -confirm:$false | Remove-Item -force -confirm:$false }
    the state now failed I created a small virtual drive e:\ and copied many folders in it and in each folder contains example.txt just for texting purpose but nothing happend and state is failed

  • #25587
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Try running Receive-Job, to see what the errors were.

  • #25588
    Profile photo of Fady Ghalayiny
    Fady Ghalayiny
    Participant

    PS E:\> get-job -name job36 | receive-job -keep
    A parameter cannot be found that matches parameter name 'confirm'.
    + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName : localhost

  • #25589
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah, that's only on Remove-Item, not Get-ChildItem.

  • #25590
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Remove-Item may need the switch, not Get-ChildItem:

    start-job -scriptblock { Get-ChildItem -path e:\ -Include example.txt -Recurse  | Remove-Item -force -confirm:$false }
    

You must be logged in to reply to this topic.