Link to home
Start Free TrialLog in
Avatar of tornadog
tornadog

asked on

Import-CSV nt recognized

I have a ps1 script to create mailboxes from a csv file. When I run the ps1 from the powershell commandline, it works fine. But when I call the same script through C# managed code, it bombs with the error message "import-csv" is not a valid cmdlet, script, etc. The same code works fine if I run a ps1 with something simple like get-mailbox or get-organization inside the ps1 file
Import-CSV $path |
foreach {
$Password = convertto-securestring "xxxxxxx" -asplaintext -force
$ln = $_.Lastname.length
if($ln -gt 0)
$name = $_.Firstname + ' ' +  $_.Lastname 

else

$name = $_.Firstname

$org = $_.EmailAddress.SubString($_.EmailAddress.IndexOf("@") + 1, $_.EmailAddress.IndexOf(".")-$_.EmailAddress.IndexOf("@")-1)

$mailboxexists = [bool](Get-Mailbox $name -Organization $org -ErrorAction SilentlyContinue)

if($mailboxexists)

 $name = $name+"1"


New-Mailbox -Database $org -name $name -UserPrincipalName $_.EmailAddress -Password $Password -FirstName $_.FirstName -LastName $_.LastName -Organization $org 
$l = $_.alias.length
if($l -gt 0)

Get-mailbox $name -organization $org | Set-mailbox -name $name -EmailAddresses @{'+'=$_.alias}

}

Open in new window

string serverName = "eXChange"; //getserver(RecordID);
                    System.Uri serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName));
                    string retVal = null;
                    string userName = "tenant\\administrator";
                    string password = "xxxxxx";
                    System.Security.SecureString securePassword = new System.Security.SecureString();

                    foreach (char c in password.ToCharArray())
                    {
                        securePassword.AppendChar(c);
                    }

                    System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);

                    RunspaceConfiguration rc = RunspaceConfiguration.Create();
                    WSManConnectionInfo wsManInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
                    // wsManInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;


                    string psScript = string.Empty;



                    using (Runspace rs = RunspaceFactory.CreateRunspace(wsManInfo))
                    {

                       



                        if (File.Exists(scriptpath))
                        {
                            System.IO.StreamReader sr = new System.IO.StreamReader(scriptpath);
                            psScript = sr.ReadToEnd().Replace("$path", csvpath);
                        }
                        else
                            throw new FileNotFoundException("Wrong path for the script file");


                        rs.Open();

                        PowerShell powershell = PowerShell.Create();
                        PSCommand command = new PSCommand();
                        command.AddScript(psScript);
                        powershell.Commands = command;
                        powershell.Runspace = rs;





                        Collection<PSObject> commandResults = powershell.Invoke();
                         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                         
                         if (powershell.Streams.Error.Count > 0)
                         {
                             foreach (ErrorRecord err in powershell.Streams.Error)
                             {
                                 stringBuilder.AppendLine(err.ToString() + "DASH");
                             }

                         }
                        // close the runspace
                        rs.Close();
                        // convert the script result into a single string  
                       
                        foreach (PSObject obj in commandResults)
                        {
                            stringBuilder.AppendLine(obj.ToString());
                        }
                        string OutPut =  stringBuilder.ToString();
                        return OutPut;

Open in new window

SOLUTION
Avatar of Em Man
Em Man
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@taga_ipil:
By the way, you've written a very nice article on PST files in your profile :)
Avatar of tornadog
tornadog

ASKER

@taga, yes I have tried that too but my issue is I am connecting to a remote server, so I am using the remoting method.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
found the solution myself
Congrads tornadog
The points were un-expected. Thanks :)