Convert jscript to vb.net

210 Views Asked by At

We use Kaspersky Security Center to manage AV on all our domain computers. I was looking for an external way to move computers to different groups and change the comments we give them in the Kaspersky Security Center. Kaspersky gave me some links to check out but I have no idea where to start on this. I was looking to code this in a VB.Net Windows Form Application.

My question is how do i convert or make the below jscript work in a VB.net Windows form. I'm going to have a sql table loaded with the Kaspersky Host ID, Comment and GroupID. once a day i want to iterate down through that sql table and only update the computers that need changes made wither to their comment or group. (the sql part i already have written)

Here is what im Aiming for:

Dim reader3 As SqlDataReader
    Dim strconnection3 As String
    strconnection3 = data_source_all 'defined globally
    Dim SqlConnection3 As New SqlConnection(strconnection3)
    Dim cmd3 As New SqlCommand
    cmd3.CommandText = "SELECT kaspersky_hostid, kaspersky_comment, pc_info_comment, kaspersky_groupid FROM pc_info where (pc_status = 'active')"
    cmd3.CommandType = CommandType.Text
    cmd3.Connection = SqlConnection3
    SqlConnection3.Open()
    reader3 = cmd3.ExecuteReader()
    If reader3.HasRows Then
        While reader3.Read()
            If reader3(1).ToString = reader3(2).ToString Then
            Else
                Update_Host_Comment(reader3(0).ToString,reader3(2).ToString)
            End If
End While
        SqlConnection3.Close()
        SqlConnection3.Dispose()
        cmd3.Dispose()
    Else
    End If

Public Sub Update_Host_Comment(ByVal hostid As String, ByVal comment As String)

'Converted JScript

'var oHosts = new ActiveXObject("klakaut.KlAkHosts");
    'oHosts.AdmServer = AcquireAdServerProxy();
    'var strHostName = hostid;    //name of the host to change attributes
   '//Fill container with attributes to change
   'var oProps = new ActiveXObject("klakaut.KlAkParams");
    'oProps.Item("KLHST_WKS_COMMENT") = comment;    //Change Comment
    'oHosts.UpdateHost(strHostName, oProps);

End Sub

Link1: https://support.kaspersky.com/9291 Link2: https://support.kaspersky.com/2810

below is the JScript i want to run with vb.net:

function AcquireAdServerProxy()
{    
    var oSrvConnectionProps = new ActiveXObject("klakaut.KlAkParams");
    oSrvConnectionProps.Add("Address", "localhost:13291");
    oSrvConnectionProps.Add("UseSSL", true);

    var oAdmServer = new ActiveXObject("klakaut.KlAkProxy");
    oAdmServer.Connect(oSrvConnectionProps);
    return oAdmServer;
};

function Update_Host_Comment(hostid,comment)
{
    var oHosts = new ActiveXObject("klakaut.KlAkHosts");
    oHosts.AdmServer = AcquireAdServerProxy();
    var strHostName = hostid;    //name of the host to change attributes
   //Fill container with attributes to change
    var oProps = new ActiveXObject("klakaut.KlAkParams");
    oProps.Item("KLHST_WKS_COMMENT") = comment;    //Change Comment
    oHosts.UpdateHost(strHostName, oProps);
};

function Update_Host_Group(hostid,groupid)
{
    var oHosts = new ActiveXObject("klakaut.KlAkHosts");
    oHosts.AdmServer = AcquireAdServerProxy();
    var strHostName = hostid;    //name of the host to change attributes
    //Fill container with attributes to change
    var oProps = new ActiveXObject("klakaut.KlAkParams");
    oProps.Item("KLHST_WKS_GROUPID") = groupid;    //Change group
    oHosts.UpdateHost(strHostName, oProps);
};

//Calling Functions
Update_Host_Comment("SomeHostID","Some Comment Text");
Update_Host_Group("SomeHostID","Some GroupID");

06/04/18 Edit: Here is the code i tried:

Public Function AcquireAdServerProxy()
    Try
        Dim oSrvConnectionProps = CreateObject("klakaut.KlAkParams")
        oSrvConnectionProps.Add("Address", "localhost:13291")
        oSrvConnectionProps.Add("UseSSL", True)

        Dim oAdmServer = CreateObject("klakaut.KlAkProxy")
        oAdmServer.Connect(oSrvConnectionProps)
        Return oAdmServer
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return False
    End Try
End Function

Public Function Update_Host_Comment(ByVal hostid As String, ByVal comment As String) As Boolean
    Try
        Dim ohosts = CreateObject("klakaut.KlAkHosts")
        ohosts.AdmServer = AcquireAdServerProxy()
        Dim strHostName = hostid
        'Fill container with attributes to change
        Dim oProps = CreateObject("klakaut.KlAkParams")
        oProps.Item("KLHST_WKS_COMMENT") = comment
        ohosts.UpdateHost(strHostName, oProps)
        Return True
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return False
    End Try
End Function

Public Function Update_Host_Group(ByVal hostid As String, ByVal groupid As Integer) As Boolean
    Try
        Dim ohosts = CreateObject("klakaut.KlAkHosts")
        ohosts.AdmServer = AcquireAdServerProxy()
        Dim strHostName = hostid
        'Fill container with attributes to change
        Dim oProps = CreateObject("klakaut.KlAkParams")
        oProps.Item("KLHST_WKS_GROUPID") = groupid
        ohosts.UpdateHost(strHostName, oProps)
        Return True
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return False
    End Try
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label4.Text = "Processing ..."
    Label4.Update()
    Try
        If TextBox1.Text <> Nothing Then
            If TextBox2.Text <> Nothing Then
                If Update_Host_Comment(TextBox1.Text, TextBox2.Text.ToUpper) Then
                    Label4.Text = "Comment Updated"
                    Label4.Update()
                Else
                    Label4.Text = "Comment Update Error"
                    Label4.Update()
                End If
            Else
            End If
            If TextBox3.Text <> Nothing And IsNumeric(TextBox3.Text) Then
                If Update_Host_Group(TextBox1.Text, TextBox3.Text) Then
                    Label4.Text = Label4.Text & " / Group Updated"
                    Label4.Update()
                Else
                    Label4.Text = Label4.Text & " / Group Update Error"
                    Label4.Update()
                End If
            Else
            End If
        End If
    Catch ex As Exception
        Label4.Text = "Error"
        Label4.Update()
    End Try
End Sub
End Class

This is the error i get when i run it:

System.Runtime.InteropServices.COMException (0xE0FF04FD): Transport level error while connecting to http://localhost:13291: failed to resolve address at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn) at kaspersky_api.Form1.AcquireAdServerProxy()

0

There are 0 best solutions below