-1

I am new to powershell.I want to open the command promt as Admin and change the directory using powershell script to a destination folder "D:\Temp" and and run a foo.war file

I have tried various forms by adding -Path "location of folder" to the command Start-Process -verb RunAs cmd.exe and -Working Directory

Start-Process -verb RunAs cmd.exe

When I run the powershell script it must open the command prompt as Admin and change to Directory "D:\Temp"

aditya pratti
  • 35
  • 3
  • 11
  • What is the purpose? Why not just stay in PowerShell? – Bill_Stewart Jun 07 '19 at 14:26
  • I am trying to automate installation of Product which involves using cmd and running war file – aditya pratti Jun 07 '19 at 14:29
  • You need to describe the goal, not the steps. (Tell _what_ you want to accomplish rather than _how_ you think it should be done.) Automation of product installation is not really a general programming topic because there are so many variables (e.g., What product? What automation options for deployment are available with said product? What tools does the product's vendor provide for automatic deployment? etc.) It may be your question is off-topic for StackOverflow. – Bill_Stewart Jun 07 '19 at 14:31
  • I want to achieve "opening command Prompt as Admin and then change to a particular directory "D:\Temp\Todaydate" and run a foo.war file in Command Prompt window, product we are using runs on Windows servers,i just need help in powershell script , I was able to open the CMD PRMPT as ADMIN but not able to change the directory.so i think this is the right forum to ask. – aditya pratti Jun 07 '19 at 14:41
  • Why cmd prompt? Why not PowerShell? – Bill_Stewart Jun 07 '19 at 14:42
  • See also: https://stackoverflow.com/questions/43494863/start-process-workingdirectory-as-administrator-does-not-set-location – mhu Jun 11 '19 at 11:20

1 Answers1

0

Try this (will prompt for UAC):

Start-Process -FilePath "cmd.exe" -ArgumentList "/K cd /d D:\temp" -Verb "runas"

For executing the war, you have several options:

  • Use & to separate commands:

    Start-Process -FilePath "cmd.exe" -ArgumentList "/K cd /d D:\temp&jar.exe -xvf foo.war" -Verb "runas"
    
  • Place the commands in a batch script

    Start-Process -FilePath "cmd.exe" -ArgumentList "/K D:\Temp\runjar.cmd" -Verb "runas"
    

    With batch script runjar.cmd:

    @echo off
    cd /d D:\temp
    jar.exe -xvf foo.war
    
mhu
  • 17,720
  • 10
  • 62
  • 93
  • Hi @mhu i got another use-case here i want to execute a war file passing JavaPath"\apps\9.0\base\java\8.0\bin\jar.exe" -xvf foo.war" from the same location "D:\temp", Could you assist me with command to run a war – aditya pratti Jun 10 '19 at 16:03
  • Hi @mhu recently i got a scenario where i need to generate the warfile using start-process, But when i am trying to generate the war it is showing the help page of jar.I used $wargen=Start-Process -FilePath "cmd.exe" -ArgumentList "/K cd /d D:\Temp\ws\9.0\base\java\8.0\bin\jar.exe -cvf foo.war" -Verb "runas" -Verbose – aditya pratti Jul 01 '19 at 19:03
  • You need to separate the command with an ampersand `&`, as shown in the first example in the answer. So: `cd /d &\jar.exe ` – mhu Jul 01 '19 at 19:37