get-job state blocked

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

  • Author
  • #25583

    Fady Ghalayiny

    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


  • #25584

    Dave Wyatt

    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

    Don Jones

    You may need to add

    -force -confirm:$false

    To Remove-Item, also,

  • #25586

    Fady Ghalayiny

    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

    Dave Wyatt

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

  • #25588

    Fady Ghalayiny

    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

    Dave Wyatt

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

  • #25590

    Rob Simmers

    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.