Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Anchor
TOC
TOC
Include Page
KBCOMMON:KB-CSSstyle
KBCOMMON:KB-CSSstyle

...

Using the "Local Group Policy Editor", it is possible to add the execution of a Powershell script at Windows shutdown. As this operation is performed with the "system" account, some operations have to be performed if depending on the chosen mode is "rsh" or "ssh"method chosen (opa0, rsh or ssh).

Warning

Please note shutdown scripts are executed when the shutdown is executed either from the "Windows Start" menu or using the shutdown command line. They are not always executed when clicking on the "Restart" button from the Windows Update tool.

opa0 mode preparation

If the combination of username/password is used, there is no need for configuration change.

If an encrypted file is used to store the password using the "cryptedpsys" parameter, it must be created on a session as "system" account.

...

C:\WINDOWS\system32>whoami
nt authority\system

Run the following command to create the encrypted file:

C:\

WINDOWS

Users\

system32>

Spock> powershell -

command 

command "ConvertTo-SecureString -String

"

'<password>

"

' -AsPlainText -Force ^
More? | ConvertFrom-SecureString | Out-File

"

'<full path to the file>'"

Example:

C:\Users\Spock> powershell -command "ConvertTo-SecureString -String '12345' -AsPlainText -Force ^
More? | ConvertFrom-SecureString | Out-File 'c:\charon\msds20vmspwd.txt'"


Div
classpagebreak


Run the Powershell script in check mode:

C:\WINDOWS\system32>powershell -file c:\charon\charon_cleanshutdown.ps1 -config c:\charon\myds20vms.ini -check

then check the "OPA0 was successful" message is displayed followed by "Command successfully completed."

...

C:\WINDOWS\system32>whoami
nt authority\system

Run the Powershell script in check mode:

C:\WINDOWS\system32>powershell -file c:\charon\charon_cleanshutdown.ps1 -config c:\charon\myds20vms.ini -check

then check the "RSH was successful" message is displayed followed by "Command successfully completed."

Note

It is recommended to move the psexec.exe program file to a secured folder or to remove it when it is no more needed (check completed)


Div
classpagebreak


ssh mode preparation

Execute the same operations as described in chapter "Using ssh" for Tru64 or VMS to create a key pair with "SYSTEM" user with a different identity file.

...

C:\WINDOWS\system32>whoami
nt authority\system

Create a new ssh trust as described in the "Using ssh" chapter.

Tip

Remember to specify a different identity file in the .ini file. This can be done thanks to the "identityfsys" parameter (see: Using ssh)

...

C:\WINDOWS\system32>powershell -file c:\charon\charon_cleanshutdown.ps1 -config c:\charon\myds20vms.ini -check

then check the "SSH was successful" message is displayed followed by "Command successfully completed."

Note

It is recommended to move the psexec.exe program file to a secured folder or to remove it when it is no more needed (check completed)


Div
classpagebreak


Windows settings - Local group policy

...

(info) The PowerShell window is displayed during Windows shutdown thanks to the enabled "Display instructions in shutdown scripts as they run" feature and the putty / OPA0 window is opened thanks to the "openconsolecmd" and "openconsolearg" parameters in the .ini file.

Full log as defined in the .ini file:

