Including a runbook.ps1 script in your runbook - powershell

Scenario:
I have 2 runbooks, runbook A and runbook B.
Inside runbook B, I want to load runbook A into memory.
Running below doesn't work, can someone help me out please?
.\runbookb.ps1
. ./runbookb.ps1

Assuming that you want to invoke runbook B from runbook A, first of all please make sure runbook B exists in your Automation Account and is published. Then, depending on the runbook type, you need to use different syntax:
For PowerShell runbooks, the correct syntax is: ./runbookb.ps1 or . ./runbookb.ps1 (depending on whether you want the "dot-sourcing" behavior or not)
For PowerShell Workflow runbooks, the correct syntax is: runbookb
If you really want to load the runbook content into memory, this is different:
For PowerShell runbooks: Get-Content ./runbookb.ps1
For PowerShell Workflow runbooks: use the Export-AzureRmAutomationRunbook cmdlet.

Related

Azure Automation - how to split out common functions

I'm wondering if there's a way I can define common functions in a separate runbook in Azure Automation? For instance, I've got a logging function that timestamps the messages and exits on errors that I use in multiple runbooks. I'd like to define it once, and then call it from the other runbooks. And if I change it in the future, I only have to change it in one place.
I know I can define parent / child runbooks and call them inline, which led me to wonder if I could split out, for instance, a function definition and then call that runbook from another runbook to "import" the function into the current runbook. So for instance, I have a runbook called "Test-FunctionDefinition" with the following code:
function Test-FunctionDefintion() {
param (
[String] $TestParam
)
Write-Output "Output from test function: $TestParam"
}
I'd like to be able to call it inline from another runbook like this to define the function, and then be able to use that function:
& .\Test-FunctionDefinition.ps1
Test-FunctionDefinition -TestParam "Test String"
I tried creating the two runbooks, but while it appears to call the runbook "Test-FunctionDefiniton" fine on line 1, subsequently calling the function on line 3 fails with:
Test-FunctionDefinition : The term 'Test-FunctionDefinition' is not recognized as the name of a cmdlet, function, script file, or operable program.
Is what I'm trying to do possible? I realize I could just modify my runbook and call & .\Test-FunctionDefinition.ps1 -TestParam "Test String", but would prefer to do it the other way if possible.
It looks like it should be possible - Create modular runbooks in Automation
You have twop options:
Inline - Child runbooks run in the same job as the parent.
Cmdlet - A separate job is created for the child runbook.
For powershell runbook it should be easy as this
$vm = Get-AzVM -ResourceGroupName "LabRG" -Name "MyVM"
$output = .\PS-ChildRunbook.ps1 -VM $vm -RepeatCount 2 -Restart $true
I did some more testing to make sure my original assumption (defining a function in one script and calling it in another) worked as I expected, and I got the same error. It appears that you need to dot-source rather than use the &. This worked both in a PowerShell console and Azure Automation:
. .\Test-FunctionDefinition.ps1
Test-FunctionDefinition -TestParam "Test String"
Note the starting '.' instead of '&'. I'll need to look up what the difference is between those...

How to run PowerShell from Azure Data Factory

