Create script to reboot computers where hostnames are located in text file

Viewed 334

I am trying to create a script to set off a command to restart machines where the hostnames are located in a .txt file. I am running this on Jenkins so ideally this should be in Groovy, however a shell script would work.

I have tried to use Groovy to create this code. My aim is to locate the .txt file and apply this CMD command to it

String fileContent = new File("C:/Users/omr/Documents/Temp/test.txt").text
println fileContent

"cmd /c shutdown -r -t 0".execute(fileContent)

So far Ive not had any luck, I've researched a lot how to execute cmd commands from Groovy and they all state .execute(), however I don't feel this is right.

2 Answers

If you need to do it on Windows, try PsShutdown or NirCmd utility to reboot the remote computer. And a batch file to cycle through your hostnames:

@echo off
for /F "tokens=*" %%A in (test.txt) do (
  echo psshutdown -r -t 0 \\%%A
)

I don't think Java/Groovy is really needed here, only maybe as a wrapper around shell script.

To run remotely, you can use psexec. To read the test file in groovy, you have the following two solutions:

String fileContents = new File('/path/to/file').text

If you need to specify the character encoding, use the following instead:

String fileContents = new File('/path/to/file').getText('UTF-8')

If you nedd a read file Line Bye Line.

File file1 = new File("C:\path\to\file\myTestfile.txt");
for (def i=0;i<=30;i++) // specify how many line need to read eg.. 30
{
 log.info file1.readLines().get(i)

}

You can also use cmd to read the text file.

FOR /F %i IN (file.txt) DO @echo %i
Related