Hi all,
I need a powershell scipt that will use the api to add a task to a job.
It needs to add a PowerShell Task with credentials set, scope local, and predefined PowerShell script text.
I'll add more if I remember after I get off work, but I mostly use the API through PowerShell. Basically, to add a task to a job you're going to need to first load up the API dll and connect to your server. You can make the load-to-connection object into functions kind of like this:
#Function that loads VisualCron's API dlls.
function Load-VCAPIDLL {
param(
[Parameter()]
[string]$VCPath = "C:\Program Files (x86)\VisualCron\VisualCron.dll",
[Parameter()]
[string]$VCAPIPath = "C:\Program Files (x86)\VisualCron\VisualCronAPI.dll"
)
$VC = [Reflection.Assembly]::LoadFrom($VCPath);
$VCAPI = [Reflection.Assembly]::LoadFrom($VCAPIPath);
}
#Returns a VisualCronAPI.Server object that can be used to interact with target VisualCron server.
function ConnectTo-VCServer {
param(
[Parameter(Mandatory=$true)]
[string]$username,
[Parameter(Mandatory=$true )]
[string]$password,
[Parameter( Mandatory=$true)]
[alias("address")]
[string]$VCServerAddress,
[Parameter( Mandatory=$true )]
[alias("connection")]
[string]$VCServerConntype,
[Parameter()]
[alias("port")]
[string]$VCServerPort
)
#Call the dll loading fn
Load-VCAPIDLL
#Create new connection objects
$ClientConnectionObj =New-Object -TypeName VisualCronAPI.Client
$ServerConnectionObj = New-Object -TypeName VisualCronAPI.Server
$APIConnectionObj = New-Object -TypeName VisualCronAPI.Connection
#Assign provided params to APIConnectionObj
$APIConnectionObj.Address = $VCServerAddress
$APIConnectionObj.UserName = $username
$APIConnectionObj.PassWord = $password
if ($VCServerPort -ne $null)
{
$APIConnectionObj.Port
}
$APIConnectionObj.ConnectionType = $VCServerConntype
#Using the ClientConnectionObj, pass in the APIConnectionObj to update ServerConnectionObj.
#This creates a connection to the target VisualCron server.
$ServerConnectionObj = $ClientConnectionObj.Connect($APIConnectionObj, $true)
#Return VisualCronAPI.Server object
Return $ServerConnectionObj
}
Those functions were cribbed from other posts around here. Once you have the connection object up, you can navigate through it just like any other PowerShell object. In this case, you want to add a task to a job that already exists:
$username = "admin"
$password ="password"
$address = "localhost"
$connection = "local"
$port = ""
#Example ID of the job that you want to modify
[string]$jobID = '63dfb7bd-a643-4a22-a671-e24342b2d561'
#connect to your VC Server
$VCServerConnectionObj = ConnectTo-VCServer $username $password $address $connection $port
#Find your job on the server and return the job object
$jobToEdit = $VCServerConnectionObj.Jobs.Get($jobID)
#Now, the method you want here would be "$jobToEdit.AddTask() or $jobToEdit.Tasks.Add()". Those methods, however, requires a task object if you look at #the intellisense in the ISE. Let's make one.
$taskToAdd = New-Object VisualCron.TaskClass
#As an example, modify the task with a name:
$taskToAdd.Name = "Test Task"
#Then insert the task into the job object:
$jobToEdit.Tasks.Add($taskToAdd)
#And update the job object back on the server
$VCServerConnectionObj.Jobs.Update($jobToEdit)
Of course, if you want to instead create a new job and push it to the server you'd do the following:
#Create a new job object
$newJob = New-Object VisualCron.JobClass
#Using the previous connection object
$VCServerConnectionObj.Jobs.Add($newJob)
Generally, I've found that if the GUI can do something, PowerShell can to.
Edited by user
2017-08-17T16:09:04Z
|
Reason: Not specified