Unable to load Exchange 2010 modules spawned from SSIS

by masquap at 2013-02-12 15:13:45

Hey all,

I am in the process of finalising an SSIS package that performs various tasks including creating and updating users in active directory with a PowerShell script I’ve written. When executed from the command line or the run command the task completes successfully, but when executed from the SSIS package it fails for a couple of reasons. The task actually runs but the Exchange 2010 modules fail to load and I have to run it with the executionpolicy bypass flag despite disabling that for my account (Yes, I know but it irks me).

I have checked that the process is running as my account (though it’s run under DtsDebugHost.exe when run from BIDS), and tried various other methods (including a HORRIBLY messy scheduled task execution)

The process that’s failing is a simple:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

The error I’m receiving is:

add-pssnapin : The Windows PowerShell snap-in ‘Microsoft.Exchange.Management.PowerShell.E2010’ is not installed on
this computer.
At C:_AdminTools\ADuser.ps1:1 char:1
+ add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Excha…owerShell.E2010:String) [Add-PSSnapin], PSArgumentEx
ception
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

I’ve tried to load the libraries using import-module which appears to not be an option in Exchange 2010, tried to figure out a path to add to the PsModulePath environment variable which doesn’t appear to exist (which is why the last command fails) and the only information that I could find is they don’t want you to run add-pssnapin in that way (which isn’t an acceptible answer).

I’m assuming I’m just doing something stupid and it’s a simple fix but can anyone give me an idea of how I can either do this properly, or turn off all security features blocking it? (Yes, I’m that frustrated I’d be happy to turn off all security features.)

My massively convoluted, rage induced solution was to add an account to log on as a batch, create a scheduled task then run it using schtasks.exe from the SSIS execute process task with the user’s password specified in plain text in the parameters. Obviously this introduces SO many more security issues than could possibly exist in just letting me load the modules.
by masquap at 2013-02-12 15:37:01
Actually it appears it’s not letting me run anything with modules at all.

A "Get-Module –ListAvailable"

Gives me:

Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

