Vivaed

Running PS1 Script with Parameters

24 posts in this topic

Trying to automate the sideload of a Windows Store app via .ps1 script...

RunWait(@ComSpec & ' /c' & 'C:\"WB Resources"\APP_Prod\Add-AppDevPackage.ps1')

Above doesn't work... I assume this is trying to open in CMD.

 

Normally we right click the .ps1 and open with powershell, then we have to type "A" to start the install, it installs and then type any yet to exit.
However I cant get the powershell to even open.

Kind of puzzled here, any suggestions would be a help.

PS I also tried to convert the .ps1 to exe, but that was a huge failure... lol

Share this post


Link to post
Share on other sites



We need to use powershell command for running powershell script through command prompt.

Eg:-

powershell "C:\Dummy Directory 1\Foo.ps1"

Share this post


Link to post
Share on other sites

Try this:

ShellExecuteWait('"C:\WB Resources\APP_Prod\Add-AppDevPackage.ps1"')

If it still doesn't work, try it without the outer single quotes around the path and file name. You can't use Run or @ComSpec with a PS script, you need to use ShellExecute. You were also missing the space after /c as well but that wouldn't have helped anyways.


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

Share this post


Link to post
Share on other sites
11 minutes ago, BrewManNH said:

You can't use Run or @ComSpec with a PS script

i beg to differ

 

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
1 minute ago, iamtheky said:

i beg to differ

 

Beg all you wish, but you are NOT using Run to run the ps1 script, you are using Run to run the powershell program and passing the script name to it. That is a major difference in what the OP was using in his script. Perhaps it would have been clearer to have said "You can't use Run or @ComSpec to directly run a PS script", but my point still stands as accurate.


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

Share this post


Link to post
Share on other sites

no its not, you said you cant use X with Y.  When clearly you can, you just must specify the application.  If accurate = misleading then yes, you did great, head pats all around.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
21 minutes ago, BrewManNH said:

You can't use Run or @ComSpec with a PS script,

Hi Vivaed,

 

You can run powershell script with RunWait command, like we run a powershell script in command prompt.  But ShellExecuteWait can also be used.

 

Thanks

Anoop

Share this post


Link to post
Share on other sites
23 minutes ago, BrewManNH said:

Try this:

ShellExecuteWait('"C:\WB Resources\APP_Prod\Add-AppDevPackage.ps1"')

If it still doesn't work, try it without the outer single quotes around the path and file name. You can't use Run or @ComSpec with a PS script, you need to use ShellExecute. You were also missing the space after /c as well but that wouldn't have helped anyways.

This gets the Shell to open, I see red text with errors but it closes, thanks for the progress!

Share this post


Link to post
Share on other sites
6 minutes ago, Vivaed said:

This gets the Shell to open, I see red text with errors but it closes, thanks for the progress!

What errors you are getting. You may have to set the execution policy.

Share this post


Link to post
Share on other sites
Just now, Anoop said:

What errors you are getting. You may have to set the execution policy.

Not showing, the window closes when it errors, I wish I could pump the output to the console... is that possible?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

7 minutes ago, Vivaed said:

Not showing, the window closes when it errors, I wish I could pump the output to the console... is that possible?

Can you try with command prompt?

Edited by Anoop

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

14 minutes ago, iamtheky said:

no its not, you said you cant use X with Y.  When clearly you can, you just must specify the application.  If accurate = misleading then yes, you did great, head pats all around.

Are you trying to be pedantic about this, or do you have a point? I said, and I still say, you can't run a ps1 file with Run. You HAVE to use the powershell.exe program to run it. To run it directly without specifying the program you HAVE to use ShellExecute. Where was I wrong in what I wrote before? You can not use Run to directly execute a .ps1 file, so you can't use x with y is 100% accurate if read as it is written and not with a hidden agenda you seem to have.

Edited by BrewManNH

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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

1 hour ago, Anoop said:

Cam you try with command prompt?

Keeps throwing an error about the space between C;\WB and Resources

C:\Windows\System32\WindowsPowerShell>powershell.exe -Command "C:\WB Resources\APP_Prod\Add-AppDevPackage.ps1"
C:\WB : The term 'C:\WB' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ C:\WB Resources\Wellbeats.Universal_2.2.41.0_Prod\Wellbeats.Universal ...
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\WB:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

# I am running As Admin

# I also triple checked the path and all spelling, everything looked good

Edited by Vivaed

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

sorry, late to the game.  Have you tried wrapping that entire command in single quotes?  If you look at my post you will see i have separated the command, and then you could just call $commmand wrapped in quotes, which also usually works just fine.

