Jump to content

RunAs() passing arguments with spaces


NDog
 Share

Recommended Posts

I want to use the RunAs() function and pass a value with (or without) spaces as a single first argument. I guess I need to include "quotes" around the argument.

Then I need to strip off the "quotes" of the argument to bring it back to the orginal value.

e.g

script.exe "a long username with spaces"

$user = a long username with spaces

1) What is the correct syntax RunAs() function to pass the single first argument with "quotes" around the value. eg '"""' or '""""' or '"' etc...

2) Is it neccessary to then strip the "quotes" from the arguments, or does autoit do that automatically?

Here is a script below which I hope to fix. It is assumed that @UserName has a space in it e.g "A long user name"

; Global variables
$completedprofilesfile = @ScriptDir & "\doneusers.log"

; ======= start the script ======= ;

; If a cmdline parameter has been passed then go directly to admin functions
MsgBox(0, @ScriptName & " Info", "Number of cmdline arguments: "&$CmdLine[0])
If $CmdLine[0] > 0 Then
; "" characters should be stripped off
; $CmdLine[1] valid username argument 1 - actual username
MsgBox(0, @ScriptName & " Info", "$CmdLine[1] = "&$CmdLine[1],5)
$user = $CmdLine[1]
MsgBox(0, @ScriptName & " Info", "$user = "&$user ,5)
LogDoneUser($user) ; valid username
Exit ; all done
EndIf

; Do some stuff logged in as current user
Function1()
Function2()

; ======= Users scripts has been processed as current user ======= ;

If Not IsAdmin() Then ; logon as an admin user and pass the currentuser name as an argument
$adminname = admin
$adminpass = password
MsgBox(0, @ScriptName & " Info", "admin user: "&$adminname&@CRLF&"admin pass: "&$adminpass)
If @Compiled Then
RunAs($adminname, @ComputerName, $adminpass, 0, @ScriptName & ' """' & @UserName & '""', @ScriptDir)
Else
RunAs($adminname, @ComputerName, $adminpass, 0, @AutoItExe & ' """' & @ScriptName & '""" """' & @UserName & '""', @ScriptDir)
EndIf
ElseIf IsAdmin() Then
LogDoneUser(@UserName)
EndIf
Exit

; ======= End of script ======= ;

Func LogDoneUser($user) ; must run as admin
$write = FileOpen($completedprofilesfile,9)
If $write = -1 Then ; Check if file opened for reading OK
MsgBox(0, @ScriptName & " Error", "Unable to open file for writing: " & $completedprofilesfile)
Exit
EndIf
FileWrite($write, $user & @CRLF)
FileClose($write)
EndFunc
Link to comment
Share on other sites

Use a single quote before the double quote. 'before quotes "This is a test in quotes" and out of quotes'

Obviously the above string isn't going to do your run command but it shows how to use single and double quotes together. The string above would be interpreted as a single string in AutoIt.

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

When dealing with troublesome quotes in a string for a Run command, my best advise is to take the whole string exactly as you'd put it into the run command, but use a ConsoleWrite instead, this way you can tell exactly what the string looks like to the run command when the script is running.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Sorry if the question sound too easy. I am not sure if what the consolewrite output will match the runas() output because i cannot see it exactly so I hoped someone here with experience may have a definite answer on specifically the runas() output. For my second question does autoit strip the "quote" marks of an argument automatically?

Link to comment
Share on other sites

The consolewrite output will be exactly the same as what the Run command is trying to run if you put it into the consolewrite the same way as you put it into the Run command, at least as far as the first comma.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...