Zobrazujú sa príspevky s označením PowerShell. Zobraziť všetky príspevky
Zobrazujú sa príspevky s označením PowerShell. Zobraziť všetky príspevky

štvrtok 4. februára 2010

PowerShell and Team Foundation Server API

Team Foundation Server has a great API and you can use it to extend its capabilities or automatize tasks and so on. This API can be also used from PowerShell, which demonstrates this post. You have to know only how to use .NET from PowerShell. Following code uses TFS API to get count of new lines of code added in specified changeset:



   1:  ################################################################################

   2:  #

   3:  #  Get-AddedLinesCountInChangeset

   4:  #

   5:  ################################################################################

   6:   

   7:  param 

   8:  (

   9:      [string] $tfsUrl      = $(throw "TFS URL is required"),

  10:      [int]    $changeSetId = $(throw "Changeset ID is required")

  11:  )

  12:   

  13:  [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

  14:  [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")

  15:   

  16:  function LinesCountInFile($item, $version)

  17:  {

  18:      $tempFile = [System.IO.Path]::GetTempFileName()

  19:   

  20:      $itemVersion = $vcs.GetItem($item.ServerItem, $version)

  21:      $itemVersion.DownloadFile($tempFile)

  22:      $linesCount = (Get-Content $tempFile | Measure-Object).Count

  23:      Remove-Item -Force $tempFile

  24:   

  25:      $linesCount

  26:  }

  27:   

  28:  function ProcessChange($change)

  29:  {

  30:      if($change.Item.ItemType -eq "Folder")

  31:      {

  32:          return $False

  33:      }

  34:   

  35:      $extensions = ".cs", ".aspx", ".ascx", ".xml", ".xaml", ".config"

  36:   

  37:      foreach ($extension in $extensions)

  38:      {

  39:          if ($change.Item.ServerItem.EndsWith($extension))

  40:          {

  41:              return $True

  42:          }

  43:      }

  44:   

  45:      return $False

  46:  }

  47:   

  48:  $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsUrl)

  49:  $vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])

  50:   

  51:  $thisVersionSpec = New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec $changeSetId

  52:  $previousVersionSpec = New-Object Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec ($changeSetId - 1)

  53:   

  54:  $chs = $vcs.GetChangeset($changeSetId)

  55:  $totalLinesCount = 0

  56:  Write-Host

  57:   

  58:  foreach ($change in $chs.Changes)

  59:  {

  60:      if (ProcessChange($change))

  61:      {

  62:          $nameParts = $change.Item.ServerItem.Split("/")

  63:          $fileName = $nameParts[$nameParts.Length - 1]

  64:   

  65:          $thisLength = LinesCountInFile $change.Item $thisVersionSpec

  66:   

  67:          if ($change.ChangeType.ToString().Contains("Add"))

  68:          {

  69:              $previousLength = 0

  70:          }

  71:          else

  72:          {

  73:              $previousLength = LinesCountInFile $change.Item $previousVersionSpec

  74:          }

  75:   

  76:          $added = $thisLength - $previousLength

  77:   

  78:          Write-Host "$fileName             $added" -Fore Yellow

  79:          if ($added -gt 0) { $totalLinesCount = $totalLinesCount + $added }

  80:      }   

  81:  }

  82:   

  83:  Write-Host "------------------------------------------------------"

  84:  Write-Host "Total Lines:            $totalLinesCount" -Fore Green

  85:  Write-Host


If you use TFS 2005/2008 then specify only server url, when TFS 2010 then specify url including the collection.


PowerShell console
Manning - Team Foundation Server 2008 in ActionWrox - Professional Visual Studio 2005 Team System

streda 7. októbra 2009

PowerShell and SharePoint API

PowerShell and SharePoint API is really powerfull combination which can help us to automatize administration and development tasks. SharePoint exposes .NET API which can be typically called from some .NET programming language such as C#. Using this approach we have to write code, compile it and than run. SharePoint API can be also used from PowerShell, this approach removes the step of compilation. Only write a script and run it.

Following PowerShell script demonstrates using of SharePoint API from PowerShell. Script is intended to collectively set the force checkout property of document libraries (Shared Documents) in all webs of the site collection on true.


   1:  #################################################################################

   2:  #

   3:  #  Set-RequireCheckOut

   4:  #  Sets ForceCheckout on document libraries Shared Documents on site collection

   5:  #

   6:  #################################################################################

   7:   

   8:  param

   9:  (

  10:      [string] $siteUrl

  11:  )

  12:   

  13:  [void][Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

  14:   

  15:  $site = New-Object Microsoft.SharePoint.SPSite $siteUrl

  16:   

  17:  foreach ($web in $site.AllWebs)

  18:  {

  19:      foreach ($list in $web.Lists)

  20:      {

  21:          if ($list.Title -eq "Shared Documents")

  22:          {

  23:              $list.ForceCheckout = $True

  24:              $list.Update()

  25:          } 

  26:      }

  27:  }

Manning - Windows PowerShell In ActionMS Press - Inside Microsoft Windows SharePoint Services Version 3