something like:

$command = "C:\WB Resources\APP_Prod\Add-AppDevPackage.ps1"

run('powershell.exe -Command ' & '"' & $command & '"')

 

*idk if its chrome or IPB (or a combo) but code boxes are jacked

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
9 minutes ago, iamtheky said:

sorry, late to the game.  Have you tried wrapping that entire command in single quotes?  If you look at my post you will see i have separated the command, and then you could just call $commmand wrapped in quotes, which also usually works just fine.

something like:

$command = "C:\WB Resources\APP_Prod\Add-AppDevPackage.ps1"

run('powershell.exe -Command ' & '"' & $command & '"')

 

*idk if its chrome or IPB (or a combo) but code boxes are jacked

Just tried it, didnt see a window or anything, not sure if even ran, but the app is not installed

Share this post


Link to post
Share on other sites
1 hour ago, BrewManNH said:

You can not use Run to directly execute a .ps1 file, so you can't use x with y is 100% accurate if read as it is written and not with a hidden agenda you seem to have.

I'm not arguing semantics, thats why i quoted you.  Your statement is not accurate. Your revisions are better, but revisionist history is for the badged folk.  You seem to think you need to be right and could not possibly have misrepresented the facts.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
1 minute ago, Vivaed said:

Just tried it, didnt see a window or anything, not sure if even ran, but the app is not installed

do a cmd /k powershell.exe so you can watch it and we can see the failure


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
5 minutes ago, iamtheky said:

do a cmd /k powershell.exe so you can watch it and we can see the failure

Still barking an error about the space:

C:\WB : The term 'C:\WB' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\WB Resources\Wellbeats.Universal_2.2.41.0_Prod\Wellbeats.Universal ...
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\WB:String) [], CommandNotFou
   ndException
    + FullyQualifiedErrorId : CommandNotFoundException

 

My code:

$iDir = "C:\WB Resources\Wellbeats.Universal_2.2.41.0_Prod\Wellbeats.Universal_2.2.41.0_Prod\Add-AppDevPackage.ps1"

Run('cmd /k C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command ' & '"' & $iDir & '"')

 

I wish I could change the space but we have 2000 systems in the field with this naming convention

Share this post


Link to post
Share on other sites

The -Command parameter is for directly running command strings with PS, not for running scripts with it. Try it without using the -Command parameter.


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

Share this post


Link to post
Share on other sites

Use -File instead of -Command.  

 

Adam

 

1 person likes this

Share this post


