EDN Admin
Well-known member
(Hopefully someone here can help me with this. I have searched the Internet quite a lot but did not found a solution. I found some C# code for this, but at first I am not used to C# and second my translation into VB.net did not worked.)
I want to use Powershell in my VB 2008 code, and my code shall use the Windows authentication of that Active Directory user account, which is currently logged on. The Acccount has sufficient privileges to run the Powershell command against the remote server.
This here is the code snippet that works fine for me. The problem is, it has hard coded logon credentials, which I want to remove.
<pre class="prettyprint lang-vb Private SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange" Dim myRunSpace As Runspace = Nothing<br/>Dim pipeLine As Pipeline = Nothing<br/>Dim server As String = "MYSERVER"<br/><br/>Dim securePassword As System.Security.SecureString = New System.Security.SecureString()<br/>Dim c As Char<br/>Dim password As String = "mypassword"<br/>For Each c In password.ToCharArray()<br/> securePassword.AppendChar(c)<br/>Next<br/>Dim creds As System.Management.Automation.PSCredential = New System.Management.Automation.PSCredential("MyUsername", securePassword)<br/>Dim serverUri As System.Uri = New Uri(String.Format("https://" & server & "/powershell"))<br/>Dim wsManInfo As WSManConnectionInfo = New WSManConnectionInfo(serverUri, SHELL_URI, creds)<br/>wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential<br/>wsManInfo.SkipCACheck = True<br/>wsManInfo.SkipCNCheck = True<br/>wsManInfo.NoEncryption = False<br/>wsManInfo.MaximumConnectionRedirectionCount = 4<br/><br/>myRunSpace = RunspaceFactory.CreateRunspace(wsManInfo)<br/><br/>Console.WriteLine("Workspace opened")<br/>myRunSpace.Open()[/code]
I have tried to remove the credentials section and using Windows authentication by using this code:
<pre class="prettyprint Dim creds As System.Management.Automation.PSCredential = DirectCast(Nothing, PSCredential)
[/code]
The result is Access Denied. I have also tried all variations of authenticationmechanisms, no luck, just some different push backs.
<pre class="prettyprint wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential
wsManInfo.SkipCACheck = True
wsManInfo.SkipCNCheck = True
wsManInfo.NoEncryption = False
wsManInfo.MaximumConnectionRedirectionCount = 4[/code]
<br/>
What I am doing wrong here and how to solve this? Any help with some example code would be highly appreciated.
Many thanks in advance!
Klaus
View the full article
I want to use Powershell in my VB 2008 code, and my code shall use the Windows authentication of that Active Directory user account, which is currently logged on. The Acccount has sufficient privileges to run the Powershell command against the remote server.
This here is the code snippet that works fine for me. The problem is, it has hard coded logon credentials, which I want to remove.
<pre class="prettyprint lang-vb Private SHELL_URI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange" Dim myRunSpace As Runspace = Nothing<br/>Dim pipeLine As Pipeline = Nothing<br/>Dim server As String = "MYSERVER"<br/><br/>Dim securePassword As System.Security.SecureString = New System.Security.SecureString()<br/>Dim c As Char<br/>Dim password As String = "mypassword"<br/>For Each c In password.ToCharArray()<br/> securePassword.AppendChar(c)<br/>Next<br/>Dim creds As System.Management.Automation.PSCredential = New System.Management.Automation.PSCredential("MyUsername", securePassword)<br/>Dim serverUri As System.Uri = New Uri(String.Format("https://" & server & "/powershell"))<br/>Dim wsManInfo As WSManConnectionInfo = New WSManConnectionInfo(serverUri, SHELL_URI, creds)<br/>wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential<br/>wsManInfo.SkipCACheck = True<br/>wsManInfo.SkipCNCheck = True<br/>wsManInfo.NoEncryption = False<br/>wsManInfo.MaximumConnectionRedirectionCount = 4<br/><br/>myRunSpace = RunspaceFactory.CreateRunspace(wsManInfo)<br/><br/>Console.WriteLine("Workspace opened")<br/>myRunSpace.Open()[/code]
I have tried to remove the credentials section and using Windows authentication by using this code:
<pre class="prettyprint Dim creds As System.Management.Automation.PSCredential = DirectCast(Nothing, PSCredential)
[/code]
The result is Access Denied. I have also tried all variations of authenticationmechanisms, no luck, just some different push backs.
<pre class="prettyprint wsManInfo.AuthenticationMechanism = AuthenticationMechanism.NegotiateWithImplicitCredential
wsManInfo.SkipCACheck = True
wsManInfo.SkipCNCheck = True
wsManInfo.NoEncryption = False
wsManInfo.MaximumConnectionRedirectionCount = 4[/code]
<br/>
What I am doing wrong here and how to solve this? Any help with some example code would be highly appreciated.
Many thanks in advance!
Klaus
View the full article