0

I have a PS script that I need to run without the user having to open powershell - the easiest thing in my mind is writting an application or .bat file that will run the PS script. However I cant get the script to run using any other languages.

Ive tried using java but it doesnt run the script, neither does C# or a batch command. Ill attach the batch below as that seems to be the easier method im just not sure why it doesnt work - theres no error messages it just doesnt work.

Edit** The Powershell script is below and can be run. I need a third party application that will run the script.

$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Select a Folder that you want to Analyze
Please be aware the larger the folder size the Longer it will take to Generate")
@echo off
 
powershell.exe -ExecutionPolicy Unrestricted -Command "'I:\TestScript\FolderInformation.ps1'"
 
TIMEOUT /T 10

I just need to be able to run the script by launching a single application as its for users who wont know to use ISE or any other PS applications.

Any application or script ive used thus far doesnt work besides just using ISE to run it.

***** Java Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PowerShellJavaDemo {

    public static void main(String[] args) throws IOException {

        String line;

        String command = "powershell.exe & \"I:\\testingscript\"";

        Process powerShellProcess = Runtime.getRuntime().exec(command);

        powerShellProcess.getOutputStream().close();

        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        while ((line = stdout.readLine()) != null) {
            System.out.println("Output: " + line);

        }

        stdout.close();

        BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            System.out.println("Output: " + line);
        }
        stderr.close();

    }

}
    
  • And the options listed [here](https://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script/2035209) does not solve the problem? – aksappy Jul 28 '21 at 15:19
  • I would advise you to read what the `-file` and `-command` options do. – Squashman Jul 28 '21 at 15:20
  • Its not that I cant run the PS script myself. the Script runs from ISE - I need to be able to run it through a different program – Michael Schwarz Jul 28 '21 at 15:26
  • Oh, so you want to write a program to launch this PS1. Gotcha, and what Java code did you use? May be that is a starting point for us – aksappy Jul 28 '21 at 15:28
  • its a script I had found online - ill add it to the post – Michael Schwarz Jul 28 '21 at 15:29
  • @MichaelSchwarz the link that was posted by aksappy shows you several ways to run powershell from a cmd prompt or batch file. – Squashman Jul 28 '21 at 15:38
  • So all you are trying to implement is for the user to select a folder through a GUI? – Squashman Jul 28 '21 at 17:09
  • 2
    Why do you want to use other script interpreters? That is really not necessary. It is possible to create a shortcut file (`.lnk` file) of which property __Target__ contains `%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -NoLogo -file "I:\TestScript\FolderInformation.ps1"` and customize the other properties like __Start in__ and __Comment__ (shown as tooltip on hovering mouse pointer over the shortcut). Then publish the shortcut file. – Mofi Jul 28 '21 at 18:35
  • If you don't know how to create a shortcut file, then look into Windows __Start__ menu which contains already a shortcut file with name __Windows PowerShell__. Right click on this shortcut file, click in context menu on __Copy__ and paste it, for example, on the Windows desktop with __Ctrl+V__. Right click on __Windows PowerShell__ on your desktop and use __Rename__ to rename the shortcut file to whatever you want. Right click once again on renamed shortcut file and click on last context menu item __Properties__ to open the dialog window to configure the properties. – Mofi Jul 28 '21 at 18:46
  • BTW: `explorer.exe` running as Windows shell opens on double click the shortcut file, reads all the properties and runs next the Windows kernel library function [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with the parameters as defined in the shortcut file. `cmd.exe`, `cscript.exe`, `wscript.exe`, `powershell.exe`, `java.exe`, `python.exe`, etc. use all on Windows the function `CreateProcess` to run an executable independent on how the function/command is named in the script language of the script interpreter. – Mofi Jul 28 '21 at 18:46

1 Answers1

0

Two things as shown below that might help.

  1. Remove the ' before and after the path\file. if this doesn't help then:
  2. Try using "Bypass" rather than "Unrestricted"

@echo off

powershell.exe -ExecutionPolicy Bypass -Command "I:\TestScript\FolderInformation.ps1"

TIMEOUT /T 10

I use this all the time to talk to our Servers and run updates or other important information.

NOTE: There is a chance that your Group Policy is limiting this. If nothing above works.

I might have a solution being you can use ISE.

start powershell_ise.exe "I:\TestScript\FolderInformation.ps1"

timeout /t 5

ECHO Set obkey = CreateObject("WScript.Shell") > temp.vbs

ECHO obkey.SendKeys "{F5}" >> temp.vbs

cscript temp.vbs

DEL temp.vbs

timeout /t 5

taskkill /im powershell_ise.exe

pause

  • Sadly that didnt work either - so it seems like it is a group policy thing. The issue is I dont have access to change those so im not sure the next step. Nothing seems to run the script regardless of language used. – Michael Schwarz Jul 28 '21 at 15:58
  • @MichaelSchwarz I see. If you are unable to open the Powershell.exe not Powershell_ISE.exe and run the script from in there, then the Group policy is stopping the Powershell from running. ISE is for testing usually and usually is not included in the Group Policy hence the reason you can use it(I think it is usually overlooked). You would need to gain access to the Group Policy or ask for it to be allowed to run on the systems you are trying to run it on through your Administrator. are you able to open gpedit.exe locally? – James Davis Jul 28 '21 at 16:44
  • I was tasked to write a program that basically searches a drive for specific files based on user input and it needs to be runnable by the other users. I could right click and run with ISE everytime but I was hoping to make it easier on the users and allow them to simply run something like a .bat and have the script run. – Michael Schwarz Jul 28 '21 at 17:59
  • Take a look I edited my answer to include a possible solution. Give it a try and let me know. – James Davis Jul 28 '21 at 19:03
  • That does indeed work, although I would have to remove the taskkill as it causes it close right after it opens. But it is one possible solution. – Michael Schwarz Jul 28 '21 at 19:18
  • Sorry for that, I thought the PowerShell was running another program:) – James Davis Jul 28 '21 at 19:22
  • No worries - its further along than I was able to get earlier so im grateful for that. – Michael Schwarz Jul 28 '21 at 19:41