Search code examples
c#powershellvisual-studio-2008powershell-4.0powershell-5.0

Call powershell v4 and above from Visual Studio 2008


I have Powershell scripts in my system, which require Powershell v4 at least. I have powershell v5, I am able to run those scripts using Powershell console.

But I want to call those scripts from C#-Visual Studio 2008(.net 3.5). I am restricted to use Visual Studio 2008. I have referred System.Management.Automation in my solution from path

"C:\WINDOWS\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" But this supports only Powershell v2.

I downloaded latest System.Management.Automation.dll from NuGet and my solution does not recognize it. Is this possible to achieve?


Solution

  • Well, it's been years, since I've even seen anyone with Visual Studio 2008. ;-}

    However, this is not really a PowerShell non working code question (which is what we are here to help with) but a Visual Studio environment / operational configuration one, in order to run PowerShell at all.

    Visual Studio is bound to the .Net version on its release as is PowerShell.

    Microsoft .NET Framework Requirements

    Windows PowerShell 5.1 requires the full installation of Microsoft .NET Framework 4.5. Windows 8.1 and Windows Server 2012 R2 include Microsoft .NET Framework 4.5 by default.

    Windows PowerShell 5.0 requires the full installation of Microsoft .NET Framework 4.5. Windows 8.1 and Windows Server 2012 R2 include Microsoft .NET Framework 4.5 by default.

    Windows PowerShell 4.0 requires the full installation of Microsoft .NET Framework 4.5. Windows 8.1 and Windows Server 2012 R2 include Microsoft .NET Framework 4.5 by default. …

    Of course since you say you have and have used PowerShell v5 on your system, then you have the needed .Net dlls, and you should not have had to download the dll you did separately. Lastly, each of those System.Management.Automation.dll are located / registered in their own folders.

    C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35 C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.134.1.4\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\f C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.134.1.4\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\r C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.194.1.5\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\f C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~17763.194.1.5\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\r C:\Windows\WinSxS\msil_system.management.automation_31bf3856ad364e35_1.0.0.0_none_6340379543bd8a03 C:\Windows\WinSxS\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0 C:\Windows\WinSxS\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\f C:\Windows\WinSxS\msil_system.management.automation_31bf3856ad364e35_10.0.17763.134_none_3a18c3b5ad9702a0\r C:\Windows\WinSxS\msil_system.management.automation_31bf3856ad364e35_10.0.17763.1_none_b60c54e2801db2ff

    You also, don't say or show how you tried toe register that .dll so that Visual Studio 2008 would see it. So, this appears, this is a Visual Studio 2008 specific limitation, and your only option is to shell out to PowerShell.exe independently, as you would cmd.exe, to run your .ps1 files.

    Have you already seen / tried the following approaches / details?

    Visual Studio 2008 PowerShell

    PowerShell Visual Studio 2008 templates

    Visual Studio 2008 project and item templates for Windows PowerShell including Cmdlet,

    Calling a PowerShell Script From Your .NET Code

    An article on embedding and/or launching PowerShell scripts from a C# program.

    Use VSCmdShell to run a PowerShell script from within Visual Studio 2008