Login to participate
Endpoint Management & Virtualization ArticlesRSS

A Quick Way to Write SVS Scripts

k00tje@gmail.com's picture

VB Script can be a good way to manage your SVS environment but often takes a lot of time. I found a quick and easy way to write VB scripts for SVS that'll impress your friends and amaze your loved ones.

Download the "WMI Code Creator" here and start it up.

  • Make sure that the default code language is VB script.
  • Now select the second tab named "Execute a method"
  • Here you will see that it's defaulting to the namespace "root\CIMV2".
  • Now change the path to "root\DEFAULT" by selecting the dropdown box.
  • You see it contains 3 classes.
  • Select the class named "AltirisVSProv".
  • Now you can select a task from the "Methods" dropdown box -- there are 13 available.
  • In this example I chose "Activate"
  • As you see there's no parameter specified yet.
  • Double-click on "LayerNameOrGuid". You see that it has to be a string value.
  • You can type the name of the layer you want to activate and click "Ok".
  • The parameter is added to the script now.
  • You can now choose to execute the script or open it in notepad.
Tip: You can replace the line 'strComputer = "."' with 'strComputer = InputBox("Enter I.P. or name of computer","Activate a layer")' to manage a remote machine.

One thing of interest is that the source is included in the zip file of the "WMI Code Creator" so you can modify it to your own needs.

Unfortunately the code that "WMI Code Creator" produces for VB.NET and C#.NET is not working. If someone knows a workaround for this then please add it to this post.

erikw's picture

This is great

K00tje,

This is a great way to create VB scripts. It is a very good way for the newbee's to learn how to create and work with VB scripts.

Thnxsss for the very good job.

regards
Erik
www.svs4u.nl

Regards
Erik
www.DinamiQs.com
Dinamiqs is the home of VirtualStorm (www.virtualstorm.org)

Screenbert's picture

Script

It's important to note that this is happening via WMI, not the Altiris Console. This has pros and cons. Pros being that you can make it happen quicker and get better reporting back, but the cons being if the machine is not up, you have the reschedule through your script. Great tip!

Screenbert

Screenbert

riva11's picture

I didn't know this way,

I didn't know this way, nice info.

Thanks
PM

erikw's picture

Using VB Scripts for SVS Deployement

For small companies where Deployment Server is overkill, VB scripting is a very good way to get the job done.

Create your VB scripts, and use Kix32 to run them.

It works fine, but is a real tech solution, and most of the times you need external consultancy to get it working.

I use it quite often.

regards
Erikw

Regards
Erik
www.DinamiQs.com
Dinamiqs is the home of VirtualStorm (www.virtualstorm.org)

rcboenne's picture

C# code for SVS

This is some code I'm working on. It just enumerates the current layers.
--------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace Focus_XP_SVS_Layer_Console
{
public class WMITest
{
public static void Test()
{
try
{

ManagementScope scope = new ManagementScope("root\\default");
scope.Connect();
ManagementClass classInstance = new ManagementClass(
scope,
new ManagementPath("AltirisVSProv"),
null);

// Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("EnumerateLayers");

// Add the input parameters.
inParams["Verbose"] = 1;

// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("EnumerateLayers", inParams, null);

// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("EnumData: " + outParams["EnumData"]);
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
catch (ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
}

}
}

k00tje@gmail.com's picture

VB code for SVS

Thnx for the example.
I translated it for the VB people.

Imports System
Imports System.Management

Try
Dim scope As ManagementScope = New ManagementScope("root\default")
scope.Connect()
Dim classinstance As ManagementClass = New ManagementClass(scope, New ManagementPath("AltirisVSProv"), Nothing)

'Obtain in-parameters for the method
Dim inParams As ManagementBaseObject = classinstance.GetMethodParameters("EnumerateLayers")

'Add the input parameters.
inParams("Verbose") = 1

'Execute the method and obtain the return values.
Dim outParams As ManagementBaseObject = classinstance.InvokeMethod("EnumerateLayers", inParams, Nothing)

'List outParams
Console.WriteLine("Out parameters:")
Console.WriteLine("EnumData: " & outParams("EnumData"))
Console.WriteLine("ReturnValue: " & outParams("ReturnValue"))
RichTextBox1.Text = outParams

Catch ex As Exception
MsgBox(ex.ToString)
End Try

Rgds

erikw's picture

VB translation

K00tje,

Thnxsss for the VB translation. This prevents us from all doing exactly the same.

Regards
Erik

Regards
Erik
www.DinamiQs.com
Dinamiqs is the home of VirtualStorm (www.virtualstorm.org)

gariese's picture

VB translation like WMI format

Another translation for WMI to vb.net
You can almost use the syntax code generated in WMI code creator

Now somebody why I get the error # 1041 (all the methods give this error) if i use VirtualSoftwarePackage to activate a layer


Imports System.Management

Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object

Dim objShare As Object
Dim objOutParams As Object
Dim objInParam As Object

Dim res As String
Dim Ind As Integer
Dim svsName(64) As String
Dim svsActive(64) As String
Dim svsId(64) As String

objWMIService = GetObject("WinMgmts:\\.\root\DEFAULT")
colItems = objWMIService.ExecQuery("SELECT * FROM VirtualSoftwarePackage")
Ind = 0
For Each objItem In colItems
svsName(Ind) = objItem.name
svsActive(Ind) = objItem.active
svsId(Ind) = objItem.id
Next

'the new way
' svsId(1) is foxit
objShare = objWMIService.Get("VirtualSoftwarePackage.Id='" & svsId(1) & "'")
objOutParams = objWMIService.ExecMethod("VirtualSoftwarePackage.Id='" & svsId(1) & "'", "Activate")
res = objOutParams.ReturnValue ' don't work give error 1041

'the old way
objShare = objWMIService.Get("AltirisVSProv")
objInParam = objShare.Methods_("Activate").inParameters.SpawnInstance_()
objInParam.Properties_.Item("layerNameOrGuid") = "foxit"
objOutParams = objWMIService.ExecMethod("AltirisVSProv", "Activate", objInParam)
res = objOutParams.ReturnValue ' 0=no error's layer is activate

AngelD's picture

Activate single layer

Don't know if this helps but here is how I activate a single layer instance using C# with the new WMI provider.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace SVSWMIActivateLayer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("first argument is layer guid");
                return;
            }
            string layerId = args[0];

            ManagementObject WMIObject = null;
            string ObjectPath = string.Format(@"root\default:VirtualSoftwarePackage.Id='{0}'", layerId);
            string MethodName = "Activate";

            try
            {
                // bind to the WMI object instance
                WMIObject = new ManagementObject(ObjectPath);

                // get a WMI object representing all in-parameters for this method
                ManagementBaseObject InParams = WMIObject.GetMethodParameters(MethodName);

                // execute the method on the WMI object and get the out-parameters
                ManagementBaseObject OutParams = WMIObject.InvokeMethod(MethodName, InParams, null);

                // output all the return values
                foreach (PropertyData Property in OutParams.Properties)
                    Console.WriteLine("{0}: {1}", Property.Name, OutParams[Property.Name]);

                // dispose the COM object of the in param object
                if (InParams != null)
                    InParams.Dispose();

                // dispose the COM object of the out param object
                if (OutParams != null)
                    OutParams.Dispose();
            }

            // show any error message
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            // free up the underlying COM object
            finally
            {
                if (WMIObject != null)
                    WMIObject.Dispose();
            }
        }
    }
}
gariese's picture

