Announcement

Collapse
No announcement yet.

Code Check Please

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Code Check Please

    Hello,

    I'm trying to use AMS to map a drive(s). The code I have works, however on File.RunAs the command complete successfully but no drive is mapped. If I click on the batch file that File.RunAs is linked to, it's maps just fine. No errors when executed in AMS.

    Code is below - What am I missing to make the batch file execute correctly in AMS?

    Code:
    RunUID = TextFile.ReadToString("AutoPlay\\Content\\Common\\CommonA.dat");
    RunPWD = TextFile.ReadToString("AutoPlay\\Content\\Common\\CommonP.dat");
    
    Drive = Input.GetText("Dletter");
    IP = Input.GetText("ip");
    User = Input.GetText("User");
    PWD = Input.GetText("Password");
    qut = Input.GetText("qut");
    
    
    --set = Input.SetText("CMD", "net use " ..Drive.. " "  ..IP.. " /user:" ..User.. " " ..qut ..PWD ..qut.. "");
    set = Input.SetText("CMD", "net use " ..Drive.. " "  ..IP.. " /user:" ..User.. " " ..qut ..PWD ..qut.. "\r\npause");
    
    get = Input.GetText("CMD");
    
    TextFile.WriteFromString("C:\\TSAT\\MapDrive.bat", get.. "", false);
    
    File.RunAs("C:\\TSAT\\MapDrive.bat", "", "", SW_SHOWNORMAL, false, RunUID, RunPWD, "", LOGON_NO_OPTION, nil, nil);
    
    --Check to see if an error occurred.
    error = Application.GetLastError();
    
    -- If an error occurred, display the error message.
    if (error ~= 0) then
      Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
    end
    
    
    TextFile.WriteFromString("AutoPlay\\Docs\\SystemLog.txt", System.GetDate(DATE_FMT_US).." | "..System.GetTime(TIME_FMT_AMPM).." | Map Drive was run\r\n", true);
    
    -- Write out the IP and UID to a text file
    TextFile.WriteFromString("AutoPlay\\Docs\\IP.txt", IP, false);
    TextFile.WriteFromString("AutoPlay\\Docs\\User.txt", User, false);
    TextFile.WriteFromString("AutoPlay\\Docs\\Drive.txt", Drive, false);
    TextFile.WriteFromString("AutoPlay\\Docs\\PWD.txt", PWD, false);

  • #2
    Attached is the project. Thanks
    Attached Files

    Comment


    • #3
      When you run the batch file directly, it is executed in the current user's context. If you use File.RunAs(), it executed the command in the other user's (specified as RunUID) context. The exact same would most likely happen if you use runas at the command line.

      You don't need to create a batch file at all to map a drive, instead you can execute net.exe (found in _SystemFolder) with File.Run() and pass the desired parameters as the argument.
      Code:
      File.Run(_SystemFolder .. "\\net.exe", "use blah blah", "", SW_SHOWNORMAL, true);
      Ulrich

      Comment


      • #4
        Using the following code I get "File Execution error."

        Code:
        Drive = Input.GetText("Dletter");
        IP = Input.GetText("ip");
        User = Input.GetText("User");
        PWD = Input.GetText("Password");
        qut = Input.GetText("qut");
        
        File.Run_SystemFolder .. "\\net.exe", "use" ..Drive.. " " ..IP.. " /user:" ..User.. " " ..qut ..PWD ..qut.. "", SW_SHOWNORMAL, true);

        Comment


        • #5
          The code below does not give a file execution error. It's runs but does not map a drive. What am I missing? Thanks

          Code:
          RunUID = TextFile.ReadToString("AutoPlay\\Content\\Common\\CommonA.dat");
          RunPWD = TextFile.ReadToString("AutoPlay\\Content\\Common\\CommonP.dat");
          
          Drive = Input.GetText("Dletter");
          IP = Input.GetText("ip");
          User = Input.GetText("User");
          PWD = Input.GetText("Password");
          qut = Input.GetText("qut");
          
          
          set = Input.SetText("CMD", "use " ..Drive.. " "  ..IP.. " /user:" ..User.. " " ..qut ..PWD ..qut.. "");
          ----set = Input.SetText("CMD", "net use " ..Drive.. " "  ..IP.. " /user:" ..User.. " " ..qut ..PWD ..qut.. "\r\npause");
          
          CMD = Input.GetText("CMD");
          
          
          File.Run(_SystemFolder .. "\\net.exe", CMD.. "", "C:\\Windows\\System32", SW_SHOWNORMAL);
          
          
          --Check to see if an error occurred.
          error = Application.GetLastError();
          
          -- If an error occurred, display the error message.
          if (error ~= 0) then
            Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
          end

          Comment


          • #6
            Don't use File.run as you need to make the bat file use CMD, use this instead:

            Example:
            Code:
            result = Shell.Execute(_SystemFolder.."\\net.exe", "open", CMD, _SystemFolder, SW_SHOWNORMAL, false);
            
            or change your code to this:
            
            result = File.Run(_SystemFolder.."\\net.exe", CMD, _SystemFolder, SW_SHOWNORMAL, false);
            In your project settings, go to the advanced tab and check - require administrator. I find sometimes running program from system32/system require this to work.

            Comment


            • #7
              mrdude,

              Tried using both sets of code but they return with "System Error 85" Which I think is a net use admin rights issue.

              Comment


              • #8
                Originally posted by Widget View Post
                mrdude,

                Tried using both sets of code but they return with "System Error 85" Which I think is a net use admin rights issue.
                I think if your program is trying to run a command from system32 such as cmd/net etc - you need to be admin for it to work. I have had this happen to me loads of time - for example if I want to call a batch file everything seems to work properly, but doesn't do anything - then I invoke as admin - then it works without any code changes.

                That's probably why you're having issues - although you can run a batch file manually - I think to test, you can go to your apms autorun.exe and right click on it - run as administrator. To check this.

                Comment


                • #9
                  mrdude,

                  I tried using File.RunAs and that also fails. Using RunAs on other batch files works fine except for using net use.

                  RunAs should work if using the correct account that has admin rights?

                  Comment


                  • #10
                    I have admin rights on my pc as well - however that doesn't mean every program that runs on it does, you still need to tell the computer that you apms autorun.exe is allowed to run system32 files and make changes to other files - so you need to make the run as admin, like I said above.

                    Just try it and if it works - you'll know what the problem is, if not???

                    Comment


                    • #11
                      using
                      Code:
                      File.RunAs("C:\\TSAT\\MapDrive.bat", "", "", SW_SHOWNORMAL, false, RunUID, RunPWD, "", LOGON_NO_OPTION, nil, nil);
                      launch the batch file and says the command complete successfully, but no drive mapping was mapped. I did run autorun as admin.


                      Not sure sure why this does not work. Seems fairly simple to do!?

                      Is there away to save the batch file as IP from IP = Input.GetText("ip");? Example the batch file would be called MappedDrive_192.168.1.1.bat.

                      Thanks

                      Comment


                      • #12
                        Also, using the same account as I did for RunAs, I can manually click on the batch file and it does map the drive.

                        Tried on Windows 7 and Windows 10 both have the same results.

                        Comment


                        • #13
                          Originally posted by Widget View Post
                          using
                          Code:
                          File.RunAs("C:\\TSAT\\MapDrive.bat", "", "", SW_SHOWNORMAL, false, RunUID, RunPWD, "", LOGON_NO_OPTION, nil, nil);
                          launch the batch file and says the command complete successfully, but no drive mapping was mapped. I did run autorun as admin.


                          Not sure sure why this does not work. Seems fairly simple to do!?

                          Is there away to save the batch file as IP from IP = Input.GetText("ip");? Example the batch file would be called MappedDrive_192.168.1.1.bat.

                          Thanks
                          Open a command prompt on your PC in the same location as your bat file: (go to the folder - hold shift/right click on your mouse - open command window here), then type the name of your bat file. You should see in the command window what it's doing, first thing is to make sure that your batch file is working properly.

                          Comment


                          • #14
                            Here's an example of some code I made ages ago - it only works if I invoke apms as administrator in the settings:

                            Code:
                            args = ' interface ipv4 set address "xxxxx" static 192.168.100.10 255.255.255.0 192.168.100.1 1';
                            args2 = String.Replace(args, "xxxxx", lanname, false);
                            --Shell.Execute("AutoPlay\\Docs\\netsh.exe", "open", args2, "AutoPlay\\Docs\\", false, true);
                            File.Run(_SystemFolder .. "\\netsh.exe", args2, _SystemFolder, false, true);
                            And doesn't work on windows xp - but works on everything upwards from that.

                            Comment


                            • #15
                              Batch file works fine.

                              C:\TSAT>mapdrive.bat

                              C:\TSAT>net use t: \\10.10.10.40\C$ /user:administrator "password"
                              The command completed successfully.

                              I now have a drive mapping. running the batch manually works every time. When calling it up through AMS it says the same results (Complete successfully) but does not map the drive. Double click the batch file and it work.

                              I posted my project, can you change the code to reflect your user info to see if it works for you? Thanks

                              Comment

                              Working...
                              X