I am trying to create a function that Exports all settings in VisualCron via Powershell through VisualCron API
I first create a function to load the DLLS
Then I create the function to establish a connection
These work fine
I however struggle with inputing a value in the ExportSettings() method that is accepted. I always get a null value no matter what I try.
#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
}
##Export VisualCron settings to import in QA environment
function Export-VCSettings {
param(
[Parameter(Mandatory=$true)]
[string]$Usedefaultfile,
[Parameter(Mandatory=$true )]
[string]$IncludeallConnections,
[Parameter( Mandatory=$true)]
[string]$IncludeAllCredentials,
[Parameter( Mandatory=$true )]
[alias("Svrsettings")]
[string]$IncludeAllServerSettings,
[Parameter()]
[alias("InclAllJobs")]
[string]$IncludeAllJobs,
[Parameter()]
[alias("InclAllPerm")]
[string]$IncludeallPermissions,
[Parameter()]
[alias("IncludeCerts")]
[string]$IncludeAllCertificates,
[Parameter()]
[alias("JobObjects")]
[string]$Jobjcts
)
{
$global:Server = New-Object VisualCronAPI.Server
$eip = New-Object VisualCron.ExportImportProgressClass
$eip.UseDefaultFile = $Usedefaultfile
$eip.IncludeAllCertificates = "true"
$eip.IncludeAllConnections = $IncludeallConnections
$eip.IncludeAllCredentials = $IncludeAllCredentials,
$eip.IncludeAllServerSettings = $IncludeAllServerSettings
$eip.IncludeAllJobs = $IncludeAllJobs
$eip.IncludeAllPermissions = $IncludeallPermissions
$eip.JobObjects = $global:Server.jobs.GetAll()
}
$escr = New-Object VisualCron.ExportSettingsResponseClass
$escr = $global:Server.ExportSettings($eip)
if ($escr.Success -and $escr.FileBytes -ne 'null') {
[system.IO.File.Writeallbytes]::("c:\VC-Settings.zip", $escr.FileBytes)
}}
Export-VCSettings -Usedefaultfile $true -IncludeallConnections $true -IncludeAllCredentials $true -IncludeAllServerSettings $true -IncludeAllJobs $true -IncludeallPermissions $true -IncludeAllCertificates $true -Jobjcts "all"
However it keeps telling me the below
You cannot call a method on a null-valued expression. At line:47 char:1
- $escr = $global:Server.ExportSettings($eip)
-
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
I was basing it off of this C# example
if (s.Connected)
{
Console.WriteLine("Connected to Server");
ExportImportProgressClass eip = new ExportImportProgressClass();
eip.UseDefaultFile = false;
eip.IncludeAllCertificates = false;
eip.IncludeAllConditions = false;
eip.IncludeAllConnections = false;
eip.IncludeAllCredentials = false;
eip.IncludeAllExitCodes = false;
eip.IncludeAllNetworkDrives = false;
eip.IncludeAllNotifications = false;
eip.IncludeAllPermissions = false;
eip.IncludeAllPGPKeyRings = false;
eip.IncludeAllServerSettings = false;
eip.IncludeAllTimeExceptions = false;
eip.IncludeAllUserGroups = false;
eip.IncludeAllVariables = false;
eip.IncludeAllJobs = false;
/*var jobs = (from j in s.Jobs.GetAll()
where j.Group == "WD Standard"
select j.Name).ToList();*/
//eip.Jobs = jobs;
//eip.JobObjects = s.Jobs.GetAll().Where(item => item.Group == "WD Standard").ToList();
eip.JobObjects = s.Jobs.GetAll();
ExportSettingsResponseClass esrc = s.ExportSettings(eip);
if (esrc.Success && esrc.FileBytes != null)
{
System.IO.File.WriteAllBytes("C:/Temp/VC-Settings.zip", esrc.FileBytes);
}
s.Disconnect();
}
Any ideas or help or suggestions would be most appreciated and sorry if this is the wrong place.
I was basing it off of this C# example
if (s.Connected)
{
Console.WriteLine("Connected to Server");
ExportImportProgressClass eip = new ExportImportProgressClass();
eip.UseDefaultFile = false;
eip.IncludeAllCertificates = false;
eip.IncludeAllConditions = false;
eip.IncludeAllConnections = false;
eip.IncludeAllCredentials = false;
eip.IncludeAllExitCodes = false;
eip.IncludeAllNetworkDrives = false;
eip.IncludeAllNotifications = false;
eip.IncludeAllPermissions = false;
eip.IncludeAllPGPKeyRings = false;
eip.IncludeAllServerSettings = false;
eip.IncludeAllTimeExceptions = false;
eip.IncludeAllUserGroups = false;
eip.IncludeAllVariables = false;
eip.IncludeAllJobs = false;
/*var jobs = (from j in s.Jobs.GetAll()
where j.Group == "WD Standard"
select j.Name).ToList();*/
//eip.Jobs = jobs;
//eip.JobObjects = s.Jobs.GetAll().Where(item => item.Group == "WD Standard").ToList();
eip.JobObjects = s.Jobs.GetAll();
ExportSettingsResponseClass esrc = s.ExportSettings(eip);
if (esrc.Success && esrc.FileBytes != null)
{
System.IO.File.WriteAllBytes("C:/Temp/VC-Settings.zip", esrc.FileBytes);
}
s.Disconnect();
}