Yet I get the error:

ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest ActiveDirectory {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-A…
etc

dsquery : The term ‘dsquery’ 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.
At C:_AdminTools\test\addsnapin.ps1:5 char:1
+ dsquery
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (dsquery:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Yet it works perfectly when run manually, from the start | run command or from a scheduled task run as me.

FYI the whole test script contains:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
Import-Module ActiveDirectory
Get-Module –ListAvailable
get-pssnapin
dsquery
by masquap at 2013-02-12 15:38:22
For your reference when run manually from within PowerShell it gives me the following:

PS C:_AdminTools\test> .\addsnapin.ps1


Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest ActiveDirectory {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-A…
Manifest AppLocker {Get-AppLockerFileInformation, Get-AppLockerPolicy, New-AppLockerPoli…
Manifest Appx {Add-AppxPackage, Get-AppxPackage, Get-AppxPackageManifest, Remove-Ap…
Manifest BestPractices {Get-BpaModel, Get-BpaResult, Invoke-BpaModel, Set-BpaResult}
Manifest BitLocker {Unlock-BitLocker, Suspend-BitLocker, Resume-BitLocker, Remove-BitLoc…
Manifest BitsTransfer {Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer, Remove-BitsTr…
Manifest BranchCache {Add-BCDataCacheExtension, Clear-BCCache, Disable-BC, Disable-BCDowng…
Manifest CimCmdlets {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSes…
Binary ClusterAwareUpdating {Get-CauPlugin, Register-CauPlugin, Unregister-CauPlugin, Invoke-CauS…
Manifest DFSN {Get-DfsnRoot, Remove-DfsnRoot, Set-DfsnRoot, New-DfsnRoot…}
Manifest DhcpServer {Add-DhcpServerInDC, Add-DhcpServerv4Class, Add-DhcpServerv4Exclusion…
Manifest DirectAccessClientComponents {Disable-DAManualEntryPointSelection, Enable-DAManualEntryPointSelect…
Script Dism {Add-AppxProvisionedPackage, Add-WindowsDriver, Add-WindowsPackage, C…
Manifest DnsClient {Resolve-DnsName, Clear-DnsClientCache, Get-DnsClient, Get-DnsClientC…
Manifest DnsServer {Add-DnsServerConditionalForwarderZone, Add-DnsServerDirectoryPartiti…
Manifest FailoverClusters {Add-ClusterCheckpoint, Add-ClusterDisk, Add-ClusterFileServerRole, A…
Manifest GroupPolicy {Backup-GPO, Block-GPInheritance, Copy-GPO, Get-GPInheritance…}
Manifest International {Get-WinDefaultInputMethodOverride, Set-WinDefaultInputMethodOverride…
Manifest iSCSI {Get-IscsiTargetPortal, New-IscsiTargetPortal, Remove-IscsiTargetPort…
Manifest IscsiTarget {Add-ClusteriSCSITargetServerRole, Add-IscsiVirtualDiskTargetMapping,…
Script ISE {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}
Manifest Kds {Add-KdsRootKey, Get-KdsRootKey, Test-KdsRootKey, Set-KdsConfiguratio…
Manifest Microsoft.PowerShell.Diagnostics {Get-WinEvent, Get-Counter, Import-Counter, Export-Counter…}
Manifest Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path…}
Manifest Microsoft.PowerShell.Security {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential…}
Manifest Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide…}
Manifest Microsoft.WSMan.Management {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredSSP, Set-WSM…
Manifest MMAgent {Disable-MMAgent, Enable-MMAgent, Set-MMAgent, Get-MMAgent}
Manifest MsDtc {New-DtcDiagnosticTransaction, Complete-DtcDiagnosticTransaction, Joi…
Manifest NetAdapter {Disable-NetAdapter, Disable-NetAdapterBinding, Disable-NetAdapterChe…
Manifest NetConnection {Get-NetConnectionProfile, Set-NetConnectionProfile}
Manifest NetLbfo {Add-NetLbfoTeamMember, Add-NetLbfoTeamNic, Get-NetLbfoTeam, Get-NetL…
Manifest NetQos {Get-NetQosPolicy, Set-NetQosPolicy, Remove-NetQosPolicy, New-NetQosP…
Manifest NetSecurity {Get-DAPolicyChange, New-NetIPsecAuthProposal, New-NetIPsecMainModeCr…
Manifest NetSwitchTeam {New-NetSwitchTeam, Remove-NetSwitchTeam, Get-NetSwitchTeam, Rename-N…
Manifest NetTCPIP {Get-NetIPAddress, Get-NetIPInterface, Get-NetIPv4Protocol, Get-NetIP…
Manifest NetworkConnectivityStatus {Get-DAConnectionStatus, Get-NCSIPolicyConfiguration, Reset-NCSIPolic…
Manifest NetworkLoadBalancingClusters {Add-NlbClusterNode, Add-NlbClusterNodeDip, Add-NlbClusterPortRule, A…
Manifest NetworkTransition {Add-NetIPHttpsCertBinding, Disable-NetDnsTransitionConfiguration, Di…
Manifest NFS {Get-NfsMappedIdentity, Get-NfsNetgroup, Install-NfsMappingStore, New…
Manifest PKI {Add-CertificateEnrollmentPolicyServer, Export-Certificate, Export-Pf…
Manifest PrintManagement {Add-Printer, Add-PrinterDriver, Add-PrinterPort, Get-PrintConfigurat…
Script PSDiagnostics {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-WSManTrace, E…
Binary PSScheduledJob {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger…}
Manifest PSWorkflow {New-PSWorkflowExecutionOption, New-PSWorkflowSession, nwsn}
Manifest PSWorkflowUtility Invoke-AsWorkflow
Manifest RemoteAccess {Add-DAAppServer, Add-DAClient, Add-DAClientDnsConfiguration, Add-DAE…
Manifest RemoteDesktop {Get-RDCertificate, Set-RDCertificate, New-RDCertificate, New-RDVirtu…
Manifest ScheduledTasks {Get-ScheduledTask, Set-ScheduledTask, Register-ScheduledTask, Unregi…
Manifest SecureBoot {Confirm-SecureBootUEFI, Set-SecureBootUEFI, Get-SecureBootUEFI, Form…
Script ServerManager {Get-WindowsFeature, Install-WindowsFeature, Uninstall-WindowsFeature…
Manifest SmbShare {Get-SmbShare, Remove-SmbShare, Set-SmbShare, Block-SmbShareAccess…}
Manifest SmbWitness {Get-SmbWitnessClient, Move-SmbWitnessClient, gsmbw, msmbw}
Manifest Storage {Add-InitiatorIdToMaskingSet, Add-PartitionAccessPath, Add-PhysicalDi…
Manifest TroubleshootingPack {Get-TroubleshootingPack, Invoke-TroubleshootingPack}
Manifest TrustedPlatformModule {Get-Tpm, Initialize-Tpm, Clear-Tpm, Unblock-Tpm…}
Manifest UpdateServices {Add-WsusComputer, Approve-WsusUpdate, Deny-WsusUpdate, Get-WsusClass…
Manifest VpnClient {Add-VpnConnection, Set-VpnConnection, Remove-VpnConnection, Get-VpnC…
Manifest Wdac {Get-OdbcDriver, Set-OdbcDriver, Get-OdbcDsn, Add-OdbcDsn…}
Manifest WindowsDeveloperLicense {Get-WindowsDeveloperLicense, Show-WindowsDeveloperLicenseRegistratio…
Script WindowsErrorReporting {Enable-WindowsErrorReporting, Disable-WindowsErrorReporting, Get-Win…


Directory: C:\Program Files\Common Files\Microsoft Lync Server 2010\Modules


ModuleType Name ExportedCommands
---------- ---- ----------------
Manifest Lync {Get-CsAddressBookConfiguration, Set-CsAddressBookConfiguration, Remo…

Name : Microsoft.PowerShell.Core
IsDefault : True
ApplicationBase : C:\Windows\System32\WindowsPowerShell\v1.0
AssemblyName : System.Management.Automation, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL
ModuleName : C:\Windows\System32\WindowsPowerShell\v1.0\System.Management.Automation.dll
PSVersion : 3.0
Version : 3.0.0.0
Types : {types.ps1xml, typesv3.ps1xml}
Formats : {Certificate.format.ps1xml, DotNetTypes.format.ps1xml, FileSystem.format.ps1xml,
Help.format.ps1xml…}
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows
PowerShell.
Vendor : Microsoft Corporation
LogPipelineExecutionDetails : False


Name : Microsoft.Exchange.Management.PowerShell.E2010
IsDefault : False
ApplicationBase : C:\Program Files\Microsoft\Exchange Server\V14\bin
AssemblyName : Microsoft.Exchange.PowerShell.Configuration, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
ModuleName : C:\Program Files\Microsoft\Exchange
Server\V14\bin\Microsoft.Exchange.PowerShell.Configuration.dll
PSVersion : 1.0
Version : 14.0.0.0
Types : {}
Formats : {}
Description : Admin Tasks for the Exchange Server
Vendor : Microsoft Corporation
LogPipelineExecutionDetails : False

Description: This tool’s commands suite allow you to query the directory
according to specified criteria. Each of the following dsquery commands finds
objects of a specific object type, with the exception of dsquery *, which can
query for any type of object:

dsquery computer - finds computers in the directory.
dsquery contact - finds contacts in the directory.
<snip>


PS C:_AdminTools\test>
by masquap at 2013-02-17 14:27:13
No one has ever tried to run a PowerShell script that loads modules from a scheduled task?