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.
.Execreturns immediately, even if the process it starts is not finished. Does this answer resolve the issue?