# Random Errors in Powershell

**URL:** <https://forums.powershell.org/t/random-errors-in-powershell/10724>\
**Category:** PowerShell Help\
**Created:** [May 30, 2018, 7:46am UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724 "2018-05-30T07:46:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai1150](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@sai1150](https://forums.powershell.org/u/sai1150)\
**Post date:** [May 30, 2018, 7:46am UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724/1 "2018-05-30T07:46:52Z")

</div>

Hi,  
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.](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

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [May 30, 2018, 7:54am UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724/2 "2018-05-30T07:54:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sai1150](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@sai1150](https://forums.powershell.org/u/sai1150)\
**Post date:** [May 30, 2018, 8:20am UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724/3 "2018-05-30T08:20:48Z")

</div>

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();
		runSpace->Open();
		PowerShell^ ps = PowerShell::Create();
		ps->Runspace = runSpace;
		PSCommand^ command = gcnew PSCommand();
		command->AddCommand("New-PSSession");
		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");
		command->Commands->Add("Out-String");
		ps->Commands = command;
		ps->Invoke();
```

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)
    {
                    			
        Thread::Sleep(1000);
                    			
    }
	ps->EndInvoke(iAsyncResult);
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [May 30, 2018, 8:23am UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724/4 "2018-05-30T08:23:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/random-errors-in-powershell/10724/5 "2024-05-16T20:34:59Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


