cli-microsoft365 icon indicating copy to clipboard operation
cli-microsoft365 copied to clipboard

CI/CD --> 'm365' is not recognized as an internal or external command, operable program or batch file.

Open martinlingstuyl opened this issue 1 year ago • 53 comments

Hi @AkashSatoliya, I created a separate issue for this as the comment trails might overlap otherwise.


Hi @martinlingstuyl,

I am also getting the same error. I am trying to create CD pipeline in Azure DevOps to deploy sppkg to SharePoint. I was getting error "The system cannot find the file specified." when running task to login to Sharepoint.

When I tried to login using m365 cli on one of my VM I was getting the same error:

m365 login --authType password --userName [email protected] --password ***
The system cannot find the file specified.

When I ran "npm view @pnp/cli-microsoft365" to check details on m365 CLI, I got below output:

C:\Users\User>npm view @pnp/cli-microsoft365

@pnp/[email protected] | MIT | deps: 24 | versions: 488
Manage Microsoft 365 and SharePoint Framework projects on any platform
https://pnp.github.io/cli-microsoft365/

keywords: office 365, microsoft 365, sharepoint framework, o365, m365, spfx, sha
repoint online, sharepoint, microsoft teams, microsoft graph, microsoft flow, az
ure active directory, azure ad, azure, microsoft

bin: m365, m365_comp, microsoft365

But When I run "m365 version" I'm getting below error:

C:\Windows\system32>m365 version
'm365' is not recognized as an internal or external command,
operable program or batch file.

Nodejs path(C:\Program Files\nodejs) was already included in PATH variable.

nodejs version: V14.15.0 Operating system: Windows Shell: PowerShell

Originally posted by @AkashSatoliya in https://github.com/pnp/cli-microsoft365/issues/3565#issuecomment-1223752643

martinlingstuyl avatar Aug 23 '22 16:08 martinlingstuyl

Hi @akashsatoliya,

Could you show us the relevant parts of your yaml file?

Also: are you using the CLI for Microsoft 365 action?

martinlingstuyl avatar Aug 23 '22 16:08 martinlingstuyl

Hi @martinlingstuyl,

Thank you so much for looking into it.

I am using classic pipeline for CD release. I have added tasks mentioned in this document.

Tasks are as follows:

  1. Node.js tool installer, v12.22.1
  2. npm install Microsoft 365 CLI: install -g @pnp/cli-microsoft365
  3. Command line to connect to SPO: m365 login -t password -u $(username) -p $(password)
  4. Command line to Add the solution to SP App Catalog: m365 spo app add -p $(System.DefaultWorkingDirectory)/D:\a\r1\a\_Application_CI\drop\App_Folder1\App_Folder2\sharepoint\solution\Package_Name.sppkg
  5. Command line to Deploy SP App: m365 spo app deploy --name Package_Name.sppkg

Variables defined are :

  1. catalogsite: sites/appcatalog
  2. username
  3. password
  4. tenant

I have tried running CD pipeline using MS hosted Windows and Ubuntu agents. The deployment tasks runs successfully till step 2 i.e. npm install Microsoft 365 CLI. I am getting error for step 3, i.e. Command line to connect to SPO. The error is as below:

========================== Starting Command Output =========================== ##[debug]Entering Invoke-VstsTool. ##[debug] Arguments: '/D /E:ON /V:OFF /S /C "CALL "D:\a_temp\032fe971-b3c4-4662-b7dc-544dd7b568d1.cmd""' ##[debug] FileName: 'C:\Windows\system32\cmd.exe' ##[debug] WorkingDirectory: 'D:\a\r1\a' ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a_temp\032fe971-b3c4-4662-b7dc-544dd7b568d1.cmd"" The system cannot find the file specified. ##[debug]Exit code: 1 ##[debug]Leaving Invoke-VstsTool. ##[error]Cmd.exe exited with code '1'.

I am not using CLI for Microsoft 365 action. I have configured only steps mentioned in the document.

I tried providing tenant details in login command, similar to o365 cli, like below but didn't work as well. m365 spo login $(tenant)/$(catalogsite) -t password -u $(username) -p $(password) or m365 login $(tenant)/$(catalogsite) -t password -u $(username) -p $(password)

AkashSatoliya avatar Aug 24 '22 04:08 AkashSatoliya

The line

install -g @pnp/cli-microsoft365

should be:

npm install -g @pnp/cli-microsoft365

Could you please try it and see if it makes any difference?

