Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Problem to execute PowerShell in C#

$
0
0

Hello Everyone, i´m trying execute a Sql Server Full backup in C# using this code:
     private string RunScript(string scriptText)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();

            pipeline.Commands.AddScript(scriptText);

            pipeline.Commands.Add("Out-String");

            Collection<PSObject> results = new Collection<PSObject>();

            try
            {

                results = pipeline.Invoke();

            }

            catch (Exception ex)
            {

                results.Add(new PSObject((object)ex.Message));

            }

            runspace.Close();

            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            return stringBuilder.ToString();
    }

 I created a list containing the backup cmdlet, when my code passes trough the "Comand.Add(" $fullBackupFile = Join-Path $backupfolder $backupfile");"  i have a error message saying:
"Cannot bind argument to parameter 'Path' because it is null."
Commands that fill this variable are:
    Comand.Add(" $backupfolder = " + "\"C:\\Backup\\\"");
        Comand.Add(" $backupfile = " + "\"$($databasename)_Full_$($timestamp).bak\"");
 Problem   ->   Comand.Add(" $fullBackupFile = Join-Path $backupfolder $backupfile");

Thanks!


E-mail: eduardo-schmidt@hotmail.com


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>