1

I am working on a sysvol check script for active directory monitoring. Completed the script which checks connectivity to sysvol on all the domain controllers in the given domain. As part of this i am doing the below check

Start-Job -sb "(Measure-Command{Dir \\$DCName\sysvol\$Domain\Policies}).Milliseconds"

this returns the time taken in milliseconds; unfortunately it returns the time even the sysvol is inaccessible and its even faster compared to if its accessible, due to obvious reasons.

My question is when i do receive-Job , is there a way to find out if job has errors? because measure command doesn't give the whole picture. Please let me know if any other additional questions regarding this.

Mathias R. Jessen
  • 24,907
  • 4
  • 62
  • 95
Darktux
  • 827
  • 5
  • 20
  • 36
  • Seems like it would be far more straight forward if the share/path existed, then perform the throughput test. – Greg Askew Nov 27 '15 at 18:38
  • Problem is i do entirely separate loop for receiving data for every completed job and write it to a file; if i skip a job because the path isn't there; i wont be receiving anything regarding that DC. Also my spreadsheet is a appending every-time i run the script by adding a new column labeled by current run time, so i don't have any control over cells to put data outside of jobs. Please let me know if my explanation doesn't make any sense ,i can explain. – Darktux Nov 27 '15 at 19:05

1 Answers1

6

First off, I don't understand your syntax. I'm running PS v5 on Windows 10 and your code snippet doesn't even run, because you fed Start-Job a string, not a script block. And -sb was not an acceptable alias/shorthand for -ScriptBlock for me.

But who knows, maybe that was legal in PS v4. That's beside the point.

You should add -ErrorAction Stop to your dir/gci/get-childitem cmdlet so that it produces a "terminating" error if it fails.

Behold, here is the job when I do not specify -ErrorAction Stop:

PS C:\> $MyJob = Start-Job -ScriptBlock { (Measure-Command{Dir \\$DCName\sysvol\$Domain\Policies}).Milliseconds }
PS C:\> $MyJob.State
Running
PS C:\> $MyJob.State
Completed

I won't know whether the job actually succeeded or failed. The state of the job simply goes from Running to Completed, regardless of the job's success.

Now let's try it again with a terminating error:

PS C:\> $MyJob = Start-Job -ScriptBlock { (Measure-Command{Dir \\$DCName\sysvol\$Domain\Policies -ErrorAction Stop}).Milliseconds }
PS C:\> $MyJob.State
Running
PS C:\> $MyJob.State
Failed

Now you see that the same job enters a state of Failed. You now know your job failed. In addition, if you were to save the results of the job with

PS C:\> $Result = Receive-Job $MyJob

$Result will be null if the job failed.

If you want to store the specific error that occurred within the job, try this:

PS C:\> Receive-Job $MyJob -ErrorVariable Result
PS C:\> $Result
Cannot find path '\\\sysvol\\Policies' because it does not exist.
+ CategoryInfo          : ObjectNotFound: (\\\sysvol\\Policies:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
+ PSComputerName        : localhost
Ryan Ries
  • 55,011
  • 9
  • 138
  • 197
  • Ryan you are right, i didnt want to confuse y'all so shortened my syntax; below is the full version; $Block = "(measure-Command{Dir \\$DCName\sysvol\$Domain\Policies}).Milliseconds" $sb = [scriptblock]::Create($Block) Start-Job -ScriptBlock $sb -Name $DCName PS: Dang code notation is not at all working, tried to put 4 spaces at start still it shows normally. – Darktux Dec 01 '15 at 14:02