none
Can't make New-AzApiManagementCertificate command working RRS feed

  • Question

  • Hello,

    According to the documentation, I should be able to run following two commands to add a certificate to APIM:

    $context = New-AzApiManagementContext -ResourceGroupName "resourcegroup" -ServiceName "resourcegroup-apim-name"
    New-AzApiManagementCertificate -Context $context -PfxFilePath "C:\temp\certificate.pfx" -PfxPassword "PASWORD"

    However, when running this, I am getting following meaningless error:

    New-AzApiManagementCertificate : Exception has been thrown by the target of an invocation.
    At line:1 char:1
    + New-AzApiManagementCertificate -Context $context -PfxFilePath ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : CloseError: (:) [New-AzApiManagementCertificate], TargetInvocationException
        + FullyQualifiedErrorId : Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.NewAzureApiManagementC
       ertificate

    Can anyone suggest a solution for that?

    Mike


    Wednesday, July 24, 2019 12:40 PM

Answers

  • Hi Michalkosowski,

    Thanks for using Azure. 

    Once I had Login-AzAccount and Set-AzContext to respective subscription where my resource exists , I was able to run the command successfully and don't see any issue and not able to reproduce on what you see. 

    $context = New-AzApiManagementContext -ResourceGroupName "rgname" -ServiceName "apimanagementname"
    New-AzApiManagementCertificate -Context $context -PfxFilePath C:\temp\certificate.pfx -PfxPassword password

    Even the command with "" , works too. 

    $context = New-AzApiManagementContext -ResourceGroupName "rgname" -ServiceName "apimanagementname"
    New-AzApiManagementCertificate -Context $context -PfxFilePath "C:\temp\certificate.pfx" -PfxPassword "password"
    Could you please give it a try without "" and make sure the pfxfilepath is accessible, please revert back with your outcome and we can continue to troubleshoot. Thank you. 

    Wednesday, July 24, 2019 5:53 PM
    Owner

