In this previous article I showed you about in-guest scripting in VAO. While normal PowerShell scripts work fine, the article showed how you could get more info into the UI and reports. In this article we are going to look at how we can pull variables into the script and use them. I had some trouble doing this - not in the docs yet - so I thought I should write it down.
Here is the situation. We have a script and we want to add to it variables from VAO. So in the script there can be code that uses those variables. In this article we will see how to do that. This article is a bit long but I want to make sure that things are clear, and useful.
First, lets look at my script. It has had some changes in it to help.
########### #Michael White - 2/26/18 # # # script to create a file and put the date in it. Testing from within VAO. # Added some logic to add info to VAO logs. Trying to use parameters from VAO. # # v1.13 Param( [Parameter(Mandatory=$true)] [string]$planName, [string]$planState ) try { $A = Get-Date; Add-Content c:\utils\vao_log.txt "$A - test recording date. " Add-Content c:\utils\vao_test.txt "Plan name is $planName and state is $planState" Write-Host "file written and updated with date / time" Write-Host "testing with parameters - planName, planState" } Catch { Write-Error " something did not work - error" Write-Error $_.Exception.Message Write-Warning "Something sort of did not work - small error" Write-Warning $_.Exception.Message }
As we learned in a previous article, I have the two Write-Error lines (lines 24,25) to help get more info into the VAO UI (logs or reports too) if problems occur. I have the two Write-Host lines (lines 20,21) to put some additional info into the UI, reports and logs. The two Write-Warning lines are simular to the Write-Error but will provide info on warnings instead.
What is new is in lines 10 through 14. I am pulling from VAO, during a test or failover, the name of the failover plan, and the state of the plan. I am outputting that info to a log file - vao_test.txt but those variables could be used in other ways. Note the comma (line 12)? Very important, and exciting errors if you don’t have it. Misleading and confusing error message. Also, make sure the variable names (line 12,13) match the field names in VAO.
There is quite a number of different fields that can be passed to a script - not just these too. And they are listed in the docs. We are using Name and State but the process in here will work for others.
So lets see how to connect things. Lets log into VAO, and change to the Configuration page. Now select the Plan Steps tab.
Lets use the green Add button to add our script.
You can see above I have named the VM step that is my script. And selected the script too.
In the next step I leave the Make this step visible to all sites option selected. Once the script is uploaded you should select it by clicking on it.
We are going to use the other green Add button to add in what we need to do to link the variables so they can be consumed in the script.
Now in the Name field, you need to be very precise. This is the name that would be used in your script. So in my case planName is what I will use. Then use the blue … button. You will see a new popup.
We can now double-click on any variables we see. There is a list and you can scroll. We will select %plan_name% as that is what we are working on.
It is important to note you can have multiple variables selected here, and even add text if you want. You are filling in the default value of the variable that ware are creating with the name - in my case of planName.
Once you have what need done, use the Save button.
I need to add a planState so will do that now. Now we need to configure our choices. So lets click on the first option - Common parameters.
It is important to note that these default values may not be appropriate for you. For example, I want this script to be execute during a test failover. How else will I test it? In addition, I like to change the Retries to 2 rather than 10.
Now we change to Execute Location and make sure it shows what we need - Guest OS. The alternative is the Veeam Backup server.
Next we need to select the credentials to be used. Remember the credentials will be used to connect to the VM so we can execute the script. The credentials are added in the Configuration area and we cannot see the password in this area which is very good.
So that is all the config that is necessary for our script. Notice how all the names are bold and slightly italicized. We need to hit the Save button to save our changes.
When you hit save you see an interesting and powerful screen.
What this screen is asking is if you want those default values you changed to be pushed out to all of the other occurrences of the script. This script was just added so it doesn’t matter, but if you added the script yesterday and you tweaked the default values today, this is very nice and very powerful since you could easily update all the occurrences of this script and maybe it was used in a 100 plans - so a big timesaver. Once saved our names will look different.
Now we have this script added, and configured, we can add it to a plan. So I leave the configuration area, and go edit a plan.
We use the Add button and select the script we added.
We could click on it, and actually change any of those default values we changed. But no need to. Time to test!
So save your plan and lets make sure the Readiness Check is good. We are prompted for that when we Save.
We check the Failover Plans area to see if the Readiness check passed. I love that check as it is simple and fast, checks a lot of things but is not turning anything on, or making any changes. So it is very fast!
Lets fire off a test and see how it goes! Make sure to leave the plan VMs running when the test is complete so we can make sure our files are written.
Remember when you are watching a test failover, you can click on the VM you attached the script step too, then click on the step itself to see additional information.
Once the test failover is finished, and our step is done. We see if things have worked or not. We can see the script worked.
That really only shows the script worked. We need to connect to the console of the VM and check the files. We can see the files were created.
And when we open vao_test.txt we see if things really worked or not!
And it did! We can see the plan name, and state too.
So you have learned how to pull variables from VAO - like plan name, plan state, or any of the many others and have them used in a script.
I really hope that this helps, and of course, as always, let me know if you have questions.
Updates:
- 10/3/18 - added to the code sample a write warning example.
- 2/26/18 - the script as it was originally above would work with Win2K8R2, and Win2k12 but not with Windows 10. I have changed the script above so that it works on Win2K8R2, Win2K12 and Win10. The issue was the space between Parameter and (Mandatory.. and removing the space fixed the issue. What you see above is what works.
- 2/26/18 - This works fine with the GA build.
- 2/26/18 - You need to have PowerShell 3.0 or later (and .Net Framework 4.0) inside the VM that is going to execute the script. If you don’t you will see a very specific error - “Cannot find the type for custom attribute ‘Parameter’. Make sure that the assembly that contains this type is loaded”.
Michael
=== END ===