How do I get a list of all tfs alert subscriptions via powershell (get .Net assembly via PowerShell?) - powershell

I'm trying to move my tools to powershell, can this be done in PowerShell? the bit I'm really interested in is:
IEventService es = tfs.GetService(typeof(IEventService)) as IEventService;
List<Subscription> ls = es.GetAllEventSubscriptions().ToList();
Edit: what I really want to do might be using a .NET assembly from powershell and this might then be a duplicate of Using .NET library from PowerShell

Here is a TFS API in PowerShell function that I found on a blog long ago that will get you started. I've posted it to a GitHub Gist. Basically you ensure you've loaded up the TFS assemblies into the AppDomain and then you can add any TFS Service Interfaces you want to the object and just operate on them just as you would in any c# application, etc.
https://gist.github.com/3288447
Once you have the TFS object returned from the method in the Gist above, you can operate on the loaded services like so:
#use work item service
$tfs = get-tfs $env:TFSSERVERURL -silent
$project = $tfs.wit.Projects | ?{ $_.Name -eq $projectName}
#todo - replace with text for query or file read -- this is deprecated
$query = $project.StoredQueries | ?{ $_.Name -eq 'Active Bugs' }
$queryText = $query.QueryText.Replace("#project","'$projectName'")
$results = $tfs.wit.Query($queryText)
#do something with the results...
In your request above you can just alter the get-tfs method to add your service interface to the set loaded and then operate on the .NET methods much like I do in the example above.

Related

How to destroy a branch in TFVC using the REST API?

I am looking to delete and then destroy a branch in TFVC (in Azure Devops) using the REST API and Powershell, but after checking the documentation I have to ask: is this possible using the API?
Using the GET documentation as a guide I could take a guess at it and run something similar to the following:
DELETE https://dev.azure.com/{organization}/{project}/_apis/tfvc/branches?path={path}&api-version=6.0
but given the destructive nature of these calls I am reluctant to guess. And there is no obvious way to invoke the destroy functionality.
Alternatively, is there a way to trace the API call that must (probably?) be executed if I was to run the command line tf vc destroy "$/MyBranch/Path"? Or am I going to have to resort to using a Powershell snapin for this?
Why not use tf vc destroy from powershell? Most of TFVC is handled by the old SOAP API and doesn't have a REST equivalent. The investment in more REST APIs for TFVC is unlikely. You can invoke tf vc from powershell assuming you have Team Explorer installed on the machine.
Alternatively, you can load the Client Object Model into PowerShell directly and invoke the destroy calls from it. The call is pretty straightforward. That way the Client Object Model will do all the API wrestling. You can get the assemblies from NuGet without installing Team Explorer to a machine.
To get an instance of the VersionControlServer class you can look at my TFVC tasks. The code below is untested, but should get you really close:
[System.Reflection.Assembly]::LoadFrom("Newtonsoft.Json.dll")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Common")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.VersionControl.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.WorkItemTracking.Client")
[System.Reflection.Assembly]::LoadFrom("Microsoft.TeamFoundation.Diff")
$OnNonFatalError = [Microsoft.TeamFoundation.VersionControl.Client.ExceptionEventHandler] {
param($sender, $e)
if ($e.Exception -ne $null -and $e.Exception.Message -ne $null)
{
Write-Message -Type Warning $e.Exception.Message
}
if ($e.Failure -ne $null -and $e.Failure.Message -ne $null)
{
Write-Message -Type Warning $e.Failure.Message
if ($e.Failure.Warnings -ne $null -and $e.Failure.Warnings.Length -gt 0)
{
foreach ($warning in $e.Failure.Warnings)
{
Write-Message -Type Warning $warning.ParentOrChildTask
}
}
}
}
function Get-TfsVersionControlServer()
{
[cmdletbinding()]
param(
[string] $ProjectCollectionUri
)
$collection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(
$ProjectCollectionUri)
$collection.EnsureAuthenticated()
$versionControlServer =
$provider.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$versionControlServer.add_NonFatalError($OnNonFatalError)
return $versionControlServer
}
Get-TfsVersionControlServer().Destroy(...)
Then call the destroy function from there.
I looked at the API documentation as well. In my experience, Azure keeps their API documents up to date pretty well. The absence of the DELETE operation in their documentation indicates that the DELETE operation is not yet supported. Even if it 'worked', I wouldn't use it until it was supported in their API - especially with something dealing with version control.

