0

I am trying to learn about writing snapin for powershell 4.0 using VS 2013. The script is very simple and compiled with no error. However, when I added gacutil and installutil to deploy the snapin using post-built, I got this error:

Error   1   
The command ""C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\gacutil.exe" -if "Tools.Powershell.Printscreen.dll"
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" "Tools.Powershell.Printscreen.dll"" exited with code -1.

However, if I copied the commands and ran it from command prompt, both commands ran successfully and the installation was successfully deployed. I can then add the snapin to powershell, the snapin ran successfully as well.

I checked and the framework used was correct 4.5.1, VS 2013, system is windows 8.1

The post-build commands are:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\gacutil.exe" -if "$(TargetPath)"
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" "$(TargetPath)"

I think at this point the code for the snapin is irrelevant but if you want me to include, please let me know. I do not believe it is going to affect the solution at this point bc it is simple at best and it worked after I deployed it manually using command prompt.

Please advise.

Thanks!

user1205746
  • 3,110
  • 11
  • 44
  • 73

1 Answers1

2

Why build a snapin unless you need to target V1 which you can't if you're compiling against .NET 4.5.1. I would recommend using a module. There is no registration/installation required. You just xcopy the module to the target system ($home\Documents\WindowsPowerShell\Modules) and use it. I would recommend that you create a PSD1 manifest file for your module. The RootModule will be the name of your dll: Tools.Powershell.Printscreen.dll. Check out the New-ModuleManifest command for help creating the PSD1 file. It is basic stuff like author name, version, GUID to uniquely identify the module, etc. I also recommend that you list your exported cmdlets in the CmdletsToExport field. This will speed processing for automatic module loading.

And in a pinch, for testing purposes, you can just import the dll directly (without having to create a PSD1 file):

Import-Module -assembly c:\temp\Tools.Powershell.Printscreen.dll
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thank you for your input, will try using module instead as I am still very new to powershell snapin and windows 8.1. Just want to make sure I understand you correctly: I can not use post build in VS2013 to automatically deploy the snapin if my powershell version is V4. Correct? I can still deploy it manually (which I did and it seems to work). – user1205746 Aug 01 '14 at 13:05
  • No, I didn't mean to imply that - only that modules have been available since version 2. You only *need* to use a snapin if you need to target PowerShell v1. In theory the gacutil/installutil commands should work. Have you seen this SO post: http://stackoverflow.com/questions/6656235/gacutil-postbuild-event-exists-with-code-1 – Keith Hill Aug 01 '14 at 15:55
  • Thanks! That question is similar to mine, just different operating system, windows 8 instead of 8.1. That one actually had syntax error and had to run it from command to troubleshoot it. Mine is working fine outside of VS2013 (gac it and install it with no problem but gave me error left and right when I used PostBuild) It should be something in the postbuild that I missed. BTW, doing xcopy on postbuild did not work for me either. Still error out and if I xcopy it from Command, then I have no problem. Maybe it is a bug of VS2013? – user1205746 Aug 01 '14 at 17:26