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

Community forum

Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
View
Go to last post Go to first unread
Offline Danny van Oijen  
#1 Posted : Wednesday, July 5, 2017 12:05:10 PM(UTC)
Danny van Oijen

Rank: Paid support

Joined: 5/16/2017(UTC)
Posts: 80
Netherlands
Location: Limburg, Venlo

Thanks: 2 times
Was thanked: 4 time(s) in 4 post(s)
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.

Edited by moderator Wednesday, August 16, 2017 1:40:23 PM(UTC)  | Reason: Not specified

Offline Danny van Oijen  
#2 Posted : Monday, July 10, 2017 12:52:51 PM(UTC)
Danny van Oijen

Rank: Paid support

Joined: 5/16/2017(UTC)
Posts: 80
Netherlands
Location: Limburg, Venlo

Thanks: 2 times
Was thanked: 4 time(s) in 4 post(s)
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.
Offline Danny van Oijen  
#3 Posted : Thursday, July 13, 2017 9:25:52 AM(UTC)
Danny van Oijen

Rank: Paid support

Joined: 5/16/2017(UTC)
Posts: 80
Netherlands
Location: Limburg, Venlo

Thanks: 2 times
Was thanked: 4 time(s) in 4 post(s)
Anyone can help me with this?
Offline Support  
#4 Posted : Thursday, July 13, 2017 6:50:44 PM(UTC)
Support

Rank: Official support

Joined: 2/23/2008(UTC)
Posts: 11,258

Thanks: 877 times
Was thanked: 450 time(s) in 427 post(s)
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!
Offline Danny van Oijen  
#5 Posted : Thursday, August 3, 2017 8:53:47 AM(UTC)
Danny van Oijen

Rank: Paid support

Joined: 5/16/2017(UTC)
Posts: 80
Netherlands
Location: Limburg, Venlo

Thanks: 2 times
Was thanked: 4 time(s) in 4 post(s)
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.
Offline Support  
#6 Posted : Thursday, August 3, 2017 9:55:00 AM(UTC)
Support

Rank: Official support

Joined: 2/23/2008(UTC)
Posts: 11,258

Thanks: 877 times
Was thanked: 450 time(s) in 427 post(s)
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!
Offline SnapDiligence  
#7 Posted : Tuesday, August 15, 2017 7:02:15 PM(UTC)
SnapDiligence

Rank: Paid support

Joined: 2/17/2017(UTC)
Posts: 10
United States
Location: Texas, Houston

Thanks: 2 times
Was thanked: 3 time(s) in 3 post(s)
Originally Posted by: Danny van Oijen Go to Quoted Post
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.

Edited by user Thursday, August 17, 2017 6:09:04 PM(UTC)  | Reason: Not specified

thanks 1 user thanked SnapDiligence for this useful post.
Support on 8/16/2017(UTC)
Users browsing this topic
Guest
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Scroll to Top