Thanks AngelD,

Thanks AngelD,

Its helps a little bit.
I am confused about how many way’s you can used the WMI interface.
I see many articles en examples in different language (vb.net, script, c# and c++) and it seems that even for the same language a lot of different methods can be used.

Thunder-man's picture

WMI

Is this only for the old WMI Style?

Google English…
SVSLoad.com

k00tje@gmail.com's picture

Can also be used for the new class

You only need to connect to the new management class.
The old one was named AltirisVSProv, now it's called VirtualSoftwarePackage.
Be aware that the old class contains 13 methods and the new only 8.
Have not used the new class so far.
(first have to read the docs in the 2.1 sdk)

Thunder-man's picture

New WMI ...

THX,
I see now, the new WMI is VirtualSoftwarePackage and VirtualSoftwareSublayer.

But why only 8 methods???
I think it´s bad, the old class where not supported in newer SVS Versionen.

So I can only use the new style?!

And why has the new class only the ID and not ID and Name??? (e.g. for activate / deactivate)
(Hence, I think it has become complicated)

I have install the SDK but the only Help I found is fsllib.hlp (Not Help for WMI, or is there inside fsllib.hlp)

Google English…
SVSLoad.com

AngelD's picture

Activate layer by name

As you can't use WMIService.Get("VirtualSoftwarePackage.Name=""My Layer Name""") You will have to do something like this.

LayerName = "My Layer Name"

Set WMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate,(Restore,Backup)}!\\" _
		& ".\root\default")

Set LayerItems = WMIService.ExecQuery("Select * From VirtualSoftwarePackage" & _
	" Where Name=""" & LayerName & """")

If LayerItems.Count = 0 Then
	WScript.Echo "No Layer found with the name """ & LayerName & """"
	WScript.Quit
ElseIf LayerItems.Count > 1 Then
	WScript.Echo "More then one layer found with the name """ & LayerName & """"
	WScript.Quit
End If

For Each Layer In LayerItems
	'// one way to activate a layer
	WScript.Echo "Activate layer"
	ActionResult = Layer.Activate
	WScript.Echo "ActionResult: " & ActionResult
	
	'// deactivate layer
	WScript.Echo "Deativate layer"
	ActionResult = Layer.Deactivate
	WScript.Echo "ActionResult: " & ActionResult
	
	'// another way to activate a layer
	Set ImportedLayer = WMIService.Get("VirtualSoftwarePackage.Id=""" & Layer.Id & """")
	WScript.Echo "Activate layer"
	ActionResult = ImportedLayer.Activate
	WScript.Echo "ActionResult: " & ActionResult
	
	Exit For
Next

The new provider is handled differently then the old one that's why you only see 8 methods.

You can still use the old WMI provider, thus have in mind that it will be removed in some future release.

Thunder-man's picture

RE: Activate layer by name

AngelD thank you once again, for the code.

Now it works.

I had done a mistake with the variables. lol

vbs
LayerName = "My Layer Name"

AutoIt
$LayerName = "My Layer Name"

============================

Can somebody send to me the file "WMI Help" from the SDK? I cannot install it!

Google English…
SVSLoad.com

AngelD's picture

WMI help file

Don't think there is any help files for the WMI provider(s) in the SVS SDK.

But you can find the mof files in the directory where you installed SVS, ex. "C:\Program Files\Altiris\Software Virtualization Agent".

SvsWmi.mof (New WMI provider)
AltirisVSProvider.mof (Old WMI provider)

The structure in the mof file for the new provider is kinda self-explained.

Thunder-man's picture

RE: WMI help file

But Juicemaster write...

The SDK contains C header files, a lib file, full documentation, and sample code. Documentation on the new WMI provider is also included.

Yes, I look yesterday inside the *.mof Files (very interesting)

Google English…
SVSLoad.com