0

I am currently hardstuck at this problem and I hope there is a solution.

I need to run a SQL query on one of our servers, where i check for the creation of a user. The main problem is, that i need to run this query over vbs, respectively in a vbs launched powershell script.

My ps code is running, but i am unable to get the output over vbs.

This is the ps code:

$command = function Invoke-SQL {Param( [string]$SqlCommand  );
    $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8';
    $E = Get-OdbcDsn -Name SQL_UserCheck;
    $driver = $E.DriverName;
    $server= $E.Attribute.Server;
    $database = $E.Attribute.Database;
    $connectionString = "Server=$server; integrated security = true";
    $connection = new-object system.data.SqlClient.SQLConnection($connectionString);
    $command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection);
    $connection.Open();
    $adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command;
    $dataset = New-Object System.Data.DataSet;
    $adapter.Fill($dataSet) | Out-Null;
    $connection.Close();
    $dataSet.Tables | Format-List;  
} 


    Invoke-SQL -SqlCommand "DECLARE @tracefile VARCHAR(256) 
                    SELECT @tracefile = CAST(value AS VARCHAR(256)) 
                    FROM ::fn_trace_getinfo(DEFAULT) 
                    WHERE traceid = 1 
                    AND property = 2 
                    SELECT NTUserName, SessionLoginName, SPID, TargetLoginName, StartTime, ServerName 
                    FROM ::fn_trace_gettable(@tracefile, DEFAULT) trc 
                    INNER JOIN sys.trace_events evt ON trc.EventClass = evt.trace_event_id 
                    WHERE trc.EventClass IN (108, 109, 110) AND ObjectName ='sysadmin' 
                    ORDER BY trc.StartTime";

This is my try at putting the whole thing into a .vbs

pscommand = "<powershellscript from above>"
cmd = "powershell.exe -noprofile  -command " & pscommand
Set Shell = CreateObject("Wscript.Shell")
Set executor = shell.Exec(cmd)
executor.StdIn.Close
strStarter = executor.StdOut.ReadAll
WScript.Echo strStarter

But for some reason, the output stays empty. I already checked for te quotation marks and replaced them all with "" instead of ". I also checked this with WScript.Echo "" and all the " get transfered correctly.

Sadly, i am no out of ideas and have no clue where to start.

In the end, i need to get this out of vbs as either a single string or an array, so i can put these into propertybags.

Thanks in advance for all possible help.

5
  • .Exec returns immediately, even if the process it starts is not finished. Does this answer resolve the issue? Commented Feb 18, 2020 at 12:30
  • Sadly not. I think the problem lies in the Datatype of $dataset. If i run GetType() on the variable it is System.Data.DataTable. Another problem i face with this, it seems like all values get written into a single cell, so with multiple values it gets even more problematic. Commented Feb 19, 2020 at 6:36
  • Write the data into a file and read it in VBS? Commented Feb 19, 2020 at 7:05
  • Writing this into a file sadly isnt an option, since i need to run this script over scom on multiple servers. Creating files is a no-go for me here i am affraid. Commented Feb 19, 2020 at 9:44
  • May I ask why you cannot use PowerShell directly? Or, perhaps, rewrite the whole thin in C#? Commented Aug 9, 2020 at 22:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.