powershellcertificatekeyself-signedpowergui

How do I generate a self-signed certificate and use it to sign my powershell script?


So I've been researching/googling for the last 2 hours, and I'm practically at the point of tears...

I can't use New-SelfSignedCertificate because I'm on Windows 7. I can't use makecert because of a bug that won't allow me to install the SDK for Windows 7 because it thinks I have a pre-release version of .NET 4, but I don't. Trying to install .NET 4 informed me I have a new or better version.

I tried a registry hack that I found to get around this, which unfortunately didn't work.

I've downloaded this https://gallery.technet.microsoft.com/scriptcenter/Self-signed-certificate-5920a7c6#content

But can't seem to manage to get through all the steps I need to actually get my script signed so I can give it to other people to use safely.

I think I've managed to create the certificate (although I'm not sure if I did it right).

From what I can tell I need to apply a password or key to it now, and then export it? I'm still not sure how I specifically sign my script, so others can execute it as 'Signed'.

Thanks guys.

Alternatively all this could possibly be unnecessary if anyone knows how I can get relative .ps1 paths working in a .exe file?

The script works fine as a .ps1, but as soon as I compile it into a .exe using PowerGUI, these lines don't work.

. .\Import-XLS.ps1
$OutFile = ".\TEST$(get-date -Format dd-MM).txt"
$Content = Import-XLS '.\TEST.xlsx'

I instead get things like "The term '.\Import-XLS.ps1' is not recognised as the name of a cmdlet, along with some reference to a Appdata\Local\Temp\QuestSoftware\PowerGUI\ folder.

So I'm guessing PowerGUI is doing something weird, but I don't know how else to convert a .ps1 into a .exe. Depending on the answer to the main question, I may submit a new question for the .exe one officially.

Thanks guys.


Solution

  • So I ended up resolving this issue with a combination of two things.

    Split-Path $MyInvocation.MyCommand.Path
    

    and

    [System.AppDomain]::CurrentDomain.BaseDirectory}
    

    I needed to use both, as the former worked in a .ps1 but not in a compiled .exe, while the latter worked in a compiled .exe, but not in a .ps1.

    As the PowerGUI compiled .exe has a consistent path folder name, I ended up using the following.

    $ScriptPath = Split-Path $MyInvocation.MyCommand.Path
    if ($ScriptPath -match 'Quest Software') {$ScriptPath = [System.AppDomain]::CurrentDomain.BaseDirectory}
    

    I also included the Function into the .exe (but it wasn't necessary). I then used $OutFile = "$ScriptPath\<Filename>.txt" and $Content = Import-XLS "$ScriptPath\<Filename>.xlsx"

    This means I can now use a .exe instead of trying to get a working certificate for the script. While also being able to quickly test changes to it while it's still a .ps1.

    I hope this is helpful for others using PowerGUI to make .exe's in the future, who also need to use relative paths.

    Thanks to those that provided help and advice.