forked from sqlrodbloke/sqlrodscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSJobScript.ps1
More file actions
55 lines (39 loc) · 1.4 KB
/
PSJobScript.ps1
File metadata and controls
55 lines (39 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<#
Install-Module -Name ThreadJob -Force
import-module ThreadJob
#>
$TaskName='GetSQLInfo'
$throttle = (Get-WmiObject –class Win32_processor).NumberOfCores
clear
# Our list of targets goes here.
$Serverlist=@()
$Serverlist='Server1','Server2','Server3'
# Script Block of what you actually want to run.
$GetSQLInfo = {
Param($server)
$sqloutput=(Invoke-Sqlcmd -ServerInstance $server -Database master -Query 'SELECT @@servername AS Servername, getdate() as dt')
[pscustomobject]@{
Returnedserver=$sqloutput.Servername
Returnedtime=$sqloutput.dt
}
}
# End of Script Block to run.
$i=0
ForEach($Server in $serverlist)
{
$i++
while( @(Get-Job -State Running).Count -ge $Throttle)
{
Write-Host "Max concurrency reached...Throttling..." -ForegroundColor Yellow
Start-Sleep -Milliseconds 500
}
#Call a job for the task for the server
Start-Job -Name $TaskName-$server -ScriptBlock $GetSQLInfo -ArgumentList $server
#Or a Threaded Job
#Start-ThreadJob -Name $TaskName-$server -ScriptBlock $GetSQLInfo -ArgumentList $server -ThrottleLimit $throttle
}
Write-Host "Threads started for all targets. Collating." -ForegroundColor Green
#Return results from the jobs
Get-Job | Wait-Job | Receive-Job | Select ReturnedServer, Returnedtime
<#Now Tidy Up#>
Get-Job | Remove-Job -Force