Get Source Code for referenced functions in Azure Runbook

336 views Asked by At

I'm new to powershell and Azure automation. Currently I've an Azure Automation Account and it has few Runbook jobs. I'm trying to add new logic to an existing Azure Runbook job by updating its powershell script. I see there are some functions but unfortunately we didn't maintain the source code :(. As the runbook is currently running without any issues, i want to know how to get the source code of the referenced functions.

I searched in the modules, modules gallery, python 2 packages, etc in the Automation Account used by this runbook as well as under Assets, cmdlets, and runbooks nodes (that you see in the Edit mode of the script in Portal) but couldn't find where these functions are referenced. I see one module which I suspect to have something related but not sure.

As an FYI, the functions are named like this: GetClassicConnection, GetRunAsConnection, Set-Subscription $subcriptName

So here are my questions:

  1. Is there are way to get the source code of all the referenced functions within this runbook powershell script? Something like disassembling a .NET dll using disassembler tools.

  2. How to see the source code of an existing module in Automation Account that has its Status as "Available" under Modules section.

1

There are 1 answers

1
postanote On

I have not had teh reason, yet, to use Azure Runbooks, however, PowerShellCore is already open source and can be viewed on GitHub.

That being said, you can get source code from local cmdlets for example this way...

Param
(
    [string]$CmdletName = (Get-Command -CommandType Cmdlet | 
    Out-GridView -Passthru)
)

# Get the DLL is it is a compiled cmdlet
'Getting DLL if the entered cmdlet name is a compiled cmdlet'
(Get-Command $CmdletName).DLL 

'Getting cmdlet details / source code'
$metadata = New-Object system.management.automation.commandmetadata (Get-Command $CmdletName)
[System.management.automation.proxycommand]::Create($MetaData)

Note: Even with the above I've had issues with some cmdlets erroring out.

You can get source code from local functions for example this way...

Param
(
    [string]$FunctionName = (Get-Command -CommandType Function | 
    Out-GridView -Passthru)
)

(Get-Command -Name $FunctionName).ScriptBlock 

For dll, one could use the same approach for looking at any other .Net dlls, would be the same tools, ILSpy or dotNetPeek and the like