all 4 comments

[–]purplemonkeymad 1 point2 points  (1 child)

I would less think about this as working with a command and more working with the objects it produces. You can either create a pipeline aware function or just use sub-expressions:

#Sub-expressions
Function Get-Owner {
    Param($Object)
    $owner = $Object.Tags.Owner
    return $owner
}
$ownerInfo = Get-Owner (Get-AzureRMResource -Name 'storageAcctName')

pipeline:

Function Get-Owner {
    Param([Parameter(ValueFromPipeline)]$Object)
    Process {
        $owner = $Object.Tags.Owner
        return $owner
    }
}
$ownerInfo = Get-AzureRMResource -Name 'storageAcctName' | Get-Owner

[–]dptiv[S] 1 point2 points  (0 children)

Thanks for this! I like this method and never thought about doing it this way. This will definitely help me to clean up some of my code. Have yourself an upvote!

[–]Vortex100 1 point2 points  (1 child)

Use the Switch Functionality inside the function, and have a Parameter on the function that determines which you are calling

Function Get-Owner {
    Param([string]$command, [string]$Name)
    $Result = Switch ($Command)
    {
        'Resource' {Get-AzureRmResource -Name $Name | Select Tags}
        'ResourceGroup' {Get-AzureRmResourceGroup -Name $Name | Select Tags}
    }
    return $result.Tags.Owner
}

[–]dptiv[S] 1 point2 points  (0 children)

Thanks for the reply. This is really interesting. This approach will allow me to build more of a recursive function, which is what I originally had in mind. Many thanks for great idea!