Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何验证Invoke RestMethod以列出工件存储库_Powershell_Artifactory_Credentials - Fatal编程技术网

Powershell 如何验证Invoke RestMethod以列出工件存储库

Powershell 如何验证Invoke RestMethod以列出工件存储库,powershell,artifactory,credentials,Powershell,Artifactory,Credentials,尝试使用PowerShell 5.1从Win10 desktop调用RestMethod从Artifactory Enterprise v6实例获取存储库列表,但无法查看以使其进行身份验证 看起来很简单,但是这个 $myCred = Get-Credential notStanley $lstART = Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Credential $myCred

尝试使用PowerShell 5.1从Win10 desktop调用RestMethod从Artifactory Enterprise v6实例获取存储库列表,但无法查看以使其进行身份验证

看起来很简单,但是这个

$myCred = Get-Credential notStanley
$lstART = Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Credential $myCred
$myCred=获取凭证
$lstART=调用RestMethod-URIhttps:///artifactory/api/repositories -凭证$myCred
仅返回允许匿名访问的项

如果我打开浏览器并登录到该Artifactory实例,我就可以粘贴上面的URI,并获得我的帐户可以访问的所有存储库的完整列表


是否有任何提示表明,
$myCred
缺少什么?

我过去曾尝试使用artifactory,但
-Credential
对我来说并不奏效

我尝试了更简单、更容易使用的API方法

使用API密钥连接到Artifactory

了解如何在artifactory上为您的帐户获取API密钥

$header = @{"X-JFrog-Art-Api" = "yourAPIKey"}
Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Headers $header
$header=@{“X-JFrog-Art-Api”=“yourAPIKey”}
调用RestMethod-URIhttps:///artifactory/api/repositories -Headers$header
使用基本身份验证和凭证

如果确实希望使用Get Credential提示符,请确保使用在Artifactory中工作的用户名。它与域\用户不同

$login=Get Credential-消息“为Artifactory输入凭据”
#无效的信用。。但是没关系。需要告诉invoke restmethod使用基本身份验证。
$headers=@{Authorization=“Basic Zm9vOmJhcg==”}
#使用-Credential覆盖凭据。
$new=调用RestMethod-URIhttps:///artifactory/api/repositories -Headers$Headers-凭证$login

谢谢Jawad。这让我开始使用API(我的第一次尝试没有完全正确地形成)。在您的链接之后,我发现了另外两个问题(2795561和60325084),它们也帮助我获得了凭证。为了避免混淆源代码中的API键,我选择了Credential

我的基础骨架现在看起来像:

# get standard PowerShell credential
$myCred  = Get-Credential -Message "just <name> for this server, not '<domain>\<name>'"

# format credential for Artifactory API
$credUser    = $myCred.UserName                                   # extract user name
$credPswd    = $myCred.GetNetworkCredential().password            # extract user password
$credPair    = "${credUser}:${credPswd}"                          # concatenate into BasicAuth format
$credBytes   = [System.Text.Encoding]::ASCII.GetBytes($credPair)  # convert byte values to text
$cred64      = [System.Convert]::ToBase64String($credBytes)       # condense a bit more secure string RFC2045-MIME    
$credAuth    = "Basic $cred64"                                    # intermediate formatting 
$restHeaders = @{ Authorization = $credAuth }                     # initialize web headers

# clear collection array
$cfgSite = @()

# locate server
$lstURL "https://<myserver>/artifactory/api/repositories"

# get list of repositories
$theseRepo = Invoke-RestMethod -Headers $restHeaders -Uri $lstURL 

# collect each configuration
ForEach ($thisRepo in $theseRepo)
   {
   $thisURI  = $lstURL + $thisRepo.key
   $thisCfg  = Invoke-RestMethod -Headers $restHeaders -Uri $thisURI    
   $thisCfg  | Add-Member -NotePropertyName "SITE" -NotePropertyValue "$thisSite"     
   $cfgSite += $thisCfg
   }

# output to file
$cfgAll | Export-Csv .\lstArtRepoConf.csv -NoTypeInformation   
#获取标准PowerShell凭据
$myCred=获取凭据-消息“仅用于此服务器,而不是'\'”
#为Artifactory API格式化凭据
$credUser=$myCred.UserName#提取用户名
$credPswd=$myCred.GetNetworkCredential().password#提取用户密码
$credPair=“${credUser}:${credPswd}”#连接为基本格式
$credBytes=[System.Text.Encoding]::ASCII.GetBytes($credPair)#将字节值转换为文本
$cred64=[System.Convert]:ToBase64String($credBytes)#压缩更安全的字符串RFC2045-MIME
$credAuth=“基本$cred64”#中间格式
$restHeaders=@{Authorization=$credAuth}#初始化web头
#清除集合数组
$cfgSite=@()
#定位服务器
$lstURL“https:///artifactory/api/repositories"
#获取存储库列表
$theseRepo=调用RestMethod-Headers$restHeaders-Uri$lstURL
#收集每个配置
ForEach($theseRepo中的本次回购)
{
$thisURI=$lstURL+$thisRepo.key
$thisCfg=调用RestMethod-Headers$restHeaders-Uri$thisURI
$thisCfg |添加成员-NotePropertyName“SITE”-NotePropertyValue“$thisSite”
$cfgSite+=$thisCfg
}
#输出到文件
$cfgAll |导出Csv。\lstArtRepoConf.Csv-NoTypeInformation
# get standard PowerShell credential
$myCred  = Get-Credential -Message "just <name> for this server, not '<domain>\<name>'"

# format credential for Artifactory API
$credUser    = $myCred.UserName                                   # extract user name
$credPswd    = $myCred.GetNetworkCredential().password            # extract user password
$credPair    = "${credUser}:${credPswd}"                          # concatenate into BasicAuth format
$credBytes   = [System.Text.Encoding]::ASCII.GetBytes($credPair)  # convert byte values to text
$cred64      = [System.Convert]::ToBase64String($credBytes)       # condense a bit more secure string RFC2045-MIME    
$credAuth    = "Basic $cred64"                                    # intermediate formatting 
$restHeaders = @{ Authorization = $credAuth }                     # initialize web headers

# clear collection array
$cfgSite = @()

# locate server
$lstURL "https://<myserver>/artifactory/api/repositories"

# get list of repositories
$theseRepo = Invoke-RestMethod -Headers $restHeaders -Uri $lstURL 

# collect each configuration
ForEach ($thisRepo in $theseRepo)
   {
   $thisURI  = $lstURL + $thisRepo.key
   $thisCfg  = Invoke-RestMethod -Headers $restHeaders -Uri $thisURI    
   $thisCfg  | Add-Member -NotePropertyName "SITE" -NotePropertyValue "$thisSite"     
   $cfgSite += $thisCfg
   }

# output to file
$cfgAll | Export-Csv .\lstArtRepoConf.csv -NoTypeInformation