Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:10:10+00:00 2026-06-01T13:10:10+00:00

I’m trying to adapt a VBscript that runs the QWINSTA command against a text

  • 0

I’m trying to adapt a VBscript that runs the QWINSTA command against a text file I’ve defined as an array and displays the results in a text file.

after looking at numerous examples on Stack and other sites this is my latest iteration, that displays everything except the STDOUT, which is really what I’m after, not sure if it’s an issue with how I’m calling the command piping the output or what. I’m also passing two variables as arguments to complicate matters 🙂

The Text file that stdout is piped to is empty after the FOR Loop completes
I may be going the long way about this, most of my past experience with this is in BATCH

For Each server In RemotePC

        'The Query is launched
        Dim Query As New ProcessStartInfo

        Query.WorkingDirectory = "c:\"
        Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
        Query.FileName = "QWINSTA"
        Query.WindowStyle = ProcessWindowStyle.Hidden
        Process.Start(Query)

        'Call Shell("QWINSTA /SERVER:" & server & " " & profile & " >C:\Windows\Temp\SFOUT.txt", vbHidden, Timeout:=5000)


        Dim SFOUT As New IO.StreamReader("C:\windows\temp\SFOUT.txt")

        'Results are Echoed to TextboxResults
        TextBoxResults.Text &= "__________________________________________________________" & ControlChars.CrLf
        TextBoxResults.Text &= server & ControlChars.CrLf
        TextBoxResults.Text &= SFOUT.ReadToEnd & ControlChars.CrLf
        SFOUT.Close()

    Next

Here is what the working code in VBscript looks like

For Each server In RemotePC


Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set StrDir = "QWINSTA /SERVER:" & server & " " & profile


'The Query is launched
Set oExec = WshShell.Exec(StrDir)


'We wait for the end of process
Do While oExec.Status = 0
WScript.Sleep 500
Loop

'We scan and only display the command output of commands without NULL output
Do While oExec.StdOut.AtEndOfStream <> True
      Results.WriteLine("__________________________________________________________")
        Results.WriteLine(server)
        Results.WriteLine oExec.StdOut.ReadLine
Loop



NEXT

Any help is appreciated

@Nilpo

this is what I get back

SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 

console Matt 1 Active

I’ve built something similar to this using QWINSTA and piping the output in batch and it works flawlessly, just having issues adapting this.


here’s the last thing I tried I tried to simplify things by calling something as basic as notepad.exe, and even trying to define the environment variable thinking it’s not reading %PATH%, in reply to @Nilpo

Dim Query As New Process

        Query.StartInfo.FileName = "notepad.exe"
        'Query.StartInfo.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
        Query.StartInfo.UseShellExecute = False
        Query.StartInfo.RedirectStandardOutput = True
        Query.StartInfo.WindowStyle = ProcessWindowStyle.Normal

        Environment.SetEnvironmentVariable("PATH", "%PATH%;" & "C:\Windows\System32\notepad.exe")

        Query.Start()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:10:12+00:00Added an answer on June 1, 2026 at 1:10 pm

    You should have a space after the output redirection.

    Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
    

    Should be

    Query.Arguments = server & " " & profile & " > C:\Windows\Temp\SFOUT.txt"
    

    That being said, the only thing I can see that may not work is here.

    Query.WorkingDirectory = "c:\"    
    Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"    
    Query.FileName = "QWINSTA"    
    Query.WindowStyle = ProcessWindowStyle.Hidden    
    Process.Start(Query)
    

    I’m not sure that you can use arguments in this fashion. Your output redirection is being read as a literal argument when it’s really not. In a command line environment it’s interpreted as a separate command, not an argument to the first command. It’s very similar to piping in that manner. Supplying this as an argument in your code probably won’t work as expected. You may want to go back to the Call Shell method. That will execute the command as if it were on the command line.

    [Edit]

    I was correct. Here is the proper way to do this using your method.

    Query.WorkingDirectory = "c:\"
    Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"
    Query.FileName = "QWINSTA"
    Query.WindowStyle = ProcessWindowStyle.Hidden
    
    'Redirect output
    Query.UseShellExecute = False
    Query.RedirectStandardOutput = True
    
    Process.Start(Query)
    
    'Read the output
    Dim output As String = Process.StandardOutput.ReadToEnd
    ' wait for the process to terminate 
    Process.WaitForExit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.