waldekmastykarz avatar Aug 24 '22 10:08 waldekmastykarz

Hi @waldekmastykarz,

Thank you so much for your suggestion. As suggested, I tried npm install -g @pnp/cli-microsoft365 but it failed while installing Microsoft 365 CLI in step 2 with below error:

##[warning]Couldn't find a debug log in the cache or working directory ##[error]Error: Npm failed with return code: 1

AkashSatoliya avatar Aug 24 '22 10:08 AkashSatoliya

Thank you for trying it and such a quick reply. Is there a way to get more detailed output log of this step that could help us understand what's wrong? Could you also share with us the configuration of this in your pipe? Thank you!

waldekmastykarz avatar Aug 24 '22 12:08 waldekmastykarz

Hi @waldekmastykarz,

As mentioned earlier here, the first error I got was The system cannot find the file specified. when running Azure DevOps task to login SharePoint. Then I tried installed M365 CLI on VM and tried to login manually to SharePoint and got same error The system cannot find the file specified..

The configuration steps for CD pipeline are as below:

  1. Node.js tool installer, v12.22.1
  2. npm task with Custom command to install Microsoft 365 CLI: npm install -g @pnp/cli-microsoft365
  3. Command line to connect to SPO: m365 login -t password -u $(username) -p $(password)
  4. Command line to Add the solution to SP App Catalog: m365 spo app add -p $(System.DefaultWorkingDirectory)/D:/a/r1/a/_Application_CI/drop/Folder1/Folder2/sharepoint/solution/Package_Name.sppkg
  5. Command line to Deploy SP App: m365 spo app deploy --name Package_Name.sppkg

Variables defined are :

  1. catalogsite: sites/appcatalog
  2. username
  3. password
  4. tenant

As per your suggestion, I updated the step 2 (npm task to install Microsoft 365 CLI) with command npm install -g @pnp/cli-microsoft365. Now pipeline fails on step 2 with error as mentioned here. Error Logs are as below:

 ##[section]Starting: Connect to SPO  ==============================================================================  Task         : Command line  Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows  Version      : 2.201.1  Author       : Microsoft Corporation  Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line  ==============================================================================  Generating script.  Script contents: shell  m365 login -t password -u username -p ***  ========================== Starting Command Output ===========================  ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a_temp\068d3114-e622-4a2c-b5d3-fb2012e21a0f.cmd""  The system cannot find the file specified.  ##[error]Cmd.exe exited with code '1'.  ##[section]Finishing: Connect to SPO

Detailed debug logs for step 2 are too long(450+ lines). Is it ok to post entire logs here?

AkashSatoliya avatar Aug 24 '22 13:08 AkashSatoliya

Hi @AkashSatoliya,

Okay, rollback to the step where the install completed successfully,

Could you run the following commands in a cmd task on a windows pipeline and see what the output is?

Check if the CLI is in fact installed:

npm list -g --depth 0 

Check the path where global modules are installed and verify if that path is in the PATH variable

npm root -g

echo %PATH%

martinlingstuyl avatar Aug 24 '22 18:08 martinlingstuyl

Hi @martinlingstuyl,

Thanks for reply.

Below are the updates for shared command:

  1. Check if the CLI is in fact installed:
    npm list -g --depth 0
    @pnp/[email protected]
  1. Path where global modules are installed
npm root -g
C:\npm\prefix\node_modules
  1. Verify if that path is in the PATH variable. Global module path C:\npm\prefix\node_modules is not available in PATH variable. Instead C:\npm\prefix is available in PATH variable.
echo %PATH%

AkashSatoliya avatar Aug 25 '22 06:08 AkashSatoliya

Okay, That's the reason. It cannot find the m365 executable, so it cannot execute its commands.

I find that odd though. You are running on GitHub? Azure DevOps? Your own host? Hosted vm?

The workaround is to add the global npm folder to your path. You can do this using the following statement:

set PATH=%PATH%;C:\npm\prefix\node_modules\

This adds the directory to the path in the current session and should cause m365 to be reachable.

martinlingstuyl avatar Aug 25 '22 06:08 martinlingstuyl

Hi @martinlingstuyl

I am running it on Azure DevOps Microsoft hosted agent.

I tried above command but it adds npm path to current task only. When next task is run path is not available in path variable.

I tried adding npm path to system path variable using below PowerShell and setx command but they also saved it to current task only, not to the system. When next task is run value is not available.

  • PowerShell
