14 Replies - 474 Views - Last Post: 25 January 2014 - 01:02 AM Rate Topic: ****- 1 Votes

#1 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

How to use more than one Quote in String

Posted 24 January 2014 - 02:03 AM

to all the professionals,
I looking for a way to run a command the requires "Last Logon" and use this command within the bounds of the "" that run the command as a string,

I will explain,

Code

Import

Imports System.ComponentModel


Main Body Of code,

Dim myProcess As New Process()
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.RedirectStandardOutput = True
        Try
            myProcess.StartInfo.FileName = "net"
            myProcess.StartInfo.Arguments = "user " & Environment.UserName & " | findstr /B /C:"last logon"
            myProcess.StartInfo.CreateNoWindow = True
            myProcess.Start()
            Label1.Text = _
               Replace(myProcess.StandardOutput.ReadToEnd(), _
               Chr(13) & Chr(13), Chr(13))
            myProcess.WaitForExit()
        Catch ex As Win32Exception
            MsgBox((ex.Message + ". Error Detected."))
        End Try

in the command prompt you can use a command that is as follows to retrieve user last logon

net user val | findstr /B /C:"Last logon"

Val would be replaced with the users username and will display this as last logon plus the time the objective of this is to display the information what would be in the command line in a label but I don't know how to double quote if you like because you already have to use "" in order for the text to be allowed.

any help will be much appreciated.

also this command works without errors the label will just disappear.

Kind Regards

Bradley

Is This A Good Question/Topic? 0
  • +

Replies To: How to use more than one Quote in String

#2 jgferguson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 11-July 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:28 AM

Hi there,

Without testing your code, have you tried using single quotes around the command? I have found this works in most of the scenarios where double quote marks are used.

Failing that, would need to look into it a bit further but it would be worth ruling a single quote out first.

Hope this helps,
Jerry.
Was This Post Helpful? 0
  • +
  • -

#3 Sharparam  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 3
  • Joined: 16-June 13

Re: How to use more than one Quote in String

Posted 24 January 2014 - 07:21 AM

To use double quotes inside a string in VB.Net, you need to escape them by doubling it, like so:

Dim foo As String = "Quoted word: ""Foo"""
System.Console.WriteLine(foo)
' Output: Quoted word: "Foo"


Was This Post Helpful? 0
  • +
  • -

#4 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:02 PM

jgferguson,
Thank you for the tip yes I did try that as well before I came on here.

Sharparam,
Thank you for your help and the code sample I followed to a degree what you said adding in what spaces but came up with the same result as I was getting the label that runs the command disappears and is a weird one as it does not display any errors and is like it has received the information successfully but why does it disappear what am I doing wrong it appears you can not use commands that have "" in a command in visual basic but there must be away around this I just have to figure it out thanks both for your help so far it is much appreciated any further advice would be grand or if you can get a sample to work.

the information displayed should look like this: Last logon 24/01/2014 23:41:45

Best Regards

Bradley
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:19 PM

Please use properly formed sentences. That single, large, paragraph has no punctuation at all and is difficult to decipher.

Putting double-quotes around "last logon" would be:
myProcess.StartInfo.Arguments = "user " & Environment.UserName & " | findstr /B /C:""last logon"""

If the UserName has spaces then it probably requires:
myProcess.StartInfo.Arguments = "user """ & Environment.UserName & """ | findstr /B /C:""last logon"""

You should also test the syntax by running directly from a command-prompt. Once you've got this working it is easier to translate for code - and a sensible approach anyway.
Was This Post Helpful? 0
  • +
  • -

#6 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:42 PM

once again thanks for your help here is what happens.

http://i877.photobuc...zps2096d2ad.png

Then

http://i877.photobuc...zps79deac03.png

Kind Regards

Bradley

This post has been edited by BJseal91: 24 January 2014 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:45 PM

I seriously have no idea what I am supposed to do with those pink blocks :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#8 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: How to use more than one Quote in String

Posted 24 January 2014 - 05:49 PM

lol forget the pink it is only a demo to what happens with the label when the command is run from the button
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 06:26 PM

