header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Type Initializer threw Exeception
Last Post 05 Apr 2008 08:29 PM by topski. 2 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
topskiUser is Offline
New Member
New Member
Posts:10

--
03 Apr 2008 09:01 PM  

 

 

 

 

 

, snapInException)

Hi All,

 

I am using vb.net and try to load a snap in for Exchange with the following line :

 

 

Dim myRunspace As

Runspace

 

 

 

Dim rsConfig As

RunspaceConfiguration

 

 

 

Dim snapInException As New

PSSnapInException

 

 

 

Dim info As

PSSnapInInfo

 

 

 

 

'The following code opens a runspace that has access to the Exchange Management Shell.

 

rsConfig = RunspaceConfiguration.Create()

info = rsConfig.AddPSSnapIn(

 

"Microsoft.Exchange.Management.PowerShell.Admin"

 

This results in an Exception :

The Type Initializer threw an Exception : Microsoft.Exchange.Data.Directory.Globals

Any ideas ??

BR,

Ronald

 

marco.shawUser is Offline
Co-Community Director
Basic Member
Basic Member
Posts:181

--
04 Apr 2008 01:27 AM  
I think you're code is really getting "chewed" up when you submit it. Can you attach the script as a txt, for example?
topskiUser is Offline
New Member
New Member
Posts:10

--
05 Apr 2008 08:29 PM  
Hi Marco,

Here's what I have :
Exchange 2007 SP1 ru1 on Win2K3 on a remote server. Got the Management tools (same sp level as server) installed on my development PC.

Here's what I do :
Running this code within a local admin privilege context, there's no problem.
Running this code within a windows user privilege context, I get the execption, despite the fact that an impersonation is done before calling the actual PowerShell routines.

And here's the code :
Just create a form1, add the powershell reference to your project, create a button btn_ok and paste the code. Fill in the proper credentials in the impersonation call.


Public Class Form1

Public strDomainController As String = "DomainController01"

Public impersonationContext As System.Security.Principal.WindowsImpersonationContext
Public currentWindowsIdentity As System.Security.Principal.WindowsIdentity

Public LOGON32_LOGON_INTERACTIVE As Integer = 2
Public LOGON32_PROVIDER_DEFAULT As Integer = 0

Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer

Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer

Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

Public Function impersonateValidUser(ByVal userName As String, _
ByVal domain As String, ByVal password As String) As Boolean

Dim tempWindowsIdentity As System.Security.Principal.WindowsIdentity
Dim token As IntPtr = IntPtr.Zero
Dim tokenDuplicate As IntPtr = IntPtr.Zero
impersonateValidUser = False

If RevertToSelf() Then
If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, _
LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
tempWindowsIdentity = New System.Security.Principal.WindowsIdentity(tokenDuplicate)
impersonationContext = tempWindowsIdentity.Impersonate()
If Not impersonationContext Is Nothing Then
impersonateValidUser = True
End If
End If
End If
End If
If Not tokenDuplicate.Equals(IntPtr.Zero) Then
CloseHandle(tokenDuplicate)
End If
If Not token.Equals(IntPtr.Zero) Then
CloseHandle(token)
End If
End Function

Public Sub undoImpersonation()
impersonationContext.Undo()
End Sub

Function GetEmailAddressPolicyUsr(ByVal Ident As String) As String

Dim commandResults
Dim item
Dim prop

Dim PSCommand As String

PSCommand = "Get-EMailAddressPolicy"
Dim myCommand As New Command(PSCommand)

myCommand.Parameters.Add("DomainController", strDomainController)
myCommand.Parameters.Add("Identity", Ident)

commandResults = PowerShellResults(myCommand)

GetEmailAddressPolicyUsr = ""

For Each item In commandResults

prop = item.properties("EnabledPrimarySMTPAddressTemplate")
GetEmailAddressPolicyUsr = prop.value.ToString

Next

If Not IsNothing(PSCommand) Then
PSCommand = Nothing
End If
If Not IsNothing(myCommand) Then
myCommand = Nothing
End If
If Not IsNothing(commandResults) Then
commandResults = Nothing
End If

End Function
Function PowerShellResults(ByVal CommandArray As Command)

Dim myRunspace As Runspace
Dim rsConfig As RunspaceConfiguration
Dim snapInException As New PSSnapInException
Dim info As PSSnapInInfo

'The following code opens a runspace that has access to the Exchange Management Shell.
rsConfig = RunspaceConfiguration.Create()
info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapInException)
myRunspace = RunspaceFactory.CreateRunspace(rsConfig)
myRunspace.Open()

'First, create a new instance of the Pipeline class by using the runspace that you created.
Dim pipeLine As Pipeline
pipeLine = myRunspace.CreatePipeline()

'Next, add the command to the Commands collection of the pipeline.
pipeLine.Commands.Add(CommandArray)

'Now, call the Pipeline.Invoke method to run the command.

PowerShellResults = pipeLine.Invoke()

myRunspace.Close()
myRunspace = Nothing
rsConfig = Nothing
info = Nothing
snapInException = Nothing

End Function


Private Sub btn_ok_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_ok.Click

Dim Ident As String = "te"

'Impersonate Exchange Admin to get the policies
impersonateValidUser("ExchAdmin", "DOMAIN", "Password")
Dim strEmailAddressPolicyUsr As String = GetEmailAddressPolicyUsr(Ident)
'Undo Impersonation
undoImpersonation()

MsgBox(strEmailAddressPolicyUsr)
End Sub

End Class
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer