forked from vxav/Scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start-PowerCLIJob.ps1
34 lines (28 loc) · 1012 Bytes
/
Start-PowerCLIJob.ps1
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
Function Start-PowerCLIJob {
param(
[parameter(mandatory=$True)]
[VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl]
$DefaultVIServer,
[string]
$JobName,
[parameter(mandatory=$True)]
[scriptblock]
$ScriptBlock,
[object[]]
$ArgumentList,
[psobject]
$InputObject,
[string[]]
$Modules = "VMware.VimAutomation.Core"
)
$ScriptBlockPrepend = {import-module $using:Modules | out-null;
Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false -Scope Session -confirm:$False | out-null;
Connect-ViServer -Server $using:DefaultVIServer.name -session $using:DefaultVIServer.SessionSecret | out-null;
}
$ScriptBlock = [ScriptBlock]::Create($ScriptBlockPrepend.ToString() + $ScriptBlock.ToString())
$params = @{scriptblock=$ScriptBlock}
if ($JobName) {$params.Add('name',$JobName)}
if ($ArgumentList) {$params.Add('ArgumentList',$ArgumentList)}
if ($InputObject) {$params.Add('InputObject',$InputObject)}
Start-Job @params
}