Zum Inhalt springen

Script zum Herunterfahren von VMs in ESXi


Nopp

Empfohlene Beiträge

Hi Leute,

ich hab von folgender Seite: How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS. ein Script zum sauberen herunterfahren aller VMs auf einem ESX Host heruntergeladen und bin den Anweisungen auch treu geblieben. Ich bekomme aber eine Fehlermeldung. Ich bin selber kein Programmierer, weswegen ich hier etwas Hilfe brauche...

Anbei das Script mit anschließende Fehler.


REM

REM *** ESXi Shutdown Script for use with free-licensed ESXi ***

REM

REM Primarily designed to shutdown an ESXi server under UPS management software

REM control, i.e. APC PowerChute.

REM

REM Performs a graceful ESXi shutdown - note that the server will ultimately

REM be powered off after VMs are suspended or shutdown, depending on their

REM configuration.  As a result, when utility power is restored, the host

REM *will not restart* itself (unless the UPS battery was completely

REM depleted and the server BIOS was appropriately configured).

REM

REM

REM Written by James Pearce (vmware communities user ID J1mbo

REM (http://communities.vmware.com/people/J1mbo?view=profile).

REM

REM Version 4 (being for use with ESXi 4)

REM - tested against ESXi 4 U1 from a Windows 2003 VM

REM - completed 09-Dec-09

REM

REM Usage: Shutdown.bat [hostname] [username] [password]

REM

REM [hostname] must be valid DNS name or an IP address

REM [username] is a user with shutdown rights, i.e. root

REM [password] is as used to connect to vSphere Client with the specificed

REM            username.

REM

REM Usage is logged to shutdown.log, stored in the same directory as the script.

REM

REM Depends on supporting text files:

REM

REM - open-1.txt         - Establishes the session, part 1.

REM - open-2.txt         - Establishes the session, part 2.

REM

REM - auth-1.txt         - Authentication request, part 1.

REM - auth-2.txt         - Authentication request, part 2.

REM - auth-3.txt         - Authentication request, part 3.

REM

REM - shutdown-1.txt     - Shutdown request, part 1.

REM - shutdown-2.txt     - Shutdown request, part 2.

REM - shutdown-3.txt     - Shutdown request, part 3.

REM

REM

REM IMPORTANT: DEPENDS ON NMAP "NCAT" UTILITY BEING PRESENT AND ACCESSIBLE IN THE

REM DEFAULT SEARCH PATH.

REM

REM Free disrtibution and use permitted - but entirely at your own risk!!

REM

REM If you find it useful commerically, a bottle of ale would always be

REM welcome 

REM



REM First ensure in correct directory as APC software might not.

REM

REM These commands change the drive and directory to where the batch file is stored,

REM for example if it's at C:\ESXI\SHUTDOWN.BAT these lines become "C:" then

REM "cd \esxi\"


%~d0

cd %~dp0



REM Log the use of the script

call :WriteLog "System shutdown called: %date% %time%"



REM Check paramters were passed in:

If %1a==a goto :ArgumentError

If %2a==a goto :ArgumentError

If %3a==a goto :ArgumentError

call :CheckHost %1

if not %hostconnectres%==found Goto :CleanUp



REM Found and could ping the host, OK to attempt the shutdown.



REM Store specified hostname or IP in file to enable the assembly of the HTTP requests.

Echo Host: %1:443 >shutdown-host.txt

set shutdown-host=%1



REM Compute length of auth frame data and write out temp files...

call :GetAuthLength %2 %3

Echo Content-Length: %content_len% > content-len.txt



REM Now assemble the files.

copy open-1.txt + shutdown-host.txt + open-2.txt open.txt > NUL

copy auth-1.txt + shutdown-host.txt + content-len.txt + auth-2.txt authenticate.txt > NUL

Echo       ^<userName^>%2^</userName^>>> authenticate.txt

Echo       ^<password^>%3^</password^>>> authenticate.txt

copy authenticate.txt + auth-3.txt authenticate.txt > NUL




REM Announce our presence, so that the ESXi server will expect our authentication

REM attempt.

Echo Connecting...

ncat -C -i 2s --ssl %shutdown-host% 443 < open.txt > open-res.txt


REM Check that response was HTTP/1.1 200 OK:

call :GetResponse open-res.txt

if not %httpretcode%==200 Goto :ServerNotResponding



REM next, an authentication attempt which will produce a session key:

Echo Authenticating...

ncat -C -i 2s --ssl %shutdown-host% 443 < authenticate.txt > auth-resp.txt


REM Check that response was HTTP/1.1 200 OK:

call :GetResponse auth-resp.txt

if not %httpretcode%==200 Goto :AuthenticationError



Echo Creating host shutdown task...


REM Find the key, and insert it in pre-formed request split at appropriate position...

find "vmware_soap_session" auth-resp.txt > auth-key.txt

for /f "skip=2 tokens=1,2,3,4* delims==;" %%i in (auth-key.txt) do echo %%j > auth-key.txt

copy shutdown-1.txt + shutdown-host.txt + shutdown-2.txt + auth-key.txt + shutdown-3.txt shutdown.txt > NUL



REM Send completed request to the ESXi host:


REM FOLLOWING NCAT... IS THE DANGER LINE - REM IT OUT FOR INITIAL TESTING AS IT *WILL* SHUTDOWN

REM THE SPECIFIED ESXI HOST IF AUTHENTICATION SUCCEEDED!!


ncat -C -i 2s --ssl %shutdown-host% 443 < shutdown.txt > shutdown-resp.txt


REM Check that response was HTTP/1.1 200 OK:

call :GetResponse shutdown-resp.txt

if not %httpretcode%==200 Goto :ShutdownError



REM Result was OK - Log this.

call :WriteLog "Server accepted shutdown request."



Goto :Cleanup




REM --------------------------------------------------------------------------------

REM - Supporting "sub-routines"                                                    -

REM --------------------------------------------------------------------------------




REM --------------------------------------------------------------------------------

:ArgumentError

REM script was called without three parameters

Echo.

Echo Usage: Shutdown.bat [hostname] [username] [password]

Echo.

Echo [hostname] must be valid DNS name or an IP address

Echo [username] is a user with shutdown rights, i.e. root

Echo [password] is as used to connect to vSphere Client with the specificed

Echo            username.

Echo.

Echo Usage is logged to shutdown.log, stored in the same directory as the script.

Echo.

call :WriteLog "Missing parameter(s) - Usage: Shutdown.bat [hostname] [username] [password]"

Goto :End 




REM --------------------------------------------------------------------------------

:CheckHost

REM Routine to determine if host is valid and online.  Performs a Ping test.


REM Clear out any old files:

if exist test-connect.txt del test-connect.txt

if exist not-found.txt del not-found.txt

if exist found.txt del found.txt

if exist time-out.txt del time-out.txt

set hostconnectres=


REM Ping the host - first ping is desregarded as may time out due to arp delay:

ping %1 -n 1 > NUL

ping %1 -n 1 > test-connect.txt


REM Analyse the results and set environment variable hostconnectres:

find "could not find host" test-connect.txt > not-found.txt

find "Lost = 0" test-connect.txt > found.txt

find "Lost = 1" test-connect.txt > time-out.txt

for /f "skip=2 tokens=*" %%i in (not-found.txt) do if not a%%i==a set hostconnectres=cant_resolve

for /f "skip=2 tokens=*" %%i in (found.txt) do if not a%%i==a set hostconnectres=found

for /f "skip=2 tokens=*" %%i in (time-out.txt) do if not a%%i==a set hostconnectres=time-out


REM Clear-up temp files:

if exist test-connect.txt del test-connect.txt

if exist not-found.txt del not-found.txt

if exist found.txt del found.txt

if exist time-out.txt del time-out.txt


REM Write out results to the log:

if %hostconnectres%==cant_resolve call :WriteLog "Could not resolve specified hostname %1."

if %hostconnectres%==time-out call :WriteLog "Could not reach specified host %1: time-out."

Goto :End




REM --------------------------------------------------------------------------------

:GetAuthLength

REM Returns in environment variable content_len the length of the auth frame, based

REM on the username in %1, password in %2 + 467.

echo %1%2 > tempstr.txt

for %%a in (tempstr.txt) do set /a len=%%~za

set /a len -=3

set /a content_len = %len% + 467

if exist tempstr.txt del tempstr.txt

Goto :End



REM --------------------------------------------------------------------------------

:ServerNotResponding

call :WriteLog "ESXi Server did not accept connection attempt:"

call :WriteLog "Return code was %httpretcode%."

Goto :Cleanup





REM --------------------------------------------------------------------------------

:AuthenticationError

call :WriteLog "ESXi authentication did not succeed:"

call :WriteLog "Return code was %httpretcode%."

Goto :Cleanup





REM --------------------------------------------------------------------------------

:ShutdownError

call :WriteLog "ESXi shutdown request failed:"

call :WriteLog "Return code was %httpretcode%."

Goto :Cleanup





REM --------------------------------------------------------------------------------

:GetResponse

REM Stores the response code in return frame passed via a file specified as %1.

REM HTTP/1.1 response code (i.e. 200 for OK) is stored in %httpretcode%

set httpretcode=(no-response-from-server)

if exist response-tmp.txt del response-tmp.txt

find "HTTP/1.1" %1 > response-tmp.txt

for /f "skip=2 tokens=1,2* delims= " %%i in (response-tmp.txt) do set httpretcode=%%j

if exist response-tmp.txt del response-tmp.txt

Goto :End





REM --------------------------------------------------------------------------------

:CleanUp

call :WriteLog "---------------------------------------------------------------------------"

REM Clear up temporary text files...

if exist shutdown-host.txt del shutdown-host.txt

if exist open.txt del open.txt

if exist authenticate.txt del authenticate.txt

if exist shutdown.txt del shutdown.txt

if exist open-res.txt del open-res.txt

if exist shutdown-resp.txt del shutdown-resp.txt

if exist auth-key.txt del auth-key.txt

if exist auth-resp.txt del auth-resp.txt

if exist response-tmp.txt del response-tmp.txt

Goto :End





REM --------------------------------------------------------------------------------

:WriteLog

REM Appends the specified string to the log file and displays on screen.

echo %~1

echo %~1 >> shutdown.log

Goto :End


:End[/code]




Kurzer Teil der Fehlermeldung (Das Ende):

"call" kann syntaktisch an dieser Stelle nicht verarbeitet werden.



c:\scripts\esxi>if ==cant_resolve call :WriteLog "Could not resolve specified hostname "IP-Adresse"."





Das Script als Ausgabe in eine txt Datei (Der kurze Teil von oben ist [b]Fett[/b] markiert:

[code] c:\scripts\esxi>REM c:\scripts\esxi>REM *** ESXi Shutdown Script for use with free-licensed ESXi *** c:\scripts\esxi>REM c:\scripts\esxi>REM Primarily designed to shutdown an ESXi server under UPS management software c:\scripts\esxi>REM control, i.e. APC PowerChute. c:\scripts\esxi>REM c:\scripts\esxi>REM Performs a graceful ESXi shutdown - note that the server will ultimately c:\scripts\esxi>REM be powered off after VMs are suspended or shutdown, depending on their c:\scripts\esxi>REM configuration. As a result, when utility power is restored, the host c:\scripts\esxi>REM *will not restart* itself (unless the UPS battery was completely c:\scripts\esxi>REM depleted and the server BIOS was appropriately configured). c:\scripts\esxi>REM c:\scripts\esxi>REM c:\scripts\esxi>REM Written by James Pearce (vmware communities user ID J1mbo c:\scripts\esxi>REM (http://communities.vmware.com/people/J1mbo?view=profile). c:\scripts\esxi>REM c:\scripts\esxi>REM Version 4 (being for use with ESXi 4) c:\scripts\esxi>REM - tested against ESXi 4 U1 from a Windows 2003 VM c:\scripts\esxi>REM - completed 09-Dec-09 c:\scripts\esxi>REM c:\scripts\esxi>REM Usage: Shutdown.bat [hostname] [username] [password] c:\scripts\esxi>REM c:\scripts\esxi>REM [hostname] must be valid DNS name or an IP address c:\scripts\esxi>REM [username] is a user with shutdown rights, i.e. root c:\scripts\esxi>REM [password] is as used to connect to vSphere Client with the specificed c:\scripts\esxi>REM username. c:\scripts\esxi>REM c:\scripts\esxi>REM Usage is logged to shutdown.log, stored in the same directory as the script. c:\scripts\esxi>REM c:\scripts\esxi>REM Depends on supporting text files: c:\scripts\esxi>REM c:\scripts\esxi>REM - open-1.txt - Establishes the session, part 1. c:\scripts\esxi>REM - open-2.txt - Establishes the session, part 2. c:\scripts\esxi>REM c:\scripts\esxi>REM - auth-1.txt - Authentication request, part 1. c:\scripts\esxi>REM - auth-2.txt - Authentication request, part 2. c:\scripts\esxi>REM - auth-3.txt - Authentication request, part 3. c:\scripts\esxi>REM c:\scripts\esxi>REM - shutdown-1.txt - Shutdown request, part 1. c:\scripts\esxi>REM - shutdown-2.txt - Shutdown request, part 2. c:\scripts\esxi>REM - shutdown-3.txt - Shutdown request, part 3. c:\scripts\esxi>REM c:\scripts\esxi>REM c:\scripts\esxi>REM IMPORTANT: DEPENDS ON NMAP "NCAT" UTILITY BEING PRESENT AND ACCESSIBLE IN THE c:\scripts\esxi>REM DEFAULT SEARCH PATH. c:\scripts\esxi>REM c:\scripts\esxi>REM Free disrtibution and use permitted - but entirely at your own risk!! c:\scripts\esxi>REM c:\scripts\esxi>REM If you find it useful commerically, a bottle of ale would always be c:\scripts\esxi>REM welcome :) c:\scripts\esxi>REM c:\scripts\esxi>REM First ensure in correct directory as APC software might not. c:\scripts\esxi>REM c:\scripts\esxi>REM These commands change the drive and directory to where the batch file is stored, c:\scripts\esxi>REM for example if it's at C:\ESXI\SHUTDOWN.BAT these lines become "C:" then c:\scripts\esxi>REM "cd \esxi\" c:\scripts\esxi>c: c:\scripts\esxi>cd c:\scripts\esxi\ c:\scripts\esxi>REM Log the use of the script c:\scripts\esxi>call :WriteLog "System shutdown called: 19.09.2014 8:37:15,41" c:\scripts\esxi>REM Appends the specified string to the log file and displays on screen. c:\scripts\esxi>echo System shutdown called: 19.09.2014 8:37:15,41 System shutdown called: 19.09.2014 8:37:15,41 c:\scripts\esxi>echo System shutdown called: 19.09.2014 8:37:15,41 1>>shutdown.log c:\scripts\esxi>Goto :End c:\scripts\esxi>REM Check paramters were passed in: c:\scripts\esxi>If "IP-Adresse"a == a goto :ArgumentError c:\scripts\esxi>If roota == a goto :ArgumentError c:\scripts\esxi>If Key4Terrabit!a == a goto :ArgumentError c:\scripts\esxi>call :CheckHost "IP-Adresse" c:\scripts\esxi>REM Routine to determine if host is valid and online. Performs a Ping test. c:\scripts\esxi>REM Clear out any old files: c:\scripts\esxi>if exist test-connect.txt del test-connect.txt c:\scripts\esxi>if exist not-found.txt del not-found.txt c:\scripts\esxi>if exist found.txt del found.txt c:\scripts\esxi>if exist time-out.txt del time-out.txt c:\scripts\esxi>set hostconnectres= c:\scripts\esxi>REM Ping the host - first ping is desregarded as may time out due to arp delay: c:\scripts\esxi>ping "IP-Adresse" -n 1 1>NUL c:\scripts\esxi>ping "IP-Adresse" -n 1 1>test-connect.txt c:\scripts\esxi>REM Analyse the results and set environment variable hostconnectres: c:\scripts\esxi>find "could not find host" test-connect.txt 1>not-found.txt c:\scripts\esxi>find "Lost = 0" test-connect.txt 1>found.txt c:\scripts\esxi>find "Lost = 1" test-connect.txt 1>time-out.txt c:\scripts\esxi>for /F "skip=2 tokens=*" %i in (not-found.txt) do if not a%i == a set hostconnectres=cant_resolve c:\scripts\esxi>for /F "skip=2 tokens=*" %i in (found.txt) do if not a%i == a set hostconnectres=found c:\scripts\esxi>for /F "skip=2 tokens=*" %i in (time-out.txt) do if not a%i == a set hostconnectres=time-out c:\scripts\esxi>REM Clear-up temp files: c:\scripts\esxi>if exist test-connect.txt del test-connect.txt c:\scripts\esxi>if exist not-found.txt del not-found.txt c:\scripts\esxi>if exist found.txt del found.txt c:\scripts\esxi>if exist time-out.txt del time-out.txt c:\scripts\esxi>REM Write out results to the log: [B]"call" kann syntaktisch an dieser Stelle nicht verarbeitet werden. c:\scripts\esxi>if ==cant_resolve call :WriteLog "Could not resolve specified hostname "IP-Adresse"."[/B]

Ich wäre dankbar für Eure Hilfe!

Link zu diesem Kommentar
Auf anderen Seiten teilen

Hi,

in den Kommentaren auf deiner verlinkten Seite steht folgendes:

Nice post, works well! On a non english system, you have to adapt the lines:

REM Analyse the results and set environment variable hostconnectres:

find “could not find host” test-connect.txt > not-found.txt

find “Lost = 0″ test-connect.txt > found.txt

find “Lost = 1″ test-connect.txt > time-out.txt

In German it looks like:

find “nicht finden” test-connect.txt > not-found.txt

find “Verloren = 0″ test-connect.txt > found.txt

find “Verloren = 1″ test-connect.txt > time-out.txt

Und vermutlich wirst du das an einigen Stellen an deine Umgebung anpassen müssen. Davon abgesehen: Du solltest niemals Skripte einsetzen, die du nicht zumindest teilweise debuggen und/oder verstehen kannst. Wenn du nicht programmieren bzw. skripten kannst, solltest du dir das aneignen, denn auch als Admin kommst du ja offensichtlich nicht drumherum.

Bearbeitet von carstenj
Link zu diesem Kommentar
Auf anderen Seiten teilen

Dein Kommentar

Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung wiederherstellen

  Nur 75 Emojis sind erlaubt.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Editor leeren

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...