Link to post
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

  • Similar Content

    • WannaBeGut
      While Runwait
      By WannaBeGut
      Hello,
      I just wrote a script that should copy data into my cloud using xcopy (cmd), but I want the Status Bar I have in my GUI to change it's text like that:
      working. -> working.. -> working... -> working. ...
      I would also like to make a button which interrupts xcopy (simply closing it should do the trick), but I don't know how to call ProcessClose("xcopy.exe") using a button, while I'am waiting for RunWait to finish.
      Example:
      $command = 'xcopy "' & @DesktopDir & '\text.txt"' & ' "\\ADMIN-CLOUD\private\" /EECHIY' $SW_STATE = @SW_HIDE RunWait(@ComSpec & " /c " & '"' & $command & '"', @DesktopDir, $SW_State) Please tell me if you need any further information! 
    • Vivaed
      Run(@ComSpec...) with schtasks
      By Vivaed
      I have this button in an app I am building:
      When you click it, it then opens a FileOpenDialog() I select and XML, then it asks for a name input, enter the name, then it should run the following code:
      schtasks.exe /create /tn XXXX-task /XML C:\WBRESO~1\WB-LOO~1\TESTIN~1.XML It works when I put it in the CMD and run it, I get SUCCESS! and the new task has been created in Task Scheduler.
      C:\Users\vivaed>schtasks.exe /create /tn XXXX /xml C:\WBRESO~1\WB-LOO~1\TESTIN~1.XML SUCCESS: The scheduled task "XXXX" has successfully been created. But when I run it in:
      (This is just a snippet of the code, there is a lot more)
      #include <AutoItConstants.au3> Global $wbResources = 'C:\"WB Resources"\WB-LoopChecker\' Case $Button4 $getXML = FileOpenDialog("Find task to import", $wbResources ,"XML (*.xml)") If @error Then ; Display the error message. MsgBox($MB_SYSTEMMODAL, "", "No file was selected.") Else Local $taskName = InputBox("Name this task","File: " & @CRLF & $getXML & @CRLF & "Name this task:" & @CRLF,"XXXX-task") $schedTask = 'schtasks.exe /create /tn ' & $taskName & ' /XML ' & FileGetShortName($getXML) Run(@ComSpec & " /c " & $schedTask, @SW_HIDE) ConsoleWrite($wbResources & @CRLF) ConsoleWrite($schedTask & @CRLF) EndIf Here I dont get any errors but the when I refresh the Task Scheduler, there is no new task....
       
    • inopia
      Error when using ($STDIN_CHILD, $STDOUT_CHILD)
      By inopia
      Hey fellow scripters!
      I wanted to create a script to change the bitlocker PIN of our Win7 machines for users without admin rights. While researching I found out, that this doesn't seem to be an easy task. I came up with a pretty dirty solution:
      $gui=GUICreate("Bitlocker PIN",180,180,-1,-1,$WS_SYSMENU,-1) GUICtrlCreateLabel("PIN eingeben (min. 6 Zeichen):",15,15,150,15,-1,-1) $bit1=GUICtrlCreateInput("",15,30,150,20,$ES_PASSWORD,$WS_EX_CLIENTEDGE) GUICtrlCreateLabel("PIN bestätigen:",15,60,77,15,-1,-1) $bit2=GUICtrlCreateInput("",15,75,150,20,$ES_PASSWORD,$WS_EX_CLIENTEDGE) $button=GUICtrlCreateButton("Neue PIN Setzen",35,110,110,30, $BS_DEFPUSHBUTTON, -1) GUISetState(@SW_SHOW,$gui) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit ExitLoop Case $button $res1=GUICtrlRead($bit1) $res2=GUICtrlRead($bit2) If $res1 = $res2 And StringLen($res1) >= 6 Then GUISetState(@SW_HIDE,$gui) ClipPut ($res1) BlockInput(1) $proc=RunAs($o1acc, $domain, $o1pwd, 2, @ComSpec & " /c C:\Windows\System32\manage-bde.exe -changepin c:") WinWaitActive("C:\Windows\system32\cmd.exe","") Sleep(2000) Send ("!{Space}") Sleep(20) Send ("B") Sleep(20) Send ("E") Sleep(20) Send ("{ENTER}") Sleep(100) Send ("!{Space}") Sleep(20) Send ("B") Sleep(20) Send ("E") Sleep(20) Send ("{ENTER}") WinWaitClose("C:\Windows\system32\cmd.exe") Sleep(100) BlockInput(0) ExitLoop ElseIf StringLen($res1) < 6 Then MsgBox($MB_ICONERROR,"Fehler","PIN zu kurz. Minimum 6 Zeichen.") Else MsgBox($MB_ICONERROR,"Fehler","PINs sind nicht gleich."&@WindowsDir) EndIf EndSwitch Sleep(20) WEnd It works on my test system, but the problem here is, that a user could easily pause the script and have a nice cmd with elevated rights.
      So I wanted to give $STDIN_CHILD + $STDOUT_CHILD and StdoutRead + StdinWrite a try. The problem here was, that when I executed the command it would give me the following output:
      Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten. C:\Windows\System32>manage-bde.exe -changepin c: BitLocker-Laufwerkverschlüsselung: Konfigurationstoolversion 6.1.7601 Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten. FEHLER: Ein Fehler ist aufgetreten (Code 0x80070006): Das Handle ist ungültig. ~ Error: An error occured (Code 0x80070006)
          Invalid handle .
      This is the code that I tried:
      $pid = RunAs($acc, $domain, $pwd, 2, @ComSpec, "C:\Windows\System32\", @SW_SHOW, BitOR($STDERR_CHILD, $STDIN_CHILD, $STDOUT_CHILD)) StdinWrite($pid, "manage-bde.exe -changepin c:" & @CRLF) While Sleep(50) $sOut = StdoutRead($pid) If @error Then ExitLoop If $sOut <> "" Then ConsoleWrite($sOut & @CRLF) WEnd Has anyone experienced errors like this (or has a better solution for changing the bitlocker PIN)?
       
      Regards
      ino
    • khathiatz
      UninstallMe - Automatically destroy script
      By khathiatz
      UninstallMe will automatically destroy script running
      - With example files
      - Automatic, using file BAT and VBS to destroy, no GUI
       
       
      English: https://khathiatz.blogspot.com/2016/08/uninstall-me-en.html
      Tiếng Việt: https://khathiatz.blogspot.com/2016/07/uninstall-me.html