Как выполнить команду через powershell с помощью Runtime.exec()?
Например, что бы выпонлить команду dir через cmd, и оставить консоль открытой нужно написать так:
Runtime.getRuntime().exec("cmd /c start cmd /k dir");
А как сделать аналогичное через powershell? Пробовал следующее и многое другое, безуспешно:
Runtime.getRuntime().exec("powershell /c start cmd /k dir"); // нет
Runtime.getRuntime().exec("powershell start powershell dir"); // команду выполняет, но закрывает окно, а как параметр добавить неопнятно
Runtime.getRuntime().exec("powershell start powershell -noexit dir"); // нет
Runtime.getRuntime().exec("powershell start powershell -noexit -command dir"); // нет
Runtime.getRuntime().exec("powershell -command start powershell -noexit -command dir"); // нет
Runtime.getRuntime().exec("powershell start powershell \"-noexit\", \"-command {dir}\""); // нет
Runtime.getRuntime().exec("powershell start powershell \"-noexit\", \"-command {dir}\""); // нет
Runtime.getRuntime().exec("powershell \"-command start powershell '-noexit', '-command dir'\""); // нет
Runtime.getRuntime().exec("powershell -command {start powershell \"-noexit\", \"-command dir\"}"); // нет
Ответы (1 шт):
Автор решения: Etikoriya
→ Ссылка
Используя синтаксис скриптового языка powershell. Из-за его богатых возможностей, полагаю, если написать так же просто, как для cmd – может быть не ясно что именно нужно сделать.
Runtime.getRuntime().exec("powershell -command \"& start powershell '-noexit', '-command dir'\"");
// powershell -command "& start powershell '-noexit', '-command dir'"