I have PowerShell script which splits a complex CSV file to a smaller CSV file for every 1000 records. Here is the code:
$i=0;Get-Content C:\Users\dell\Desktop\Powershell\Input\bigsizeFile.csv -ReadCount 1000 | %{$i++; $_ | Out-File C:\Users\dell\Desktop\Powershell\Output\file$i.csv
}
Now I want to use this script in Azure PowerShell and I want to run this from Azure Data Factory. Can Someone please help with this.
I was able to run a PowerShell script by loading the script as an application in the Batch account associated to the Batch Custom Activity. From there it was figuring out the correct syntactical sugar to use the environment variables to get the path to my script and run it. I did this like so:
powershell powershell -command (\"(Get-ChildItem Env:AZ_BATCH_APP_PACKAGE_powershellscripts#1.0).Value\" + '\\Powershell\\processFromAzure.ps1
If you just want to call from the task dir, this should work:
powershell powershell -command ("$env:AZ_BATCH_TASK_DIR" + '\wd\processFromAzure.ps1')
Hope this helps someone else out there!
you could execute your powershell command by using Custom activity in ADFv2. Here is an example.

How to execute PowerShell script as Azure Automation Runbook from InlineScript inside PSWorkflow runbook?

In a PowerShell Workflow activity, I can call a native PowerShell script using InlineScript:
workflow test
{
InlineScript
{
.\script.ps1
}
}
But in Azure Automation, the dot-path (at least in my tests) was returning c:\windows\system32, and the script-as-runbook in Azure Automation did not exist there (or rather, it failed to execute because it could not find the script).
Is it possible to execute a native PS runbook stored in AAuto like this?
If so, how do I specify the path to the file?
Is this a bug/oversight in Azure Automation's parsing/compilation process of Workflow runbooks & InlineScript activities, preventing the dependent runbook from being copied to the worker?
I did a little hunting, and found that when native PS runbooks are executed:
They are first inspected for any other runbook references.
As part of the deployment to the worker for execution, a randomly-named folder is created under C:\Temp\
Referenced runbooks are eventually copied to this folder.
If runbooks are NOT found to be referenced, they are NOT copied to the temp directory.
The root runbook does not appear to be copied to the folder.
The dynamically-named folder is NOT created (under c:\Temp) when executing a Workflow runbook.
As part of the standard Workflow compilation, InlineScript activities have their contents copied to the autogenerated xaml. I'm uncertain about a linked file, though based on behavior that looks to be a runtime concern. My guess is that compilation happens each time a workflow is executed (hence the delayed start), and takes place on the worker, utilizing the standard PS workflow compilation just like local would.
I cannot (easily) convert this script to a workflow, and it is used from within other workflow activities. Right now the only way I can make this 'work' is to copy & paste the script into the first InlineScript within a workflow that requires it, which is obviously tedious & annoying from a maintenance perspective.
Presumably, as a workaround, I could use a Hybrid Worker, but that comes with a host of other issues, like ensuring the child runbooks are published there & having to maintain them separately, or AAuto not automatically pushing custom modules from the Automation Account to the worker (though this is planned), etc.
Please see https://azure.microsoft.com/en-us/blog/announcing-powershell-script-support-azure-automation-2/:
Right now, you can only invoke inline PowerShell runbooks from PowerShell runbooks, and PowerShell Workflow or Graph runbooks from PowerShell Workflow or Graph runbooks. This may change in the future.
It hasn't changed yet :)

Assign variable in PowerShell script during TFS build process

I am trying to execute PowerShell script during TFS build process. I added new activity "RunScript" into TFS Build workflow and new arguments. That script works with arguments.
Now I am trying to use returned value in another activity. Actually I would like to assign some global variable with that value in PowerShell script and pass to another process.
Any help?
Output the value that you want to return with Write-Host then in your Standard Output of the InvokeProcess activity you'll get the value.

Azure Runbook - Create from gallery using a Powershell script

I was trying to create a RunBook in Azure Automation and I chose to use a Powershell script (Cmdlet). But the creation failed with an error because azure couldn't directly convert it and a manual edit is required to make it a "workflow". I am not a pro on PS and I understand you need to know how a workflow works. But my immediate goal is to get the Runbook working and hence trying to figure out an easy way to convert a PS script to a PS workflow for the purpose of runbook... Appreciate any help
Please see this blog post for info on PowerShell script to PowerShell Workflow conversion: http://azure.microsoft.com/blog/2014/11/25/introducing-the-azure-automation-script-converter/
However, regardless of conversion, you can't interact with local files on your computer from Azure Automation, so this script won't work. See https://social.msdn.microsoft.com/Forums/en-US/03a7aace-4e3e-4dab-9a8f-4ed1002fde4e/how-to-upload-files-from-users-machine-to-blob-storage-using-azure-automation?forum=azureautomation for more details.