VMware Cloud Community
paulymo
Contributor
Contributor

Adding Scripts to Recovery Plan

Is there a way to add a script to the Recovery Plan that will run inside the VM being recovered? From what I have been reading, it appears adding a command to the recovery plan will run the script on the SRM server. I'm trying to get a script to run within the VM that will change the windows iSCSI initiator and rescan the disks.

Any thoughts?

0 Kudos
7 Replies
mal_michael
Commander
Commander

Yes, command that configure to run during Recovery plan , are executed on SRM server under credentials of the user that SRM service runs with.

I thought of two options:

1. You can use PowerCLI script to execute commands inside VM. There is cmdlet called invoke-vmscript that can run batch file, powershell script or exe file. It doesn't even require network connectivity, since it uses vmtools for execution. It doesn't work with all guest OSes.

2. You can use psexec to run your scripts remotely from the SRM server.

0 Kudos
paulymo
Contributor
Contributor

I've tried using the script a few different ways and they haven't worked. I'd like to try the Powershell script and was wondering if you had the syntax of the script? The SRM server at the recovery site is in a different domain then the recovered VMs. So I need the script to allow me to add credentials of a domain user.

0 Kudos
paulymo
Contributor
Contributor

Okay... I've made some progress on this and hoping someone on here can help me get over the last hump to finish it off. I have a PowerCLI script that is running a bat file. The bat file works on it own and has been tested. When I am testing the script on a VM that is test failed-over, it just hangs with no errors. PowerShell is installed on SRM and the VM being failed over, and PowerCLI is installed on the SRM server.

Is the script trying to be run over a network or directly through the host? The test VM is in the bubble network so it won't be able to communicate with vCenter or SRM directly.

Here is the PowerShell script

$srv = Connect-VIServer 10.1.2.3 -User domainuser -Password XXXXX

$script = '"%windir%\system32\cmd.exe" "%scripts%\iscsi"'

$vm = Get-VM MyVM

$HostUser = "root"

$HostPassword = "XXXXX"

$GuestUser = "VMdomain\User"

$GuestPassword = "XXXXXX"

$ScriptType = "bat"

Connect-VIServer 10.1.2.3 -Session $srv.SessionID

Invoke-VMScript -ScriptText $script -VM $vm -HostUser $HostUser -HostPassword $HostPassword -GuestUser $GuestUser -GuestPassword $GuestPassword -ScriptType $ScriptType

Here is the iSCSI script in the batch file:

@echo off

echo Adding target portals

iscsicli AddTargetPortal 10.4.3.2 3260 "Root\ISCSIPRT\0000_0" 1 * * * * * * * * * *

echo refreshing target portals

iscsicli RefreshTargetPortal 10.4.3.2 3260

echo Adding persistent targets logins

iscsicli PersistentLoginTarget iqn.1992-08.com.netapp:sn.xxxxxxxxx T 10.4.3.2 3260 "Root\ISCSIPRT\0000_0" 1 0x0 0x2 * * * * * * * 0 * 0

echo Login to Target

iscsicli logintarget iqn.1992-08.com.netapp:sn.xxxxxxx T * * Root\ISCSIPRT\0000_0 10.16.1.22 3260 * * * * * * * * * * 0

Any thoughts or suggestions?

0 Kudos
mal_michael
Commander
Commander

1. As I told, Invoke-VMScript does not require network connectivity. The script is run by vmtools and since is being executed with LocalSystem account.

2. If you only use this cmdlet to run a batch file inside VM, powershell is not required to be install in the VM.

3. What is the syntax of the command you add in the Recovery Plan to run the powershell script on SRM server?

4. Regarding the script:

what is "iscsi" in $script = '"%windir%\system32\cmd.exe" "%scripts%\iscsi"'. Name of the batch file or exe? Then file extension should be specified.

"Connect-VIServer 10.1.2.3 -Session $srv.SessionID" line should be removed since you connect to the vCenter in the first line of the script.

0 Kudos
paulymo
Contributor
Contributor

Here is the command syntax in SRM:

c:\windows\system32\cmd.exe /c c:\scripts\redirect.bat c:\scripts\powercli.ps1

The redirect script reads:

@echo off

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psc "D:\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& '%1'"

I can see in the SRM log that the powershell script is running but it times out. I have been running the powershell script directly on the SRM server while the test server is failed over. It connects and just hangs.

Thank you Michael for your comments so far... I made some of the changes you recommended to see if it will help.

0 Kudos
paulymo
Contributor
Contributor

Here is the error in the log:

dynamicType = ,

faultCause = (vmodl.MethodFault) null,

commandLine = "c:\windows\system32\cmd.exe /c c:\scripts\redirect.bat c:\scripts\powercli.ps1",

output = "WARNING: There were one or more problems with the server certificate:

* The X509 chain could not be built up to the root certificate.

* The certificate's CN name does not match the passed value.

Name Port User

-


-


-


10.2.3.4 443

",

timeOutInseconds = 300,

msg = "",

}

0 Kudos
mal_michael
Commander
Commander

What is the OS version of your VMs?

Is vmtools installed, up-to-date and running?

Are your able to run some simple script or command through invoke-vmscript?

0 Kudos