$PATH = [Environment]::GetEnvironmentVariable("PATH")
$npm_path = "C:\npm\prefix\node_modules"
$NewPath = "$(PATH);$npm_path"
[Environment]::SetEnvironmentVariable("PATH", $NewPath)

-Setx command

setx /M path "%path%;C:\npm\prefix\node_modules

AkashSatoliya avatar Aug 25 '22 07:08 AkashSatoliya

Hi @AkashSatoliya,

What's the content of your $PATH? Maybe some node.js folder is in it. In that case you could change the place where the npm install globally installs the packages to something already in the PATH. You can do this using an environmentvariable npm_config_cache, or using the --prefix option

npm install --prefix <path/to/prefix_folder> -g

You could also install the cli package locally (npm install @pnp/cli-microsoft365) and run the executable something like this:

./node_modules/.bin/m365 login

martinlingstuyl avatar Aug 25 '22 10:08 martinlingstuyl

@martinlingstuyl,

Thank you for your suggestion.

I have tried running command with PowerShell instead of command tasks.

$PATH = [Environment]::GetEnvironmentVariable("PATH")
$npm_path = "C:\npm\prefix\node_modules"
[Environment]::SetEnvironmentVariable("PATH", $NewPath)
Write-Output $Env:PATH

m365 login -t password -u ${username} -p ${password}

With above script I'm not getting error for m365 command. It attempts to login but fails with below error

Error: network_error: Network request failed. Please check network trace to determine root cause. | Fetch client threw: Error: HTTP status code 400 | Attempted to reach: https://login.microsoftonline.com/organizations/oauth2/v2.0/token

As per this document it is known issue with m365 cli and it has been resolved in m365 cli version 5.5. However I'm using m365 cli v5.6.0 but still getting this error.

AkashSatoliya avatar Aug 25 '22 14:08 AkashSatoliya

Hi @AkashSatoliya,

Okay, great that the command is now being executed at least.

Did you try running the login command on your local pc? Do you have the same issues there?

martinlingstuyl avatar Aug 25 '22 15:08 martinlingstuyl

Hi @martinlingstuyl,

I tried to run login command on local but there I am still getting 'm365' is not recognized as an internal or external command, operable program or batch file. error. Even though npm module folder path C:\npm\prefix\node_modules is already there in system PATH variable.

AkashSatoliya avatar Aug 25 '22 15:08 AkashSatoliya

Hi @martinlingstuyl,

I tried installing version 5.5.0 and latest version using install -g @pnp/cli-microsoft365@latest and run the command but still it fails with same network_error 400

AkashSatoliya avatar Aug 25 '22 16:08 AkashSatoliya

I'm not sure, but is it possible that the syntax ${password} is incorrect when you want to reference a pipeline variable in a powershell task? I believe it should be $(password)

martinlingstuyl avatar Aug 25 '22 16:08 martinlingstuyl

Hi @martinlingstuyl,

When trying with $(password) it results in variable reference error. PowerShell works with ${password} and $password. When tried with ${password} and $password it tries to login and fails with network error.

AkashSatoliya avatar Aug 26 '22 03:08 AkashSatoliya

Ok, so just to be sure: did you try this on your local pc as well? Signin In using username and password? Same problem there?

martinlingstuyl avatar Aug 26 '22 05:08 martinlingstuyl

Hi @martinlingstuyl,

I tried to run login command on local but there I am still getting 'm365' is not recognized as an internal or external command, operable program or batch file. error. Even though npm module folder path C:\npm\prefix\node_modules is already there in system PATH variable.

AkashSatoliya avatar Aug 26 '22 06:08 AkashSatoliya

Hi @AkashSatoliya,

On your local machine, could you run the following script to check if CLI is installed, if so where, and if that directory is on the path?

Same as before... If the directory is not on the path, add it.

npm list -g --depth 0 

npm root -g

write-host $Env:PATH

martinlingstuyl avatar Aug 26 '22 06:08 martinlingstuyl

When trying with $(password) it results in variable reference error.

@AkashSatoliya, reading this again. I think this means that the variable $(password) might be correctly written, but is not available.

Did you read the documentation on setting variables in a pipeline?: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cbatch#set-variables-in-pipeline

I think you need to define your variables first, and then change you script to:

m365 login -t password -u "$(username)" -p "$(password)"

martinlingstuyl avatar Aug 26 '22 10:08 martinlingstuyl

Hi @martinlingstuyl ,

