0

I am trying to automatically clone my Bitbucket via a powershell script. I am currently using the following line of code:

git clone https://myUsername@bitbucket.org/project1/project1-database.git --quiet

This works as far as it goes, but I would prefer that you have to log in first to clone the repositories because others should also use the script. I have found the following:

Import-Module BitbucketPS
New-BitBucketSession -Credential (Get-Credential "")

This will create a window where you can enter your credentials. Unfortunately, an error message appears when executing the script:

Import-Module : The specified module "BitbucketPS" was not loaded, because no valid module file was found in any module directory. module file was found.

My question is how do I extend my script to import the module and connect to BitBucket from the login window?

UPDATE 1

if( -not (Test-Path -Path $path+\project1-database -PathType Any ))
    {
        git clone https://bitbucket.org/project1/project1-database.git --quiet
    }
    else
    {
        Set-Location $path+\project1-database
        git pull https://bitbucket.org/project1/project1-database.git --quiet
    }
Doncarlito87
  • 359
  • 1
  • 8
  • 25
  • 1
    "I would prefer that you have to log in first to clone the repositories" that's sort of already the case - since the password is not part of the repo URL, you must have credentials cached - so it already behaves according to whatever local git config is present. Simply remove the username from the URL (or parameterize the repo URL so users can provide their own) :) – Mathias R. Jessen Jan 31 '22 at 12:09
  • Thank you for your quick reply. If I leave out the username then it works as you said. Now I want to test it. I logged out of BitBucket and ran the script again. Still I am not asked for the crendentials. Do I have to remove my login data somewhere else first? – Doncarlito87 Jan 31 '22 at 12:18
  • 1
    Yeah you need to check the global config of the `git` installation: https://git-scm.com/docs/gitcredentials – Mathias R. Jessen Jan 31 '22 at 12:20
  • This works very well. I have additionally looked at the following ticket to delete the credentials: https://stackoverflow.com/a/39608906/12440701 – Doncarlito87 Jan 31 '22 at 12:30
  • For systems you admin, you can also use [`cmdkey`](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmdkey) for (some) credential management scenarios – Mathias R. Jessen Jan 31 '22 at 12:31
  • I have one last question if possible: I have added my code under UPDATE 1. The goal is that if the repository already exists in the folder then the current version of the repository should be pulled. Unfortunately I get an error: git : fatal: destination path 'project1-database' already exists and is not an empty directory. Do you have a quick solution for this or should I write a new ticket? – Doncarlito87 Jan 31 '22 at 12:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241569/discussion-between-mathias-r-jessen-and-doncarlito87). – Mathias R. Jessen Jan 31 '22 at 12:50

0 Answers0