1

I have built a few Powershell functions using Azure Functions, and it is working like a charm.

Now that I have proven the concept I would very much like to refactor my existing functions.

First of all I would like to move the authentication required in my function to some kind of shared function or whatever.

Here is my example function, which return a list of all web apps in my resource group.

# Authenticate with subscription
$subscriptionId = "<SubscriptionId>"
$resourceGroupName = "<ResourceGroupName>";
$tenantId = "<TenantId>"
$applicationId = "<ApplicationId>"
$password = "<Password>"
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $userPassword
Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionId $subscriptionId -Credential $userCredential
Get-AzureRmSubscription –SubscriptionId $subscriptionId | Select-AzureRmSubscription

# Get all web apps
$Websites = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName
$Websites = $Websites | select name | ConvertTo-Json -Compress

# Write output
Out-File -Encoding Ascii -FilePath $res -inputObject $Websites

I would very much like to move everything from line 1 to line 10 somewhere else. Is it possible? If yes, can anyone please point me in the right direction here?


Update

Thanks to both Walter and Pragna I combined the two methods like this.

run.ps1

# Authenticate with subscription
Import-Module 'D:\home\site\wwwroot\bin\Authentication.ps1'

# Get all web apps
$Websites = Get-AzureRmWebApp -ResourceGroupName $env:ResourceGroupName
$Websites = $Websites | select name | ConvertTo-Json -Compress

# Write output
Out-File -Encoding Ascii -FilePath $res -inputObject $Websites

Authentication.ps1

$secpasswd = ConvertTo-SecureString $env:Password -AsPlainText -Force;
$userCredential = New-Object System.Management.Automation.PSCredential ($env:ApplicationId, $secpasswd)
Add-AzureRmAccount -TenantId $env:TenantId -ServicePrincipal -SubscriptionId $env:SubscriptionId -Credential $userCredential
Get-AzureRmSubscription –SubscriptionId $env:SubscriptionId | Select-AzureRmSubscription
1
  • probably precompiled functions might help you, never tried that with powershell Commented Jun 14, 2017 at 17:56

2 Answers 2

1

It is unsafe for you to save your account information in script. I suggest you could store these to App Setting. You could find it Your function app-->Settings-->Application settings-->Manage application settings-->App settings and key-value pairs for the settings SP_USERNAME, SP_PASSWORD, and TENANTID, SubscriptionId(You also could use other values or more key pairs). enter image description here

Modify your script as below:

# Set Service Principal credentials
    # SP_PASSWORD, SP_USERNAME, TENANTID are app settings
    $secpasswd = ConvertTo-SecureString $env:SP_PASSWORD -AsPlainText -Force;
    $mycreds = New-Object System.Management.Automation.PSCredential ($env:SP_USERNAME, $secpasswd)
    Add-AzureRmAccount -ServicePrincipal -Tenant $env:TENANTID -Credential $mycreds;
    Get-AzureRmSubscription –SubscriptionId $env:subscriptionId | Select-AzureRmSubscription

When you want to modify your account information, you don't need modify your script, you only need modify app setting. You could modify app setting by using Azure CLI.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! It works, I was looking for something like this, thanks :)
1

Yes. You can import custom powershell modules.

  • Create a shared directory e.g. bin under D:\home\site\wwwroot
  • Copy module to the shared directory
  • Call Import-Module SharedDir\MyModule.psm1 or SharedDir\MyScript.ps1 or SharedDir\MyModule.psd1 or SharedDir\MyLib.dll

Also, here is a sample that might help.

1 Comment

Thanks! I combined your answer with Walters :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.