Get installed software product context using powershell

I can easily get all installed software products on a machine using
Get-WmiObject -Class Win32_Product
Now I'd like to also fetch the Product Context. How can I access this information for every installed product using PowerShell.
In VB I did that by using the WindowsInstaller COM-Object and then querying the information. In essence this:
Set Com = CreateObject('WindowsInstaller.Installer')
Set Products = Com.ProductsEx(vbNullString,"S-1-1-0",7)
For Each P in Products
context = P.Context
Which I dont not manage to replicate in PowerShell
I realize this question is a bit stale, but I disagree with what seems to be the prevailing notion that working with Windows Installer in PowerShell is somehow a "pain" and more complicated than working with it in VBScript (this post is just one of many).
I have found that VBScript Windows Installer code translates quite literally to PowerShell, which means there are numerous examples of VBScript Windows Installer scripts that can be adapted to PowerShell and used to learn how to work with Windows Installer in PowerShell.
For this specific question of install context, the PowerShell code is quite similar to the VB code the OP gave.
# code must be run with admin rights to use "S-1-1-0" SID
enum InstallContext {
FirstVisible = 0 # product visible to the current user
None = 0 # Invalid context for a product
UserManaged = 1 # user managed install context
UserUnmanaged = 2 # user non-managed context
Machine = 4 # per-machine context
All = 7 # All contexts. OR of all valid values
AllUserManaged = 8 # all user-managed contexts
}
$Installer = New-Object -ComObject WindowsInstaller.Installer
foreach ($P in $Installer.ProductsEx("", "S-1-1-0", 7)) {
[InstallContext]$P.Context()
}
NOTE: I used Enums (about Enum - PowerShell | Microsoft Docs) with PowerShell here since tagMSIINSTALLCONTEXT is an enum in the msi.h file.
It's a pain to use that com object in powershell. I would use vbscript instead and save the text output to a powershell variable, or find an msi powershell module. That com object doesn't have a "type library" or support "IDispatch". The Windows Powershell in Action appendix for 2nd edition goes into it, but even there it's not pretty. That vbscript code has errors.

Use PowerShell to destroy source code marked for deletion in TFS, is this possible?

