1

I'm writing a little c# program to learn how to use it. The program should create a ps1 script in user-temp directory and start it. That works.

Now, the content of the ps1 script larger than before and the signing of the script doesn't work anymore. I sign the original ps1 script in PowerShell with Set-AuthenticodeSignature C:\tmp\littlescript.ps1 $cert

Inside the c# consoleapp I have a variable with the PowerShell Code AND the signingblock at the end as string. The ps1 script starts with an error, that the hash doesn't match. I think it could be a problem with the encoding.

Here are some parts of the c# Code:

string pscript1 =
@"#local ps1 script
function liste
{
    $dc = Get-ADDomainController -Discover -DomainName ""Domain.com""
    cls
    echo ""Welcome to the Little App""
    echo ""Choose Option 1 or 2...""
    …
    # SIG # End signature block
";

Because of problems with the quotations, I've created double quotations. Later in the program I create the local ps1 file:

if (!File.Exists(pathString))
{
    using (FileStream fs = File.Create(pathString))
    {
        Byte[] info = new UTF8Encoding(true).GetBytes(pscript1);
        // Add some information to the file.
        fs.Write(info, 0, info.Length);
    }
}

I'm a complete beginner and there are surely better ways to create the ps1 file, but it totally works for me. ; ) I'm open for your ideas! Thanks a lot.

4

0 回答 0