Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello All,

I would like to know how I can execute enter-pssession command using c# code.
I have used following command to run it in powershell console.
C#
$secpasswd = ConvertTo-SecureString "123$" -AsPlainText -Force 
$mycreds = New-object -typename System.Management.Automation.PSCredential("WIN-APRJ2923AV5.abc.com\Administrator",$secpasswd)
Enter-pssession 192.168.1.194 –credential $mycreds

But I want to do it using c# code, any help would be appreciated.

Regards
SEbastian
Posted
Comments
Herman<T>.Instance 11-May-12 7:00am    
what does pssession do?
Sebastian T Xavier 14-May-12 1:11am    
It enables us to enter into a power shell session of a remote machine. So we can execute any power-shell commands from local PC.

1 solution

May be this might be of any help to you:

XML
After the installation of WinRM ctpv3 and powershell ctpv3 you need to reference the correct System.Management.Automation.dll.

First add a reference tag from within the csproj file that you wish to use the Automation library. To do this open the csproj file with a text editor and locate the starting tag; <ItemGroup>.

Inbetween these tags you should see a bunch of <Reference Include> tags. Locate the last </ItemGroup> tag and on the line before it place
<Reference Include="System.Management.Automation />

This will give you access to the System.Management.Automation.Remoting namespace.

You can now use the WSManConnectionInfo class which allows you create Remote Runspaces.

Enjoy!

<Code>

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
new Uri(liveIdconnectionUri),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
creds);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

// create a runspace on a remote path
// the returned instance must be of type RemoteRunspace
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

</Code>


Source link: http://www.vistax64.com/powershell/213821-ctp3-remote-runspace-net-c.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900