1

I am trying to update my .bat script to restart a service on many machines by putting the name of each machine in a notepad. For this I am trying to run a powershell script from cmd, but I can't get it to work. I have read that using ";" you can make a script in a single line but it doesn't work for me.

I'm trying to put this powershell code:

$FicheroEquipos = Get-Content -Path 'C:\Users\Cloudpg\Desktop\Morralla\test\equipos.txt'
ForEach ($Variable in $FicheroEquipos){
Get-Service -Name $Variable | Restart-Service -Verbose
}

like this in my .bat

start powershell.exe "$FicheroEquipos = Get-Content -Path 'C:\Users\Cloudpg\Desktop\Morralla\test\equipos.txt';ForEach ($Variable in $FicheroEquipos){Get-Service -Name $Variable | Restart-Service -Verbose}"

If anyone has an idea how to achieve my goal I would appreciate the help. Thank you very much. (For the code that I have put, the variable is for the services since I cannot currently carry out the tests on several computers.)

1 Answer 1

0

Try this:

powershell.exe -command "$FicheroEquipos = Get-Content -Path 'C:\Users\Cloudpg\Desktop\Morralla\test\equipos.txt';
ForEach ($Variable in $FicheroEquipos){Get-Service -Name $Variable | Restart-Service -Verbose}"
2
  • Hello!, Thx for the answer, i test it but dosnt work (Puting the start in the begining), for aditional information, in the txt i have put "Spooler" for the tests. The command work using Powershell ISE but the problem is launch that script from CMD. I have a solution but not its beatifull and is make with cmd a file PS1 with the commands and launch that file, but im looking for other solution for this. Commented Oct 26, 2022 at 20:49
  • Ok! Works! I dont know for what making just a bat only for that didnt work, but i try from my real script and now its working very well. Thx @Ricardo, i share the line for next people need it: powershell.exe -command "$FicheroEquipos = Get-Content -Path 'C:\Users\Cloudpg\Desktop\Morralla\test\equipos.txt';ForEach ($Variable in $FicheroEquipos){Get-Service -Name $Variable | Restart-Service -Verbose}" Commented Oct 26, 2022 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .