0

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();

                }
Charlieface
  • 52,284
  • 6
  • 19
  • 43
Rami Aboulissane
  • 23
  • 1
  • 1
  • 4
  • You are reassigning the variable $escr. You cannot use the same variable twice. Try removing the line : $escr = New-Object VisualCron.ExportSettingsResponseClass – jdweng Jun 18 '23 at 23:55
  • 1
    The error implies that ```$global:Server``` is ```$null``` at the point where you call ```$global:Server.ExportSettings($eip)```. Note that although you’re initialising the variable inside your ```Export-VCSettings``` with function ```$global:Server = …```, you’re *not* calling that function before you try to use ```$global:Server``` in your main script body. It’s not clear to me what the correct fix is, but that’s where the error is coming from - you basically need to initialise the variable somehow before you try to use it… – mclayton Jun 19 '23 at 05:49
  • As an aside, note that in your c# sample, all the ```eip.IncludeAll* = …``` properties are ```bool```, but you’re using a ```[string]``` in your function parameters. This might give unexpected results if you pass the string ```”false”``` because Powershell converts *any* non-null **string** to ```$true``` when marshalling to a ```[bool]```(try these expressions… ```[bool] ([string] $null); [bool] "true"; [bool] "false"; [bool] "xyz"``` - only the first is ```$false```). Consider changing your function parameters to ```[bool]``` or ```[switch]``` to make it easier to use… – mclayton Jun 19 '23 at 06:04
  • Thank you all very much. I will see to ensure the $global:server is actually initialised and also revise the parameter properties. I will get back to you with my findings. Much appreciated – Rami Aboulissane Jun 19 '23 at 11:04

0 Answers0