All replies

  • Hi Michalkosowski,

    Thanks for using Azure. 

    Once I had Login-AzAccount and Set-AzContext to respective subscription where my resource exists , I was able to run the command successfully and don't see any issue and not able to reproduce on what you see. 

    $context = New-AzApiManagementContext -ResourceGroupName "rgname" -ServiceName "apimanagementname"
    New-AzApiManagementCertificate -Context $context -PfxFilePath C:\temp\certificate.pfx -PfxPassword password

    Even the command with "" , works too. 

    $context = New-AzApiManagementContext -ResourceGroupName "rgname" -ServiceName "apimanagementname"
    New-AzApiManagementCertificate -Context $context -PfxFilePath "C:\temp\certificate.pfx" -PfxPassword "password"
    Could you please give it a try without "" and make sure the pfxfilepath is accessible, please revert back with your outcome and we can continue to troubleshoot. Thank you. 

    Wednesday, July 24, 2019 5:53 PM
    Owner
  • Adding the output of the commands as well. Thanks

    Wednesday, July 24, 2019 5:56 PM
    Owner
  • Hi,

    It is working! :D looks like I did not made some prerequisites correct?

    I was not using Login-AzAccount but az login. Same for setting a context. Is there any difference between those two?

    Other think is that I can have made a typo some where, but the session data is lost now. Thanks for the clues anyway. Now I am going to test that in Azure DevOps

    Thursday, July 25, 2019 1:29 PM
  • Glad to hear that it worked out Michal. 

    Login-AzAccount and az login should do the same thing, former is through PowerShell and the later is using Azure CLI. Both of the commands are used only to login into Azure. 

    To set the context using Azure CLI , you might have to use "az account set"

    Hope that clarifies your query. Please feel free to revert back if you have any further questions. Thank you. 

    Thursday, July 25, 2019 3:15 PM
    Owner
  • Hey,

    So I need a help again - please tell me how to run above code in Azure DevOps Release Pipeline?

    1. I tried Azure Cli task, but:

    'New-AzApiManagementCertificate' is not recognized as an internal or external command

    2. Then I tried Azure Powershell Script task:

    ##[error]The term 'New-AzApiManagementContext' is not recognized as the name of a cmdlet, function, script file, or operable program.

    Ok, I am not that easy, I tried to install missing module calling:

    Install-Module -Name Az.ApiManagement

    But unfortunately got this:

    ##[warning]User declined to install untrusted module (Az.ApiManagement).

    I have added the -Force switch, no warning this time, but command is still not recognized.

    How I can make it working?

    Friday, July 26, 2019 9:30 AM
  • Hi Michal,

    Thank you for reaching out. Currently, Microsoft-hosted agents do not have the AZ CLI installed. You can either install it using npm install -g azure-cli (on Microsoft-hosted Linux agents, use sudo npm install -g azure-cli) or use a self-hosted agent with the AZ CLI pre-installed. If an agent is already running on the machine on which the Azure CLI is installed, restart the agent to ensure all the relevant stage variables are updated.

    Documentation reference - 

    Azure CLI Install the Classic Azure CLI

    Azure CLI Task Pre-requisites

    Can you please help confirm if the agent you are using have Azure CLI already installed ?

    Also you check out Az pipelines to manage Azure Pipelines via Azure CLI.

    Hope the above information helps, please revert if you have further queries. Thanks

    Wednesday, July 31, 2019 10:39 PM
    Owner
  • Hello,

    UPDATE

    So I made it working by changing the commands from Az to AzureRM version. Finally was able to add certificate during CI/CD deployment of APIM. Still, as far as I know, AzureRM is going to be decomissioned and we should use Az, am I right? If yes, below issues are still worth resolution...

    ==========================

    Sorry for a late response, but I just switched to other tasks.

    So yes, I understand what you are saying. I moved to use self hosted agent and tried to install the Az.ApiManagement module. And here I gone into troubles - conflicts with AzureRM already installed and working there. (I used -AllowClobber option when installing the module)

    My first thought was to remove AzureRM, but then my different pipelines stopped working (e.g. Azure File copy task). Not sure what is a best thing to do here - can you advice? Can I switch my pipeline tasks to use Az functions? How it can be done? As far as I understand both Az and AzureRM can't leave together.

    I am really  confused here - can you help?

    I have also tried to use "npm install -g azure-cli" on hosted agent but it result in errors on npm:

    2019-08-22T14:45:28.1027399Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers>if not defined npm_config_node_gyp (node "C:\npm\prefix\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild --release )  else (node "C:\npm\prefix\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" rebuild --release ) 
    2019-08-22T14:45:44.2918603Z Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.
    2019-08-22T14:45:54.9014178Z   fibers.cc
    2019-08-22T14:45:54.9014841Z   coroutine.cc
    2019-08-22T14:45:58.1374613Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(122,64): error C2660:  'v8::String::NewFromOneByte': function does not take 2 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1375202Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(2802,51): message :  see declaration of 'v8::String::NewFromOneByte' (compiling source file ..\src\fibers.cc) [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1375595Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(126,64): error C2660:  'v8::String::NewFromOneByte': function does not take 2 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1376125Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(2802,51): message :  see declaration of 'v8::String::NewFromOneByte' (compiling source file ..\src\fibers.cc) [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1376324Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(406,1): warning C4996:  'v8::String::Utf8Value::Utf8Value': was declared deprecated [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1376482Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(2891): message :  see declaration of 'v8::String::Utf8Value::Utf8Value' [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1377409Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(433,1): error C2661:  'v8::Function::NewInstance': no overloaded function takes 2 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1452691Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(433,17): error C2672:  'uni::Return': no matching overloaded function found [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1453814Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(621,14): error C2512:  'v8::TryCatch': no appropriate default constructor available [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1478701Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(8529,17): message :  see declaration of 'v8::TryCatch' (compiling source file ..\src\fibers.cc) [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1479439Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(628,1): warning C4996:  'v8::Script::Compile': was declared deprecated [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1479886Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(1255): message :  see declaration of 'v8::Script::Compile' [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1480231Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(643,1): warning C4996:  'v8::TryCatch::StackTrace': was declared deprecated [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1496413Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(8593): message :  see declaration of 'v8::TryCatch::StackTrace' [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1513777Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(741,1): error C2661:  'v8::Value::ToNumber': no overloaded function takes 0 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1514420Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(793,1): error C2660:  'v8::Object::SetAccessor': function does not take 2 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.1942551Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(3348,37): message :  see declaration of 'v8::Object::SetAccessor' (compiling source file ..\src\fibers.cc) [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.2156284Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(794,1): error C2664:  'v8::Maybe<bool> v8::Object::SetAccessor(v8::Local<v8::Context>,v8::Local<v8::Name>,v8::AccessorNameGetterCallback,v8::AccessorNameSetterCallback,v8::MaybeLocal<v8::Value>,v8::AccessControl,v8::PropertyAttribute,v8::SideEffectType)': cannot convert argument 2 from 'uni::FunctionType (__cdecl *)(v8::Local<v8::String>,const uni::GetterCallbackInfo &)' to 'v8::Local<v8::Name>' [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.2157178Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(794,1): message :  No constructor could take the source type, or constructor overload resolution was ambiguous [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.2157594Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\fibers.cc(795,1): error C2660:  'v8::Object::SetAccessor': function does not take 2 arguments [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:45:58.3263982Z   win_delay_load_hook.cc
    2019-08-22T14:46:02.2742570Z C:\Users\VssAdministrator\AppData\Local\node-gyp\Cache\10.16.1\include\node\v8.h(3348,37): message :  see declaration of 'v8::Object::SetAccessor' (compiling source file ..\src\fibers.cc) [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:46:02.4079736Z C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\src\coroutine.cc(138,2): warning C4552:  '!': result of expression not used [C:\npm\prefix\node_modules\azure-cli\node_modules\fibers\build\fibers.vcxproj]
    2019-08-22T14:46:02.6245029Z gyp ERR! build error 
    2019-08-22T14:46:02.6249772Z gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
    2019-08-22T14:46:02.6250230Z gyp ERR! stack     at ChildProcess.onExit (C:\npm\prefix\node_modules\npm\node_modules\node-gyp\lib\build.js:196:23)
    2019-08-22T14:46:02.6252026Z gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
    2019-08-22T14:46:02.6252313Z gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
    2019-08-22T14:46:02.6262596Z gyp ERR! System Windows_NT 10.0.17763
    2019-08-22T14:46:02.6263036Z gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\npm\\prefix\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--release"
    2019-08-22T14:46:02.6263463Z gyp ERR! cwd C:\npm\prefix\node_modules\azure-cli\node_modules\fibers
    2019-08-22T14:46:02.6263642Z gyp ERR! node -v v10.16.1
    2019-08-22T14:46:02.6263739Z gyp ERR! node-gyp -v v5.0.3
    2019-08-22T14:46:02.6263814Z gyp ERR! not ok 
    2019-08-22T14:46:02.6478120Z node-gyp exited with code: 1



    Thursday, August 22, 2019 11:36 AM
  • Thank you Michal for sharing the feedback. 

    AzureRM will no longer receive new cmdlets or features. However, the AzureRM module is still officially maintained and will get bug fixes through December 2020.

    Documentation link for Reference.

    Please refer to the document to see the breaking changes for Az 1.0.0

    Will further look into the specific scenario you are running into and share any insights we might have. Thanks 

    Wednesday, August 28, 2019 3:10 PM
    Owner