SMA Workflow, inline script not functioning. RRS feed

  • Question

  • Hi all.

    I'm struggling with an SMA workflow that is supposed to be called from a parent workflow and return the FQDN of a domain controller in an Active Directory domain.

    This is what it looks like right now, am I doing something obviously bad here?

    Off course $DC = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers works if I run it manually on a domain member computer. The point is that I don't want to be dependent on the ActiveDirectory Powershell module.

    workflow Get-Domaincontroller { [OutputType([string[]])] param( [Parameter(Mandatory=$true)] [string]$CMConnectionName, [PSCredential]$CMCredentialName ) $CMConnection = Get-AutomationConnection -Name $CMConnectionName $CMCredential = get-AutomationPSCredential -Name $CMCredentialName #Construct the Credential $CMSecurepw = ConvertTo-SecureString -AsPlainText -String $CMConnection.Password -Force $CMCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CMConnection.Username, $CMSecurepw inlinescript { try { # Get domaincontrollers in domain, return one. Using ADSI to remove dependency on ActiveDirectory module $DC = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers Write-Output $DC[0].Name } catch { Write-Output "Failed to retreive Domaincontroller" Write-Error $_.Exception } } -PSComputerName $CMConnection.ComputerName -PSCredential $CMCredential }

    When I run it workflow stops at the Catch block with this error: 

    System.Management.Automation.RuntimeException: Cannot index into a null array.
       at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception 
       at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
    At Get-Domaincontroller:13 char:13

        + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
        + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

    Any ideas, thought this should be a simple one, but spent 3 days so far....

    Or, should I do this in another way?

    Sunday, October 4, 2015 7:05 AM


All replies

  • Hmm, maybe I am complecating things, how about this to find a domain controller?

    $DC = ($env:LOGONSERVER).TrimStart("\") + '.' + $env:USERDNSDOMAIN

    Monday, October 5, 2015 7:11 AM
  • Thank you for your sharing.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact

    Wednesday, October 14, 2015 2:17 AM