Example
If you look at the picture, I have marked some sample files/source in VS2017 source Control Explorer as deleted, but TFS doesn't delete it automatically, you have to destroy it manually with the tf destroy command.
I wanted to make PowerShell use the TFS REST API to get source that has been marked for deletion, but looking through their API reference, I haven't found a way to do this, it doesn't seem like the API exposes that kind of information, but somehow Visual Studio 2017 is able to get it, so surely I should be able to?
I'm using TFS2017 Update 2, so this is api version 3.2.
Why I want to do this: We're running low on space and we have lots of team project collections with source marked for deletion. It would be very tedious to destroy everything manually.
If you have TFS Power Tools you can use this PowerShell script to destory all the deleted items (with tf and not with rest api, but it does the work, you don't need to do it manually):
$tfsServer = Get-TfsServer -name http://YOURTFSSERVER:PORT
$destroyList = get-tfschilditem -item $/Root/ -server $tfsServer -Deleted -recurse | Where {$_.DeletionId -ne 0}
foreach($item in $destroyList)
{
$itemPath = '"' + $item.ServerItem + ";X" + $item.DeletionId + '"'
tf destroy $itemPath /i /startcleanup
}
There are no REST APIs for this. You will have to use tf.exe.

How are exported Azure Automation Graphical Runbook definition (.graphrunbook) encoded/encrypted?

This is the (trimmed) content of a .graphrunbook after Exported from the Azure Portal.
{
"SchemaVersion" : "1.2",
"RunbookDefinition" : "AAA....AAA="
}
The runbook definition is presumably XML- or XAML-based, and while this looks like base64 encoded text, decoding it as such results in large portions that are still encoded/illegible.
Context: I'm working on a revamped version of the Sync-VsoGitRunbook that works with ARM, doesn't require an arbitrary folder hierarchy (by automatically determining dependencies), and can deploy graphical runbooks as well - because why not! This is all pretty much wrapped up for Native & Workflow, these are just the last piece to implement.
I'm not using any graphical runbooks currently, but if/once we do, I want them to fall in line with the architecture. I'm also planning on open-sourcing the code once I get it wrapped/cleaned up.
Using the Azure Automation Graphical Authoring SDK is the only supported way of inspecting and modifying the content of .graphrunbok files. For example, a script like this will give you a list of commands used by the runbook:
param(
[Parameter(Mandatory = $true)]
$RunbookPath,
$GraphicalAuthoringSDKPath = 'C:\Program Files (x86)\Microsoft Azure Automation Graphical Authoring SDK'
)
function Get-RunbookActivities($Runbook, $ActivityType)
{
$Runbook.Activities | %{ $_ -as $ActivityType } | ?{ $_ }
}
Add-Type -Path $GraphicalAuthoringSDKPath\Orchestrator.GraphRunbook.Model.dll
$SerializedRunbook = Get-Content $RunbookPath
$Runbook = [Orchestrator.GraphRunbook.Model.Serialization.RunbookSerializer]::Deserialize($SerializedRunbook)
#{
'Invoked runbooks' = Get-RunbookActivities $Runbook Orchestrator.GraphRunbook.Model.InvokeRunbookActivity |
%{ $_.RunbookActivityType.CommandName }
'Commands' = Get-RunbookActivities $Runbook Orchestrator.GraphRunbook.Model.CommandActivity |
%{ "$($_.CommandType.ModuleName)/$($_.CommandType.CommandName)" }
'Code activity content' = Get-RunbookActivities $Runbook Orchestrator.GraphRunbook.Model.WorkflowScriptActivity |
%{ $_.Process }
}
The data provided by this script is incomplete: it lists only activities shown as boxes on the runbook diagram. However, there are other ways to introduce PS code into runbooks (such as link conditions, retry conditions, PS expressions in activity parameters, etc.) If you are interested in this data as well, the script can be improved: all the data stored in a graphical runbook can be retrieved using this API.
Please download the latest version of the SDK from the link provided by Chris. The latest version contains some important fixes.
Use the Azure Automation Graphical Authoring SDK.
https://azure.microsoft.com/en-us/updates/public-preview-automation-graphical-authoring-sdk/

Get Latest Version of Folder from TFS, using Powershell

I am trying to "Get Latest Version" of a particular folder from TFS, using Powershell.
I have installed the TFS Snappin, and have been using TFS Power Tools cmdlets in PowerShell (such as Get-TfsChildItem and Select-TfsItem etc) [How do I set up TFS PowerShell Snapin ], and have gone through their documentation (which I didn't find explanatory enough!).
Confused, on the exact cmdlet to use, when I am trying to get the latest version of an entire Folder structure from TFS, that is mapped to my local drive (and not just a changeset or ChildItem).
Example :
Tfs Path - $/APD-RepairSolutions/Main/Database
Mapped path - D:\TFS\APD-RepairSolutions/Main/Database.
I want a code, that would iteratively get the latest version of the entire folder Database,( that has number of tables,stored procedures etc.)
I am using ..
PS D:\Tfs\APD-RepairSolutions\Main\Database> $server=Get-TfsServer -Name http://tfs:8080/tfs
PS D:\Tfs\APD-RepairSolutions\Main\Database> Get-TfsChangeset -Recurse -Server $Server
Not helping my case - as it is only returning the latest changeset in the current directory.
To get latest (tf get) use Update-TfsWorkspace.
Get-TfsChangeset is the equivalent of tf changeset.
Gotcha! with Update-TFSWorskpace. Has some helpful parameters as well. -Items is used to specify the exact items you want to update.
PS D:\Tfs\APD-RepairSolutions\Main>Update-TFSWorkspace -All -Overwrite -Force -Recurse -Items .\Database
The Workspace is replaced with updated versions of items. Thanks #Kmoraz!
If you would like to use the TFS API instead, you can use the Workspace.Get Method:
# Load the TFS assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
$ws = $vcServer.QueryWorkspaces("<WORKSPACENAME>", $null, $null);
# Specify properties of intended workspace get
$recursionType = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full
$latestVersion = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest
$getOptions = [Microsoft.TeamFoundation.VersionControl.Client.GetOptions]::GetAll
# Get what I want!
$ws.Get("$/Remote/Folder", $latestVersion, $recursionType, $getOptions)
Would be a good idea to replace the nulls with your domain login and computer name when Querying Workspaces.