Recently, I ran into an issue when auditing TFS. The issue was related to an intermittent problem where the Powershell script would sometimes work, and sometimes fail. From a developer perspective, this is an impossibility. Code either works or it doesn't. If it sometimes work, then there is most likely something else at play.
Inevitably this lead me down the path of dependencies. I had to ensure that we had the right TFS DLLs loaded for the script. This can easily be done with reflection via the GetAssemblies() method of the CurrentDomain within the AppDomain object in Powershell. The simple command:
[AppDomain]::CurrentDomain.GetAssemblies()
returns a comprehensive list of loaded DLLs. From here you can identify if your DLL is loaded, or as was the case in another session for me, identify that wrong versions of the target DLL is loaded. That's good, but how do I use that logically in Powershell code e.g. when I want to see if a DLL is already loaded before attempting to load it again. (As we know, that throws ugly red text that the operator might mistake for an error rather than the EXPECTED error it is.)
Therefore, using the pipe, we could do something like this:
([AppDomain]::CurrentDomain.GetAssemblies() | where {$_ -match "Microsoft.TeamFoundation.Client"}).Location -eq ""
As you can see, we pipe the output from GetAssemblies() to the where clause attempting to match with the DLL partial name, e.g. "Microsoft.TeamFoundation.Client" in this case.
By wrapping the entire statement in parentheses, we can access the .Location property of the returned object, provided there is one. If there is none, the location value would be blank and by doing an equal check for blank, we can get a boolean result statement that identifies if a given DLL is loaded. :-)
Enjoy
C
The SharePoint Knowledge Collection of Cornelius J. van Dyk, an 9 x SharePoint MVP
Subscribe to:
Post Comments (Atom)
SharePoint Remote Event Receivers are DEAD!!!
Well, the time has finally come. It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...
-
Ever wondered what the new SharePoint Online URLs are all about? Take for example https://cjvandyk.sharepoint.us/:x:/r/sites/Site1... What ...
-
Every so often, you run across the need to redirect a page somewhere else, like after a server migration when DNS isn’t used. By simply addi...
-
When using NuGet, we can easily run into assembly reference issues. A notorious error message is: CS0012: The type ‘System.Object’ is def...
No comments:
Post a Comment
Comments are moderated only for the purpose of keeping pesky spammers at bay.