0

I'm trying to trigger some sort of Folder Selection Dialog, I have a working model with nodejs and the powershell but it only works when the server and client are on the same machine. I need the prompt to occur on the client side triggered from the browser. From what i understand I can not trigger Powershell from Chrome? So is there an alternative or am i just screwed?

My current Powershell script

{
    param([string]$Description="Select Folder",[string]$RootFolder="Desktop")

 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
     Out-Null     

   $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
        $objForm.Rootfolder = $RootFolder
        $objForm.Description = $Description
        $Show = $objForm.ShowDialog()
        If ($Show -eq "OK")
        {
            Return $objForm.SelectedPath
        }
        Else
        {
            Write-Error "Operation cancelled by user."
        }
    }

$folder = Select-FolderDialog # the variable contains user folder selection
write-host $folder

My javascript function


async function asyncfindDir() {
          //executes powershell script
       let promise = new Promise((resolve, reject) => {
        const Shell = require('node-powershell');
        const ps = new Shell({
          executionPolicy: 'Bypass',
          noProfile: true
        });

        ps.addCommand('./selectfolder.ps1');
        ps.invoke()     
        .then(output => {
          //console.log(output);

          var shelloutput = output;
          console.log (shelloutput + '^^from external script');

          res.send(shelloutput);
        })
        .catch(err => {
          console.log('please select a directory path')
          //console.log('err');
        });
});
};

Is there anyway to get that working locally? Is there a trigger i'm not aware of to access that kind of dialog from the browser? I know i'm not the only person with this issue but i have yet to see a real solution.

4
  • I'm not sure what you're trying to do is a good idea. Any particular reason you can't implement the folder dialog in just JavaScript? Something like This? Commented May 28, 2019 at 19:49
  • From what i've seen, there's no way to select just a directory in javascript, I need a directory path saved without a file selected. If there is a way to prompt for a directory selection I'm eager to hear, just haven't been able to find a way myself. Commented May 28, 2019 at 20:00
  • It looks like there are a lot of security implications on what you're trying to do. What exactly are you going to use the path for? You're not asking for a specific file, which leads me to believe you're going to be saving files to that directory. But you can't use JavaScript to save files to a local directory. Commented May 28, 2019 at 20:10
  • I have an executable file that will use the path as a parameter, I need a easy way for my end user to select a directory path and save it as a variable for my exe to pull. I understand it's not a secure method but it's for our internal use only demo, i'm not too concerned about security at this point. Commented May 28, 2019 at 20:29

1 Answer 1

0

Short answer: No.

Longer answer, is best illustrated by rephrasing your question with a different script name:

Using my browser, can I click on a link to visit a website, and have it run a random PowerShell script called Delete_All_Files.ps1?

Answers why you will never be able to run a PowerShell script from a browser, on a remote machine, and why browsers will deliberately block you from doing it, because people usually don't want to have all their files deleted when they click on a random link in their email.

If you want to run PowerShell scripts on remote machines, then you should look into PSRemoting and Enter-PSSession.

@kuzimoto is right. If you just want to display a folder dialog box, there are easier ways to do that and Fine Uploader is an easier way.

Replying to your comment: If you want to specify a directory name, the reason you can't do it is because you are essentially asking:

Using my browser, can I click on a link to visit a website, and have it run a script that will enumerate through all the files and folders in my C:\ so that it can choose the folder C:\users\Justin Miller\Desktop\SECRET FILES\?

The reason both operations do not work is because both operations require local computer access. i.e. local script execution access, and local directory knowledge access. Security-wize, we, in general, don't want to visit a random website and have it execute random code, or know what files/folders I have on my machine, which is why you won't be able to do what you want to try to do.

Sign up to request clarification or add additional context in comments.

4 Comments

Here's a question, every file upload/selection tool out there does the same thing i need to do but does it to a file level. I only need the ability to select the directory and save it, i'm wondering is there a way to hack a file upload function to not require an actual file to be selected?
Not exactly, but could possibly be hacked to do what i need. it alerts the folder name but not the actual directory path.it has a bunch of other things going on that i need to figure out how to modify.
Yeah, that's what I thought. It will require some hacking together, but it's sort of the closest thing you'll get to what you want. In general, treat browsers like a sandbox, where they try really hard to not reveal any information about what they are running on. Another thought would be to try to "guess" paths based on user login names and machine names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.