You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过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 packageType in the JSON payload to match your needs—options include npm, docker, gradle, and more. You can also add extra properties like description or 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 ErrorVariable to catch and report failures, making it easier to troubleshoot issues like wrong credentials or invalid repo configurations.

内容的提问来源于stack exchange,提问作者Ken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:44:16