Cannot import OS using C# code RRS feed

  • Question

  • It's hard to translate the powershell code to C#, so I try to run multi line powershell scripts in C#, and it show such error:

    The term 'Microsoft.BDD.PSSnapIn.import-mdtoperatingsystem' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'
    The code snippet is:

                Runspace runSpace = RunspaceFactory.CreateRunspace();
                Pipeline pipeLine = runSpace.CreatePipeline();

                string scripts = @"get-date;
                     Add-PSSnapIn Microsoft.BDD.PSSnapIn;
            New-PSDrive -Name 'DS001' -PSProvider MDTProvider -Root 'E:\DeploymentShare' ;
            Microsoft.BDD.PSSnapIn.import-mdtoperatingsystem -path 'DS001:\Operating Systems' -SourceFile 'H:\sources\install.wim' -DestinationFolder 'Win10 Custom Image' -Verbose    
                Collection<PSObject> resutls = pipeLine.Invoke();

    How to fix it?



    Tuesday, July 18, 2017 8:40 AM


  • At last, I use the other way to solve it. 

    1. write the powershell code to an ps script file.  and add a 5 seconds sleep at the end of the script, in case it doesn't make effect before processing.

    2. use process.start to run the ps script from cmd: cmd.exe /c powershell -F "psfile.ps1"

    and it works fine for all my test for more than 20 times. 



    • Marked as answer by Jerome Tang Thursday, July 27, 2017 8:08 AM
    Thursday, July 27, 2017 8:08 AM