Code Block
languagetext
20200326:154759:INFO :0: Using 'C:\Charon\myds20vms_shutdown.log' as log file / append
=
Parameters taken from configuration file:
commandparams=-o Ciphers=+3des-cbc -o KexAlgorithms=+diffie-hellman-group1-sha1 -o HostKeyAlgorithms=+ssh-dss
guestsystem=192.168.152.149
identityfile=C:\Charon\win10bmrsa
identityfsys=C:\Charon\win10bmrsa_system
logfile=C:\Charon\myds20vms_shutdown.log
mode=ssh
openconsolearg=-load OPA0 -P 10007
openconsolecmd=C:\Program Files\CHARON\Build_20203\x64\putty
os=VMS
servicename=ds20vms
sshbin=C:\Program Files (x86)\OpenSSH\ssh.exe
username=system
waitbeforestop=10
=
20200326:154759:INFO :0: 'ssh' will be used
20200326:154759:INFO :0: Using 'C:\Charon\win10bmrsa_system' as identity file.
20200326:154759:INFO :0: Service 'ds20vms' is Running (Display name: ds20vms)
20200326:154759:INFO :0: Testing guest system '192.168.152.149' response
20200326:154802:INFO :0: Opening console.
20200326:154802:INFO :0: Invoking 'ssh' command and executing shutdown as WIN10-MAIN$ ...
20200326:154802:INFO :0: C:\Program Files (x86)\OpenSSH\ssh.exe -i C:\Charon\win10bmrsa_system -q -l system -o BatchMode=yes -o Ciphers=+3des-cbc -o KexAlgorithms=+diffie-hellman-group1-sha1 -o HostKeyAlgorithms=+ssh-dss 192.168.152.149 '@SYS$MANAGER:CHARON_SHUTDOWN.COM'
20200326:154804:INFO :0: Output results:
20200326:154804:INFO :0: 
20200326:154804:INFO :0: $     PURGE /KEEP=20 SYS$MANAGER:CHARON_SHUTDOWN.LOG
20200326:154804:INFO :0: $     RUN /DETACH SYS$SYSTEM:LOGINOUT.EXE /INPUT=SYS$MANAGER:CHARON_SHUTDOWN -
20200326:154804:INFO :0:           /OUTPUT=SYS$MANAGER:CHARON_SHUTDOWN.LOG /UIC=[1,4]
20200326:154804:INFO :0: %RUN-S-PROC_ID, identification of created process is 00000129
20200326:154804:INFO :0: $   ENDIF
20200326:154804:INFO :0: $ ENDIF
20200326:154804:INFO :0: $ EXIT
20200326:154804:INFO :0: $
20200326:154804:INFO :0: $ !
20200326:154804:INFO :0: $ ! Force any output to the standard output device.
20200326:154804:INFO :0: $ ! Most useful when client is Un*x.
20200326:154804:INFO :0: $ !
20200326:154804:INFO :0: $ ! V5.4-03
20200326:154804:INFO :0: $ !    WRITE SYS$OUTPUT -
20200326:154804:INFO :0: $ !     "ssh-rcmd  'f$getjpi("","USERNAME")' logged out at 'f$time()'" ! V5.4-02
20200326:154804:INFO :0:  
20200326:154804:INFO :0: $    WRITE SYS$OUTPUT "" 
20200326:154804:INFO :0: 
20200326:154804:INFO :0: $ 
20200326:154804:INFO :0: $    IF (SSHD$ERROR .NES. SSHD$INPUT_OUTPUT) 
20200326:154804:INFO :0: $    ENDIF
20200326:154804:INFO :0: $
20200326:154804:INFO :0: $ ! SS_NORMAL, SSH was succcessful, command should send its error over net.
20200326:154804:INFO :0: $    EXIT 1 
20200326:154804:INFO :0: Checking command results...
20200326:154804:INFO :0: Command successfully completed.
20200326:154806:INFO :0: Testing connection to '192.168.152.149' = True
20200326:154829:INFO :0: Testing connection to '192.168.152.149' = False
20200326:154839:INFO :0: Sleeping for 10 seconds...
20200326:154849:INFO :0: Stopping service ds20vms
20200326:154850:INFO :0: Service is Stopped
20200326:154850:INFO :0: Done.

...

classpagebreak

OPA0 console log (needs to be defined in the Charon configuration file):

...

languagetext

...

Tip

It is recommended to check the log files once the Windows server has rebooted:

  • the shutdown log file from this script and
  • the OPA0 log file defined in the Charon configuration file (if not set, it is recommended to define it)

Include Page
KBCOMMON:KB-GoToTop
KBCOMMON:KB-GoToTop