0

I have a C# code for accessing remote PowerShell and got this error after running: access in denied. the code is :

string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
var target = new Uri("http://Computername/wsman");

Pipeline p = runSpace.CreatePipeline();
SecureString passing = new SecureString();
string password = "pass";
foreach (char c in password)
{
    passing.AppendChar(c);
}
passing.MakeReadOnly();
var cred = new PSCredential(@"Domain\User", passing);
var connectionInfo = new WSManConnectionInfo(target, shell, cred); runSpace = RunspaceFactory.CreateRunspace(connectionInfo);    
 runSpace.Open(); 

this error is occurred because I can not access to this address: http://schemas.microsoft.com/powershell/Microsoft.PowerShell

after enter this address for connecting to it , show this message: An error occurred while processing your request.

Is there another address for schema or another way to write this code?

please help me. thanks,

1 Answer 1

0

First, try to access the remote machine from PowerShell first. It will be easier to debug the connection problem there IMO (instead of C#). Can you connect with the following command (from a 64-bit console):

$config = 'Microsoft.PowerShell'
$connuri = 'http://Computername:5985/wsman'
$s = New-PSSession -ConnectionUri $connuri -ConfigurationName $config -Credential (Get-Credential)
Invoke-Command -Session $s -ScriptBlock {Get-PSSessionConfiguration} | Format-List *

If that works then perhaps you're just missing the port number or there's a coding issue in your C#. However that would at least rule out a PowerShell configuration issue (which I suspect).

4
  • thanks, no , this command give this error in last line: access in denied. + CategoryInfo : NotSpecified: (:) [Get-ChildItem], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetChildItemCommand. but I can connect to remote powershell with EnterPSSession.
    – mary
    Aug 28, 2012 at 10:37
  • and I have another problem: when the internet is connected, the remote connection to powershell with enterPSSesion doesn't work and get this error: Connecting to remote server failed with the following error message : The WinRM client cannot complete the operation within the time specified. Check if the machine name is valid and is reachable over the network and firewall exception for Windows Remote Management service is enabled. please help me. thanks alot.
    – mary
    Aug 28, 2012 at 10:39
  • Is the network location set to Public on this machine? FYI blogs.msdn.com/b/powershell/archive/2009/04/03/…
    – Keith Hill
    Aug 28, 2012 at 13:47
  • I did instruction of MSDN but the powershell give same error: access is denied. for asp.net code, should I configure the web.config?
    – mary
    Aug 28, 2012 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.