Ok.

Sorry, I'm not sure at the moment why it doesn't work from code.

Maybe someone else has some input ;)

Post your current code to make sure the quotes are correctly formed.

This post has been edited by andrewsw: 24 January 2014 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to use more than one Quote in String

Posted 24 January 2014 - 06:45 PM

The findstr appears to be case sensitive. ie, "Last Logon" won't be found as well as "last logon" which is what you appear to have in your sample. Perhaps you should use "Last logon" as it appears in the result of entering Net User <userid> in the command prompt.

Also, if you set
myProcess.StartInfo.RedirectStandardError = true


and then after your process .start use
MsgBox(MyProcess.StandardError.ReadToEnd()) 


You should then be prompted with any errors that are occurring

This post has been edited by CharlieMay: 24 January 2014 - 07:03 PM

Was This Post Helpful? 2
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 07:23 PM

I tried some debugging and a message came saying it didn't recognise /B as a valid option. I removed it and it said the same thing about /C.

Posted Image

Despite this error I am beginning to suspect that it is a permissions issue. If I use this:
myProcess.StartInfo.Arguments = "user " & Environment.UserName & " /TIMES:ALL"

I get "System error 5 has occurred. Access is denied".
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to use more than one Quote in String

Posted 24 January 2014 - 07:49 PM

I started messing with it too.

It appears that since the filename is Net and Net has certain arguments, that the Pipe | is causing issues. Here's what I did that should produce a result but it's kind of a hack-workaround

       Dim myProcess As New Process()
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.RedirectStandardOutput = True

        Try
            myProcess.StartInfo.FileName = "net "
            myProcess.StartInfo.Arguments = "user " & Environment.UserName  ' | findstr /B /C:""Last logon"""
            myProcess.StartInfo.CreateNoWindow = True
            myProcess.Start()
            Dim result As String = myProcess.StandardOutput.ReadToEnd 'Read Net User <user> result
            Dim startt As Integer = result.IndexOf("Last logon") 'Find where Last Logon starts
            Dim endd As Integer = result.IndexOf("Logon hours") 'Find where Logon hours starts
            Label1.Text = result.Substring(startt, endd - startt) 'Get the substring using both starting positions to calculation start and length.

            myProcess.WaitForExit()
        Catch ex As Win32Exception
            MsgBox((ex.Message + ". Error Detected."))
        End Try

Was This Post Helpful? 2
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 08:13 PM

That works, ta, but my curiosity is raised ;)

The original command works from a prompt, but I'm struggling to find any documentation for it. That is, other than a quote from an MS article for Windows NT:

Quote

The NET USER command is designed to modify user accounts, not to return statistics.


(I found other information about getting the last logon via Active Directory.)

Interesting..
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,794
  • Joined: 12-December 12

Re: How to use more than one Quote in String

Posted 24 January 2014 - 08:30 PM

I can get my last logon time on my machine with:
Imports System.DirectoryServices    'Add References
Imports System.DirectoryServices.AccountManagement

Module Module1

    Sub Main()
        Dim c = New PrincipalContext(ContextType.Machine)
        Dim uc = UserPrincipal.FindByIdentity(c, Environment.UserName)
        Console.WriteLine(uc.LastLogon)

        Console.ReadKey()

    End Sub

End Module

24/01/2014 16:56:45 to be precise.

ContextType.Domain is also available, and .ApplicationDirectory.

This post has been edited by andrewsw: 25 January 2014 - 05:36 AM

Was This Post Helpful? 2
  • +
  • -

#15 BJseal91  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 68
  • Joined: 18-April 09

Re: How to use more than one Quote in String

Posted 25 January 2014 - 01:02 AM

Thanks for all your help both of the commands worked what I was trying to do this site is great one day we should all club together make an amazing program and sell it for charity this site is the best once the program is complete you are welcome to a free copy "no pink blocks I promise" :D

thanks again CharlieMay for helping me learn coding and for making things work in vb I must say you know you stuff.

Best wishes

Bradley
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1