none
O365 - Search-mailbox - Start-job RRS feed

  • Question

  • We have an O365 tenant with over 800,000 mailboxes. Lately we've been hit with a bunch of spam that we want to clean out of user mailboxes. We've been asked to write a script to automate the deletion of suspect messages.

    Here is the meat of our script (so far)...

    =================

     $page = 0
        do
            {
               $page = $page + 1
            $data = Get-messagetrace -senderaddress $Sender -startdate $SDate -enddate $EDate -pagesize 5000 -page $page |select recipientaddress
            $data2 = $data| sort  RecipientAddress  | group RecipientAddress | select name
            foreach ($row in $data2)
            {
            start-job -name Deleting -initializationscript (import-module msonline) -scriptblock {search-Mailbox -Identity $args[0] -SearchQuery SUBJECT:$args[1] -deletecontent -ErrorAction SilentlyContinue -Force}  -argumentlist $row.name,$Subjectline
            }
          
            }until ($data.count -lt 5000)

    =================

    Our problem is with the highlighted section. The rest works fine.

    We want to create "jobs" for each mailbox. The "jobs" are created successfully, but the fail. It appears that the variable data is not getting into the script.

    We've tried the following, but it fails too.

            start-job -name Deleting -initializationscript (import-module msonline) -scriptblock {search-Mailbox -Identity $row.name -SearchQuery SUBJECT:$Subjectline -deletecontent -ErrorAction SilentlyContinue -Force}  -argumentlist $row.name,$Subjectline

    How can we get the variable data imported into the "search-mailbox" part of the script?  The $row.name can be up to 5000 mailboxes. The $Subjectline would be static once the script is run.

    What are we missing here?

    Thanks in advance.

    Thursday, January 26, 2017 7:41 PM

Answers

  • You need to pass the arguments as parameters'

    $sb = {
    	Param(
    		$Identity,
    		$SubjectLine
    	)
    	import-module msonline
    	$search = 'SUBJECT:{0}' -f $Subjectline 
    	search-Mailbox -Identity $Identity -SearchQuery $search -DeleteContent
    }
    start-job -name Deleting -scriptblock $sb -argumentlist $row.name, $Subjectline


    \_(ツ)_/


    • Edited by jrv Thursday, January 26, 2017 8:23 PM
    • Proposed as answer by Akabe Wednesday, February 1, 2017 8:56 PM
    • Marked as answer by Richard MuellerMVP, Moderator Wednesday, February 8, 2017 3:59 PM
    Thursday, January 26, 2017 8:23 PM
  • All,

    The answer for this is that the number of Jobs we are wanting to run, would be in the thousands. Running 3 or 4 concurrent jobs is possible, but running 10s or 100s or 1000s is just not physically possible.

    Therefore, thanks to everyone.

    you can request 1000 jobs but the system may get sluggish with that many..

    Use a workflow as it can be set to run threads in batches of how-ever many your system can handle.  On a large server with many processors and memory you can run a thousand threads very easily.


    \_(ツ)_/

    Wednesday, February 1, 2017 7:59 PM

All replies

  • You need to pass the arguments as parameters'

    $sb = {
    	Param(
    		$Identity,
    		$SubjectLine
    	)
    	import-module msonline
    	$search = 'SUBJECT:{0}' -f $Subjectline 
    	search-Mailbox -Identity $Identity -SearchQuery $search -DeleteContent
    }
    start-job -name Deleting -scriptblock $sb -argumentlist $row.name, $Subjectline


    \_(ツ)_/


    • Edited by jrv Thursday, January 26, 2017 8:23 PM
    • Proposed as answer by Akabe Wednesday, February 1, 2017 8:56 PM
    • Marked as answer by Richard MuellerMVP, Moderator Wednesday, February 8, 2017 3:59 PM
    Thursday, January 26, 2017 8:23 PM
  • jrv,

    Our team has bee checking and researching 'parameters' for PowerShell. Thus far, we still do not fully understand them, nor how they should be used in our script.

    Can you elaborate on your suggestion?

    Friday, January 27, 2017 4:08 PM
  • jrv,

    Our team has bee checking and researching 'parameters' for PowerShell. Thus far, we still do not fully understand them, nor how they should be used in our script.

    Can you elaborate on your suggestion?


    help about_Parameters

    \_(ツ)_/

    Friday, January 27, 2017 4:18 PM
  • All,

    The answer for this is that the number of Jobs we are wanting to run, would be in the thousands. Running 3 or 4 concurrent jobs is possible, but running 10s or 100s or 1000s is just not physically possible.

    Therefore, thanks to everyone.

    Wednesday, February 1, 2017 7:52 PM
  • All,

    The answer for this is that the number of Jobs we are wanting to run, would be in the thousands. Running 3 or 4 concurrent jobs is possible, but running 10s or 100s or 1000s is just not physically possible.

    Therefore, thanks to everyone.

    you can request 1000 jobs but the system may get sluggish with that many..

    Use a workflow as it can be set to run threads in batches of how-ever many your system can handle.  On a large server with many processors and memory you can run a thousand threads very easily.


    \_(ツ)_/

    Wednesday, February 1, 2017 7:59 PM