Random Errors in Powershell

Of late i have been experiencing a strange behavior in powershell. I have written a script that will execute commands in powershell for every 5 minutes. First time, it will try to connect to my exchange server using New-pssession and saves the session if it is successful. After executing few commands, the execution fails with below errors :

Error 1 :

Exception calling “GetSteppablePipeline” with “1” argument(s): “Exception calling
“PromptForCredential” with “4” argument(s): “A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Enter your credentials for http://serverName/powershell?serializationLevel=Full.””|

StackTrace : at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)

Error 2
The term ‘Test-ImapConnectivity’ 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

Is there any setting related to powershell that is causing this ? I have checked user permissions, tried to connect manually and executed commands.Everything looks fine

Where are you running this? Remote run spaces don’t support prompting; you’d need to provide all the parameters - in this case I’m guessing a credential - with the command so it doesn’t need to prompt.

The script is run in a client machine.Credential is provided with New-Pssession command.The erorrs are occuring during invoking command (BeginInvoke) and ending the invoke operation(EndInvoke).

Roughly the script to connect to remote exchange server is :

Runspace^  runSpace = RunspaceFactory::CreateRunspace();
		PowerShell^ ps = PowerShell::Create();
		ps->Runspace = runSpace;
		PSCommand^ command = gcnew PSCommand();
		command->AddParameter("ConfigurationName", "Microsoft.Exchange");
		command->AddParameter("ConnectionUri", "http://servername/powershell?serialization=Full");		
		command->AddParameter("Credential", creds); // will have username and pwd stored in creds
		command->AddParameter("Authentication", "Kerberos");			
		ps->Commands = command;		
		Collection^ result = ps->Invoke();
		command = gcnew PSCommand();
		command->AddScript("Import-PSSession -Session $session");
		ps->Commands = command;

script to execute command is (Here, used stored ps variable and this will be called for every 5 minutes) :

PSCommand^ command = gcnew PSCommand();
    command->AddScript("Test-ImapConnectivity -ClientAccessServe xyz");
    ps->Commands = command;	
	IAsyncResult^ iAsyncResult = ps->BeginInvoke();			
    while (iAsyncResult->IsCompleted == false)

Ah. Things do work a bit differently in C# than in PowerShell per se; there’s a lot of state maintenance and such that the console provides which a base run space does not. In particular, when a prompt is required, your app is the one required to deal with it, and it isn’t - hence, the error. Passing a credential to a runspace is only useful when that runspace is operating against its local resources - if it needs to connect to something, the credential can’t be delegated further without an environmental configuration change. That appears, from the error, to be what’s happening. But I’m going to be of minimal help, because I take for granted all the magic built into the console, which you don’t have available.