So you need to authenticate yourself against the TFS?

Normally when you use the TFS REST API against a TFS located in your domain and you’re already authenticated against the domain you could use the switch -UseDefaultCredentials when you try to execute Invoke-WebRequest or Invoke-RestMethod. However, if you have a computer outside of the domain and you’re not authenticated against the DC, how could you connect in such case? Well if you add following function to your script:

function New-PSCredential {
  param (
    [string] $Username,
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    $Password
  )
  Set-StrictMode -Version 'Latest'
  if( $Password -is [string] ) {
    $Password = ConvertTo-SecureString -AsPlainText -Force -String $Password
  }
  elseif( $Password -isnot [securestring] ) {
    Write-Error ('Value for Password parameter must be a [String] or [System.Security.SecureString]. You passed a [{0}].' -f $Password.GetType())
    return
  }
  return New-Object 'Management.Automation.PsCredential' $UserName, $Password
}

 

Then you could call it to get a credential that works for the purpose.

[Management.Automation.PsCredential] $cred = New-PSCredential -Username 'domain\username' -Password 'p@55w0rd'

 

And then you could simply call the Rest or Web-method like this:

$resp = Invoke-WebRequest -Uri $uri -ContentType application/json -Method Get -Credential $cred

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s