-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't pipe Azcontext object in azure function environment to output/log #306
Comments
You need to use Push-Outputbinding in Azure Functions, not return. Return
and anything to the output stream basically just ends up as logs. Read up
on it :)
…On Wed, Aug 7, 2019, 1:55 PM Yanbing ***@***.***> wrote:
I have below azure ps function. The object returned to caller is $null.
if run the script not in ps function environment, the caller can receive
the object.
using namespace System.Net
using namespace Microsoft.Azure.Functions.PowerShellWorker
param($Request, $TriggerMetadata)
Write-Host "PowerShell http trigger function processed work item"
function Install-Certificate
{
param(
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
[string]$StorePath
)
Write-Host "Install-Certificate"
$store = Get-Item $storePath -ErrorAction Stop
try
{
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
}
catch
{
throw "Failed to open cert store {0} -- {1}" -f $storePath,$a
}
try
{
$store.add($Certificate)
}
catch
{
throw "Failed to add certificate to cert store {0} -- {1}" -f $storePath,$a
}
$store.close()
Write-Host "End of Install-Certificate"
}
function Connect-CustomerTenant1
{
param(
[Parameter(Mandatory=$true)][string]$SubscriptionId,
[Parameter(Mandatory=$true)][string]$SPNId,
[Parameter(Mandatory=$true)][string]$KeyVaultName,
[string]$TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47",
[string]$AzureEnvironmentName = "AzureCloud"
)
$cnt = 0
$MaximumRetryCount = 3
Write-Host "Get-AzKeyVaultCertificate -Name $SPNId -VaultName $KeyVaultName"
<#$appSecret = RetrieveSecretFromKV -SecretName $SPNId -KeyVaultName $KeyVaultName
Write-Host $appSecret
$kvSecretBytes = [System.Convert]::FromBase64String($appSecret)
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvSecretBytes, $null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet)
#>
<#$kvCertificate = Get-CertificateFromKV -KeyVaultName $KeyVaultName -CertificateName $SPNId
Write-Host $kvCertificate
$kvCertBytes = [System.Convert]::FromBase64String($kvCertificate)
#$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvCertBytes, $null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet)
#>
#Write-Output $certificate
<#$ctx = Connect-AzAccount -Identity -Force
$kvCert = Get-AzKeyVaultCertificate -VaultName $KeyVaultName -Name $SPNId -DefaultProfile $ctx
$certificate = $kvCert.Certificate#>
#$ctx = Connect-AzAccount -Identity -Force
$kvSecret = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name $SPNId
$kvSecretBytes = [System.Convert]::FromBase64String($kvSecret.SecretValueText)
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($kvSecretBytes, $null, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet)
$thumbprint = $certificate.Thumbprint
Write-Host "Thumbprint: $thumbprint"
Install-Certificate -Certificate $certificate -StorePath "Cert:\CurrentUser\My"
do {
$cnt++
$exceptionMessage = $null
try {
Write-Host "Connect-AzAccount..."
$Script:AzContext = Connect-AzAccount -CertificateThumbprint $thumbprint `
-ApplicationId $SPNID -Tenant $TenantID `
-ServicePrincipal -Environment $AzureEnvironmentName `
-SubscriptionId $SubscriptionId -ErrorAction Stop
Write-Host "After Connect-AzAccount ..."
# validate due to bug in az module (bug link: Azure/azure-powershell#9448)
$rglist = Get-AzResourceGroup -DefaultProfile $Script:AzContext
Write-Host "Get $($rglist.Count) resource groups"
Write-Host "After validation ..."
Write-Host "Execution count: $cnt"
Write-Host "context: $($Script:AzContext | Out-String)"
#return
# bug: the axContext does not return to caller
return $Script:AzContext
}
catch
{
Write-Host "AzContext in exception: $(Get-AzContext | Out-String)"
Write-Host "Connect-AzAccount failed, retry count: $cnt"
$exceptionMessage = $_.Exception.Message
Write-Host $exceptionMessage
if($_.Exception.InnerException)
{
Write-Host $_.Exception.InnerException.Message
#$exceptionMessage += $_.Exception.InnerException.Message
}
}
} while ($cnt -lt $MaximumRetryCount)
# Throw an error after $MaximumRetryCount unsuccessful invocations. Doesn't need
# a condition, since the function returns upon successful invocation.
Write-Host "connect-Azacccount failed. retry count: $cnt"
throw "Login validation failed. $exceptionMessage"
}
try
{
$cxt = Connect-CustomerTenant1 -SubscriptionId "e9c08608-0c4f-4a51-8640-968eafc186af" `
-TenantId "72f988bf-86f1-41af-91ab-2d7cd011db47" `
-SPNID "70402f7e-6079-44ef-865f-55315dc7ce19" `
-KeyVaultName "amvmyawang-kv"
if($cxt)
{
$body = $cxt | out-string
Write-Output "returned cxt in caller:"
Write-Output $cxt
}
else {
Write-Host "cxt is null in caller"
}
}
catch
{
Write-Host $_.Exception.Message
}
output in ps azure function:
2019-08-07T20:45:54.688 [Information] Executing 'Functions.bugrepro'
(Reason='This function was programmatically called via the host APIs.',
Id=06a022c9-5b62-47c5-8dca-ea1d53adf0c6)
2019-08-07T20:46:05.237 [Information] INFORMATION: PowerShell http trigger
function processed work item
2019-08-07T20:46:05.238 [Information] INFORMATION:
Get-AzKeyVaultCertificate -Name 70402f7e-6079-44ef-865f-55315dc7ce19
-VaultName amvmyawang-kv
2019-08-07T20:46:10.858 [Information] INFORMATION: Thumbprint:
E03A6AD85BEF21B872C1075EF3B6E5749904F232
2019-08-07T20:46:10.858 [Information] INFORMATION: Install-Certificate
2019-08-07T20:46:10.932 [Information] INFORMATION: End of
Install-Certificate
2019-08-07T20:46:10.933 [Information] INFORMATION: Connect-AzAccount...
2019-08-07T20:46:12.019 [Information] INFORMATION: After Connect-AzAccount
...
2019-08-07T20:46:17.049 [Information] INFORMATION: Get 3 resource groups
2019-08-07T20:46:17.050 [Information] INFORMATION: After validation ...
2019-08-07T20:46:17.051 [Information] INFORMATION: Execution count: 1
2019-08-07T20:46:17.051 [Information] INFORMATION: context:
Account SubscriptionName TenantId Environment
------------------------------
70402f7e-6079-44ef-865f-55315dc7ce19 TestEA Customer3
72f988bf-86f1-41af-91ab-2d7cd011db47 AzureCloud
2019-08-07T20:46:17.051 [Information] OUTPUT: returned cxt in caller:
2019-08-07T20:46:17.051 [Information] OUTPUT:
2019-08-07T20:46:18.765 [Information] Executed 'Functions.bugrepro'
(Succeeded, Id=06a022c9-5b62-47c5-8dca-ea1d53adf0c6)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#306?email_source=notifications&email_token=ADUNKUQ4BOYHUF7O3RJXD6TQDMZCLA5CNFSM4IKEF7WKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HD7YPJQ>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADUNKUX4IWVBBEZX3OY2HDLQDMZCLANCNFSM4IKEF7WA>
.
|
@JustinGrote, log is what I want. the issue is the object can't pipe to log. I pipe it to log for troubleshooting purpose, It always output empty, which cost me long time to figure out this object is not null, but just not able to pipe to log |
Did you try piping it to format-list -prop * | out-string? It may have a default tostring of null or something. Maybe also try.gettype() on it |
@JustinGrote , if in the my code, here is find out:
Why does it like this? |
I have below azure ps function. Looks like the object returned to caller, but the returned object can't pipe to log. Is this intended?
if run the script not in ps function environment, the caller can receive the object can pipe the return to output
output in ps azure function:
2019-08-08T20:30:03.599 [Information] Executing 'Functions.bugrepro' (Reason='This function was programmatically called via the host APIs.', Id=a170dcbe-6062-420f-afc7-907d52a1081c)
2019-08-08T20:30:14.138 [Information] INFORMATION: PowerShell http trigger function processed work item
2019-08-08T20:30:21.155 [Information] INFORMATION: Thumbprint: E03A6AD85BEF21B872C1075EF3B6E5749904F232
2019-08-08T20:30:21.156 [Information] INFORMATION: Install-Certificate
2019-08-08T20:30:21.234 [Information] INFORMATION: End of Install-Certificate
2019-08-08T20:30:21.234 [Information] INFORMATION: Connect-AzAccount...
2019-08-08T20:30:21.993 [Information] INFORMATION: After Connect-AzAccount ...
2019-08-08T20:30:27.473 [Information] INFORMATION: Get 3 resource groups
2019-08-08T20:30:27.476 [Information] INFORMATION: After validation ...
2019-08-08T20:30:27.476 [Information] INFORMATION: Execution count: 1
2019-08-08T20:30:27.477 [Information] INFORMATION: context:
Account SubscriptionName TenantId Environment
70402f7e-6079-44ef-865f-55315dc7ce19 TestEA Customer3 72f988bf-86f1-41af-91ab-2d7cd011db47 AzureCloud
2019-08-08T20:30:27.477 [Information] OUTPUT: returned cxt in caller:
2019-08-08T20:30:27.477 [Information] OUTPUT:
2019-08-08T20:30:27.478 [Information] INFORMATION: context:
2019-08-08T20:30:27.478 [Information] INFORMATION: context | Out-String
2019-08-08T20:30:27.478 [Information] INFORMATION: Hello
2019-08-08T20:30:29.490 [Information] Executed 'Functions.bugrepro' (Succeeded, Id=a170dcbe-6062-420f-afc7-907d52a1081c)
The text was updated successfully, but these errors were encountered: