crear un script en power shell

by cristianrg at 2012-09-13 08:33:59

que pregunte al usuario por el nombre de una aplicación del sistema, busque si esta aplicación se esta ejecutando actualmente, y si lo esta, pregunte al usuario si desea parar la aplicación.

Ejemplo de las impresiones en pantalla del script:

Script para búsqueda de aplicaciones en ejecución Ingrese el nombre de la aplicación :

La aplicación “XXXX” se esta ejecutando en el sistema

¿Desea parar la aplicación? (S/N)


La aplicación “XXXX” ha sido detenida!
by DonJ at 2012-09-13 08:38:18
I’m sorry, most of us here aren’t fluent in Spanish. Can you try re-posting in English, please?
by willsteele at 2012-09-13 08:42:48
Para el autor. Lo siento, la mayoría de nosotros no dominan el español. ¿Se puede tratar de volver a publicar en Inglés, por favor? Por favor, trate de usar http://translate.google.com.

Google translate (http://translate.google.com) gave me this:

prompt the user for the name of a system application, look if this application is currently running, and if so, ask the user to stop the application.

Example of screen printing script:

Script for running applications search Enter the name of the application:

The "XXXX" is running on your system

Want to stop the application? (S / N)


The "XXXX" has been arrested!
by willsteele at 2012-09-13 08:53:27
Usted tendrá que añadir más lógica para detener una instancia del programa. de lo contrario, este script se detenga todas las instancias de un programa en ejecución. Salí de la WhatIf parámetros para que pueda mostrar lo que sucedería si lo utiliza.

Translation: You will need to add more logic to stop one instance of the program. otherwise, this script will stop all instances of a running program. I left the -WhatIf parameter so it can show you what would happen if you used it.

$aplicación = Read-Host -Prompt "Introduzca el nombre de la aplicación"
if(Get-Process -Name $aplicación)
{
"La aplicación "$aplicación" se esta ejecutando en el sistema"
$desea = Read-Host -Prompt "¿Desea parar la aplicación? (S/N)"
if($desea -eq ‘S’)
{
Stop-Process -Name $aplicación -WhatIf
}
elseif($desea -eq ‘N’)
{
Write-Output "La aplicación "$aplicación" no fue detenida"
}
}
by DonJ at 2012-09-13 09:12:12

$app = Read-Host ‘Enter the name of the application’
$procs = Get-Process | Where { $.Name -like "$app" }
if ($procs.count -gt 0) {
Write-Host "$app is running on your system."
$choice = $host.UI.PromptForChoice(‘Want to stop the application?’,‘Choose:’,[system.management.automation.host.choicedescription[]]@(‘&Yes’,‘&No’,2)
if ($choice -eq 0) { Stop-Process $procs }
}
by turbomcp at 2012-09-13 14:59:48
pretty cool
i learned spanish and something new in powershell at the same time:)

i tried running the above code, seems it was missing a ) after 2)
but even after that showed error
$app = Read-Host 'Enter the name of the application'
$procs = Get-Process | Where { $
.Name -like "$app" }
if ($procs.count -gt 0) {
Write-Host "$app is running on your system."
$choice = $host.UI.PromptForChoice('Want to stop the application?','Choose]]@('&Yes','&No',2))

if ($choice -eq 0) { Stop-Process $procs }
}


after the prompt for app got this:
Cannot find an overload for "PromptForChoice" and the argument count: "3".
At line:5 char:137
+ … escription]@(‘&Yes’,‘&No’,2))
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
by poshoholic at 2012-09-13 20:05:22
You put the missing bracket in the wrong place. Put it before the ",2", not after it, like this:
$app = Read-Host 'Enter the name of the application'
$procs = Get-Process | Where { $.Name -like "$app" }
if ($procs.count -gt 0) {
Write-Host "$app is running on your system."
$choice = $host.UI.PromptForChoice('Want to stop the application?','Choose]]@('&Yes','&No'),2)

if ($choice -eq 0) { Stop-Process $procs }
}
by turbomcp at 2012-09-14 04:18:02
$app = Read-Host 'Enter the name of the application'
$procs = Get-Process | Where { $
.Name -like "$app" }
if ($procs.count -gt 0) {
Write-Host "$app is running on your system."
$choice = $host.UI.PromptForChoice('Want to stop the application?','Choose]]@('&Yes','&No'),2)

if ($choice -eq 0) { Stop-Process $procs }
}


when i do that i get this:
Exception calling "PromptForChoice" with "4" argument(s): "Specified argument was out of the range of valid values.
Parameter name: defaultChoice"
At line:5 char:137
+ … escription]@(‘&Yes’,‘&No’),2)
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: , MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
by turbomcp at 2012-09-14 04:20:07
i changed it to 0
and it was good to go
by poshoholic at 2012-09-14 05:19:02
Sorry, I did that without running it. I should have just run the script first. :slight_smile:

The last integer value is the 0-based index of the button that you want to use as a default. That’s why 2 doesn’t work. 0 will make Yes be the default, and 1 will make No be the default.