Kg7
Kg7

Reputation: 75

Powershell - Exception calling "GetService" with "1" argument(s): "TF30063

I am trying to automate task creation via powershell. I followed this link and it was running fine with TFS url.

But after changing my devops url from http://tfsapp/tfs/ to https://dev.azure.com/, I am getting following error-

Exception calling "GetService" with "1" argument(s): "TF30063: You are not authorized to access
https://dev.azure.com/"
At TFS Task Creation Script v2\AddTasksToTFSv2.ps1:41 char:1
+ $ws = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Clie ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TeamFoundationServerUnauthorizedException

Here is my code -

Function CreateWorkItem{
[string]$tfsCollectionUrl="TFS collection URL"
[string]$tfsTeamProjectName="team project"

$teamProjectCollection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$proj = $ws.Projects[$tfsTeamProjectName]

$wit = $proj.WorkItemTypes["Task"]

#Create a new work item of that type
$workitem = $wit.NewWorkItem()

$workItem.Title = "Sample Task Title 3"
$workItem.Description = "Sample Description"
$workitem.AreaPath = $tfsTeamProjectName
$workitem.IterationPath = $tfsTeamProjectName

$workItem.Save()
Write-Host "The TFS work item number is: " $workItem.Id
}`

I have already checked my windows credential manager and it has new credential. I am able to access it via browser and Visual studio.

Upvotes: 0

Views: 226

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30412

Just use the following code to create the NetworkCredential using PAT, and then use the credential to access the service.

#Create NetworkCredential using PAT
$aa = New-Object System.Net.NetworkCredential "","PAT-Here"
$bb = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential $aa
$cred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials $bb
$cred.AllowInteractive =$false

$teamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection([System.Uri]$tfsCollectionUrl, $cred)

The full script for your reference (works for me):

$TfsAssembliesPath="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"    
Add-Type -Path "$TfsAssembliesPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.ServiceBus.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Common.dll"
#Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.VisualStudio.Services.Client.Interactive.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Core.WebApi.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.ProjectManagement.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Build.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.Git.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.QueryLanguage.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.DataStoreLoader.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Common.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
Add-Type -Path "$TfsAssembliesPath\Microsoft.TeamFoundation.TestManagement.Client.dll"

Function CreateWorkItem{
[string]$tfsCollectionUrl="https://dev.azure.com/{organization}"
[string]$tfsTeamProjectName="RunTests"

#Create NetworkCredential using PAT
$aa = New-Object System.Net.NetworkCredential "","PAT-Here"
$bb = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential $aa
$cred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials $bb
$cred.AllowInteractive =$false

$teamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection([System.Uri]$tfsCollectionUrl, $cred)
#$teamProjectCollection=[Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$proj = $ws.Projects[$tfsTeamProjectName]
$wit = $proj.WorkItemTypes["Task"]

#Create a new work item of that type
$workitem = $wit.NewWorkItem()
$workItem.Title = "Sample Task Title 5"
$workItem.Description = "Sample Description"
$workitem.AreaPath = $tfsTeamProjectName
$workitem.IterationPath = $tfsTeamProjectName

$workItem.Save()
Write-Host "The TFS work item number is: " $workItem.Id
}
CreateWorkItem

Upvotes: 1

Related Questions