Thank you so much for reply.

I've already defined the password variable in variables section of the pipeline. When I tried to run the pipeline with m365 login -t password -u "$(username)" -p "$(password)", it throws below error:

Error: network_error: Network request failed. Please check network trace to determine root cause. | Fetch client threw: Error: HTTP status code 400 | Attempted to reach: https://login.microsoftonline.com/organizations/oauth2/v2.0/token

When tried to manually reach https://login.microsoftonline.com/organizations/oauth2/v2.0/token, I'm getting below error

AADSTS900561: The endpoint only accepts POST, OPTIONS requests. Received a GET request.

Is there any change required in the URL?

Also could you please suggest if we can use an account, using which we can manually deploy to SharePoint site, but it doesn't have the administrative permissions on the tenant.

AkashSatoliya avatar Sep 05 '22 12:09 AkashSatoliya

Hi @AkashSatoliya, You can only call login.microsoftonline.com using a POST request. But that is not relevant to the current point.

I just tried it out: created a build pipeline with username and password variables. The yaml is as follows. You can import it if you want. The following works for me as is:

# Variable 'password' was defined in the Variables tab
# Variable 'username' was defined in the Variables tab
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-2019
  steps:
  - checkout: self
  - task: NodeTool@0
    displayName: Use Node 16.x
    inputs:
      versionSpec: 16.x
  - task: Npm@1
    displayName: npm custom
    inputs:
      command: custom
      verbose: false
      customCommand: install @pnp/cli-microsoft365 -g
  - task: PowerShell@2
    displayName: PowerShell Script
    inputs:
      targetType: inline
      script: >-
        # Write your PowerShell commands here.

        Write-Host "Hello World"

        $status = m365 status --output text

        Write-Host "Connected: $status"

        Write-Host "Signing in with $(username)"

        m365 login -t password -u "$(username)" -p "$(password)"

        $status = m365 status --output text

        Write-Host "Connected: $status"
...

If this doesn't work with you please try to run the m365 login command on your laptop in PowerShell and see if you can get it signed in. If it gives the same Network error, it may still be a bug with some special characters in your password. Although we fixed that in a previous release.

Please let me know how this works out for you.

martinlingstuyl avatar Sep 05 '22 14:09 martinlingstuyl

Also could you please suggest if we can use an account, using which we can manually deploy to SharePoint site, but it doesn't have the administrative permissions on the tenant.

I'm not sure, but I believe you can just give an account permissions on the App Catalog. That could be enough so that you would not need the SharePoint admin role.

martinlingstuyl avatar Sep 05 '22 14:09 martinlingstuyl

Hi @martinlingstuyl ,

I tried to run the m365 login command on my system in PowerShell and it failed with same Network error. It looks like it might be a bug with special character in password. A part of password is highlighted like a variable. When debugged the script, the part of password which is like a variable it empty. Hence the value of password variable goes like incorrect password.

AkashSatoliya avatar Sep 06 '22 08:09 AkashSatoliya

Hi @martinlingstuyl

I tried with new password which does not include special character used to define variable in PowerShell but still I'm getting same network error.

AkashSatoliya avatar Sep 06 '22 09:09 AkashSatoliya

Is it possible you have a $ in your password? You could try escaping that:

"mypass`$word"

If that solves it locally, it might not solve it in the pipeline. (Do try though) if it does not you could try updating your password.

martinlingstuyl avatar Sep 06 '22 09:09 martinlingstuyl

By the way: that's the backtick "`" that you need to use to escape dollar chars in posh. It's not a single quote.

martinlingstuyl avatar Sep 06 '22 09:09 martinlingstuyl

Hi @martinlingstuyl

My previous password included $ but new password doesn't have $ in it. However it includes & and anything after & gets excluded when running 'm365 login' command When I tried to run the login command in command prompt I am getting below error:

Error: network_error: Network request failed. Please check network trace to determine root cause. | Fetch client threw: Error: HTTP status code 400 | Attempted to reach: https://login.microsoftonline.com/organizations/oauth2/v2.0/token
'XXxxXX' is not recognized as an internal or external command, operable program or batch file.

Part "XXxxXX" is after & in password which gets excluded and treated as command.

AkashSatoliya avatar Sep 06 '22 09:09 AkashSatoliya

Ok, try escaping the & with the backtick, like I showed with the dollar sign. It should work locally.

martinlingstuyl avatar Sep 06 '22 09:09 martinlingstuyl