How do I get an instance of a currently running Visual Studio instance ?
I need to access Visual Studio object model (DTE) outside of the Visual Studio.
I want to run a nuget install script from powershell ise - to be able to better understand what the script is doing and why it's not doing what i expect
So I want to feed in all the parameters that install.ps1 takes in
param(
[Parameter(Mandatory=$true)] [string] $installPath,
[Parameter(Mandatory=$true)] [string] $toolsPath,
[Parameter(Mandatory=$true)] $package,
[Parameter(Mandatory=$true)] $project
)
It's easy to set the strings parameters, gowever
project and package are the tricky ones
I do have access to nuget cmdlets. I followed this post to achieve that.
So I import-module PackageManagement.Cmdlets.dll
That all works fine
now when I attempt something like
$project = Get-Project -name SmartCom.Registration.Logic
I get this message "A project must be open to run this command." so I guess I first need to hook into vs via DTE and open a solution and then load a project in order for this to work?
I know that there are things like PowerConsole that hook up with vs automatically, but in this case I want it to work outside of the vs so i can debug it with powershell isa
Any ideas?
Assuming Visual Studio is running (and there's only one instance) then you can do this:
PS> $dte = [runtime.interopservices.marshal]::GetActiveObject("visualstudio.dte")
PS> $dte
Name : Microsoft Visual Studio
...
Hope this helps.
The post you linked to uses SharpDevelop to allow the use of NuGet PowerShell cmdlets outside of Visual Studio. It has no dependency on Visual Studio so I do not believe opening an instance of Visual Studio will help you.
The error is being displayed because there is no solution currently open. To open a solution you can use the set-project cmdlet:
set-project MyProject d:\Projects\MyProject\MySolution.sln
Then you should be able to call the get-project cmdlet.
Related
When I try to use an extension method in the Immediate or Watch windows, I get the following error:
{method} is not a member of {class}
I'm using Visual Studio Community 2013 Update 4, but the issue exists on multiple PCs here, running varying versions of Visual Studio 2013 and 2015.
It makes no difference whether the extension methods come from the .NET BCL, or are defined in our project. The code itself compiles and runs successfully; the issue is only in Immediate and Watch.
I tried setting all projects to framework 4.5.1, and using the x86 configuration, without result.
Adding Imports System.Linq at the beginning of the code file makes no difference (which makes sense, as System.Linq is already globally imported (Project properties -> References -> Imported namespaces)).
What else can be done?
In any context where System.Linq isn't imported you can call the extension methods as normal static methods instead. So for example, the following did not work for me in the QuickWatch window (where actualVariables is a List):
actualVariables.Select(x=>x.Identity.DisplayName)
Change it to this form and then it works:
System.Linq.Enumerable.Select(actualVariables,x=>x.Identity.DisplayName)
I created a default ClassLibrary project in visual studio. Then I added an basic class that has one method in it.
Then I opened Package Manager console in visual studio. And I run standard Regex replace method on it and it works. Then I try to run my own replace method but it didnt work. Is there any way to run user defined methods on Package manager console or is it only allowed by core libraries on windows ?
PowerShell needs to load the type first before it can call it.
Add-Type -Path c:\test\ClassLibrary1.TestClass.cs
[ClassLibrary1.TestClass]::Replace("testx","x","y")
You could also put the add-type call into your NuGet_profile.ps1, and load your DLL if there will be multiple classes you need to call.
Recently I am facing a problem with msbuild.exe by building a dll. My idea is to call the msbuild from DOS command line to automatically create the DLL without opening MS Visual Studio. The command line (as example) I am using is:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe C:\example.sln /p:Configuration=example_config
The problem is, even I changed the files in this project (outside of MS Visual Studio), it seems that msbuild.exe isn't able to recognize the change and still output the old dll based on the old files.
Also I tried to delete the output DLL beforehand, but still the same result. The command line from DOS delivered the old dll.
If I open Visual Studio and build the output manually, the problem won't happen.
I checked around in web but can't find a feasible solution for this. Someone mentioned that it could be problem of MS. Actually, I was also not facing this problem under Windows XP.
I am using
List item
Window 7 64-bit version
MS Visual C++ 2010
The solution contains several sub-projects which deliver libary as output separately.
If anyone can help, I'll be very thankful.
You can build your project by calling Visual Studio 2010 from the command line. To build without opening Visual Studio, call "devenv.com" (command-line-friendly Visual Studio executable) to build the project. More info regarding devenv command line usage can be found on MSDN.
Customize the following command and run it from the command prompt to build your project:
"%VS100COMNTOOLS%..\IDE\devenv.com" "full-path-to-solution" /Rebuild "desired-configuration" /Project "full-path-to-project"
I'm trying to create a NuGet package that contains both managed (assemblies) and unmanaged (native DLLs) binaries, and also support several platforms (x86, x64).
To have a better understanding of how to handle that properly, I had a look at several packages that share similar requirements, published on the official nuget feed. One of them is Microsoft.SQLServer.Compact.
Seeing what is done in the Install.ps1, Uninstall.ps1 PowerShell scripts and in the VS.psm1 PowerShell module is very helpful.
The VS.psm1 module contains definitions for a few functions allowing to control Visual Studio from within the package installation script (notably, through the $dte object). These functions are based on the Visual Studio Object Model, which I don't know yet and that is (in my opinion) not very well documented.
I was wondering if you know about a PowerShell module that would allow to easily handle the Visual Studio Object model from a NuGet PowerShell script. Alternatively, any pointer to books, blog posts, articles, ... showing a few examples about how to properly use that object model from PowerShell would be very welcome. The only examples I have found on the net usually illustrate very basic functionality.
Also, if you know about other NuGet packages dealing with both managed and unmanaged libraries that I could use as examples, that would be nice.
I could of course start from what I have seen in the files mentioned above and roll my own module, but I would rather concentrate on my package itself if there is already something available.
Thanks for your help.
Take a look at StudioShell. It is a system for bringing PowerShell automation into VS.NET and is available as an installer but also as a NuGet package to enable the sort of scenario you describe. PluralSight course "Everyday PowerShell for Developers" course has an intro to the system and there are some examples on the project site.
This may not be directly related to your post. The Package Manager Console in my opinion should be called the PowerShell Console. It gives you full access to the DTE.
I have a trivial example of using it, pure PowerShell, no t4 or extensions, to add several files to Visual Studio to automate adding Command Pattern classes.
http://github.com/jefflomax/vs-package-manager-console-cmdlets
We could certainly use a more complete library of PowerShell cmdlets to handle simple Visual Studio tasks in this environment.
NuGet's PM console is great and all but you have to be in Visual Studio to use it. I have asked this question before without any ansewr and I find it hard to believe there is no documentation on the topic.
I have two users: User A sell shoes, User B sells cars. Each have different property needs.
If I allow them to write there needed properties/datatypes to their respective Product.cs entity, how can I use PowerShell.exe to:
Scaffold the controller, views, dbcontext and repositories?
I know I can do a MSBuild afterwards, but how is it there seems to be no way to use PowerShell from say a bat file to do the scaffolding outside VS 2010?
Any insight would be appreciated.
I finally figured this out after days of no luck. First and foremost, do not use any Microsoft betas, incl Windows 8 Developer Edition, PowerShell 3.0 and VS 2011. Once you have PowerShell 2.0 up and running:
1. in the PS/v1.0 folder add a powershell.exe.config file:
Follow this link for the script:http://connect.microsoft.com/PowerShell/feedback/details/525435/net-4-0-assemblies-and-powershell-v2
restart PowerShell 2.0 and it will now support 4.0 framework.
2. Set the execution policy to require only remote scripts to be sign:
Command line: Set-ExecutionPolicy RemoteSigned
3. Import the required Custom t4 scaffolders (so you can use T4 Scaffold):
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.NuGetServices.dll
Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.dll
4. Now invoke your powershell file:
Command Line:C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\MvcApplication1\CodeTemplates\Scaffolders\StevceScaffolders.AjaxGrid\StevceScaffolders.AjaxGrid.ps1
(be sure to quote any folder that has spaces, eg. "Visual Studio 2010"
I certainly think it would have been better if MS provided some insight on this in their documentation.
In trying to run it from powershell, you're in hardly-tread territory but its conceivable.
I think the T4 templates are rendered by an external tool. I can't see any reason why you couldn't invoke that EXE from powershell, passing parameters on the command line of the t4 template to be invoked. The resultant file (or files, see the Entity Framework T4 examples for how to write multiple files) would be output by the t4 conversion tool upon completion.
Your various different file types would be handled by different t4 templates, a Controller.t4, a Views.t4, Dbcontext.t4 and respositories.t4.
Hope that helps