locked
Running Jobs RRS feed

  • Question

  • Hello,

    We have an issue around zeroing drives which we need to do on all of our servers. So I have some code which gets them all and then gets each fixed disks and runs sdelete64.exe on each one to clean the drive. This seems to work well however I cant seem to get the jobs part correctly placed. So I have a number of jobs I would like running at any one time which seems to work but for some reason the output is not as expected it seems to show as completed immediately with a command as ...

    Here is the code I am running: 

    $Jobs = 19
        
    Get-ADComputer -filter * -SearchBase "OU" -SearchScope SubTree | 
    Sort-Object | 
    ForEach-Object{ 
    
    $Computer = $_.Name
    	Do
        {
        $Job = (Get-Job -State Running | measure).count
        } Until ($Job -le $Jobs)
    
    Start-Job -Name $Computer -ScriptBlock {
    
    # Get All Fixed Disks
    $Drives = Get-WmiObject –query “select * from win32_logicaldisk where DriveType = '3'” -computername $Computer
    
    $Drives | 
    ForEach-Object{
    
    $DeviceID = $_.DeviceID
    
    C:\SysinternalsSuite\sdelete64.exe -z $DeviceID
    
    }
     }
     
    Get-Job -State Completed | Remove-Job
       }
    
    Wait-Job -State Running
    Get-Job -State Completed | Remove-Job
    Get-Job

    I am also trying to work out how I would incorporate a text file or something similar for in case the machine is rebooted or something so that it can continue from where it left off? 

    Oh the output is as follows: 

    PSJobTypeName   State         HasMoreData     Location     Command
    BackgroundJob   Completed     True            localhost            ...
    BackgroundJob   Completed     True            localhost            ...
    BackgroundJob   Completed     True            localhost            ...

    Any Help would be greatly appreciated.

    Thanks

    James

    Thursday, April 25, 2019 9:06 AM

All replies

  • This program: "C:\SysinternalsSuite\sdelete64.exe" has to be on every computer you are doing this with.

    You also need to do a Rececive-Job to get an error messages.


    \_(ツ)_/

    Thursday, April 25, 2019 10:55 AM
  • Your code will not work on remote systems.  You need to use Invoke-Command.


    You need to spend some time reading the help for the job CmdLets.  It will help you to understand what you are doing wrong.


    \_(ツ)_/




    • Edited by jrv Thursday, April 25, 2019 11:07 AM
    Thursday, April 25, 2019 11:00 AM
  • $sb = {
        Get-WmiObject win32_logicaldisk -Filter 'DriveType=3'|
            ForEach-Object{
                # this must exists on remote systems
                C:\SysinternalsSuite\sdelete64.exe -z $_.DeviceID
            }
    }
    
    $Jobs = Get-ADComputer -filter * -SearchBase 'OU...' |
        ForEach-Object{
            Invoke-Command $_.Name -ScriptBlock $sb -AsJob
        }
    
    $Jobs | Wait-Job | Receive-Job
    $Jobs | Remove-Job


    \_(ツ)_/

    Thursday, April 25, 2019 11:08 AM
  • You can also just do this:

    $computers = Get-ADComputer -filter * -SearchBase 'OU...' | select -ExpandProperty Name
    $Jobs = Invoke-Command $computers -ScriptBlock $sb -AsJob
    


    \_(ツ)_/

    Thursday, April 25, 2019 11:09 AM
  • Thank you for the responses. 

    I will give it a go I will also need the number of jobs to be in there so it doesn't start to zero every drive on every server at the same time as the hardware wont cope so we need to limit this to only being done 20 times. 

    Given your code above how would this fit in as I think it would just run all of the commands one after the other? 

    Thanks

    James

    Monday, April 29, 2019 8:11 AM
  • What hardware won't support what?  The commands are executed on the remote systems.  Nothing is executed locally.


    \_(ツ)_/

    Monday, April 29, 2019 8:30 AM
  • Yes however as were running virtual machines it will impact on the hardware underlying all the VM's which is why I am limiting the number of simultaneous jobs.
    Monday, April 29, 2019 10:41 AM
  • The system will throttle this automatically based on the limits placed on the VMs.  "sdelete" does not encumber a system.  It is a background task.  THe jobs will be released in batches of 32 by default.

    To learn how to run jobs read the help for the CmdLet.

    help about_jobs
    help about_Remote_Jobs


    \_(ツ)_/

    Monday, April 29, 2019 10:54 AM