Unknown Error running Powershell script from WinForms

I am trying to run a Powershell script from WinForms but i get a confusing error message.
VMware Error Message
To translate the german, “The ordinal 381 could not be located in the dynamic link libary”.

What is weird is that the WinForms Application by itself is working all fine, so is the Powershell Script. Only when i try to run the PS from WinForms, i get the error. Even weirder is that it tries to locate the ordinal in VMware, which i’m not even using in any way. I have no idea what to do.

This is my WinForms Code

private void buttonRunVBS_Click(object sender, EventArgs e)
        {
            try
            {
                AutoClosingMessageBox.Show("Running syncWizard, please be patient.", "Information", 5000);

                string scriptName = "syncWizardV6.ps1";
                string scriptPath = GetFolderPath(scriptName);

                PowerShell ps = PowerShell.Create();
                ps.AddScript(File.ReadAllText(scriptPath));
                //ps.AddParameter("-ExecutionPolicy Bypass");
                ps.Invoke();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }

This is my PS script

Add-Type -AssemblyName PresentationCore,PresentationFramework
$msgTitle = "syncWizard V6.0"
$msgButton = 'OK'
$msgImage="Asterisk"
$finMsg = ""
$driveEject = New-Object -comObject Shell.Application

$msgBody = "Copying your files. Please be patient."
[System.Windows.MessageBox]::Show($msgBody,$msgTitle,$msgButton,$msgImage)

$ErrorActionPreference = "SilentlyContinue"

$folderPath = "$PSScriptRoot" + "\copyFolder\*.*"

$drive = Get-Volume | Select -Property DriveLetter, DriveType |
Where-Object {$_.DriveLetter -ne "`0" -and $_.DriveType -eq 'Removable'}
$driveLetters = $drive.DriveLetter

foreach($letter in $driveLetters)
{
$destPath = "$letter" + ":\"
Copy-Item -Force -Recurse -Verbose $folderPath -Destination $destPath
$driveEject.Namespace(17).ParseName($letter+":\").InvokeVerb("Eject")
$finMsg = $finMsg + "`r`n" + $letter + " " + [char][convert]::ToInt32("2713",16) + " " + [char][convert]::ToInt32("2912",16)
}

[System.Windows.MessageBox]::Show($finMsg)

Leave a Comment