Powershell script to Add Task to a Job - VisualCron - Forum

Community forum

Danny van Oijen
2017-07-05T10:05:10Z
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.
Danny van Oijen
2017-07-10T10:52:51Z
Another option is that i can use the event trigger to watch a folder, whenever there is a new .ps1 file it does create a new task in a existing job with the contents of the .ps1 file.
Danny van Oijen
2017-07-13T07:25:52Z
Anyone can help me with this?
Support
2017-07-13T16:50:44Z
We have limited knowledge in doing this through PowerShell. We mainly focus on the .NET API.
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
Danny van Oijen
2017-08-03T06:53:47Z
Ok, is there another way to accomplish this?
So basically, i configured a powershell script that puts a file in a folder on the ViscualCron server, that file contains the powershell script that needs to be run.

Whenever there is a new file created there, it needs to be "imported" by VisualCron and added to a job.
Support
2017-08-03T07:55:00Z
No other way at the moment. We could do some paid development and create some PowerShell samples for you - we would need to hire external source for this because we are too busy with .NET development right now. Would this be interesting?
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
SnapDiligence
2017-08-15T17:02:15Z
Originally Posted by: Danny van Oijen 

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:
Quote:


#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:

Quote:


$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:
Quote:



#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.
Scroll to Top