如何通过PowerShell调用Artifactory API创建仓库?
Creating an Artifactory Repository via REST API (PowerShell Example)
If you're looking to automate repository creation in Artifactory using PowerShell, here's a tested script that leverages the Artifactory REST API. This example sets up a local Maven repository, but you can tweak it for other package types easily:
# Repository Create - This PowerShell script will create a repository # Define the security protocol for https [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # Credentials for create repo operation - change to your admin/password # Note: this is an example - real code should use keyvault to retrieve credentials and use a service principle $AF_USER = "admin" $AF_PWD = ConvertTo-SecureString "password" -AsPlainText -Force $CREDS = New-Object System.Management.Automation.PSCredential ($AF_USER, $AF_PWD) # URL to test against $Urlprefix = "https://yourartifactory.com/artifactory/api" $repokey = "team-mvn-test-local" $Json = @{key="team-mvn-test-local"; packageType="maven"; rclass="local";} | ConvertTo-Json Invoke-RestMethod -Uri "$Urlprefix/repositories/$repokey" -body $Json -ContentType "application/json" -Method Put -Credential $CREDS -ErrorVariable RestError if ($RestError) {Write-Host "Failed to create $repokey"} Else {write-host "$repokey Created"}
Key Notes for Production Use:
- Security First: Never hardcode credentials in your scripts. Use a secure secrets manager (like Azure Key Vault) to pull credentials, and use a dedicated service account with only the permissions it needs instead of an admin user.
- Adapt to Your Package Type: Change the
packageTypein the JSON payload to match your needs—options includenpm,docker,gradle, and more. You can also add extra properties likedescriptionor storage settings to the JSON object if required. - TLS Compatibility: Setting TLS 1.2 ensures your script works with modern Artifactory instances that block older, insecure protocols.
- Error Debugging: The script uses
ErrorVariableto catch and report failures, making it easier to troubleshoot issues like wrong credentials or invalid repo configurations.
内容的提问来源于stack exchange,提问作者Ken
相关产品推荐
相关产品推荐

