none
Get MIMWAL powershell output RRS feed

  • Question

  • Dear All,

    We are trying to move AD users from one OU to other OU using MIMWAL PowerShell getting error.

    ----------------------------------------------------------------

    param
    (
    [parameter(mandatory = $true)] $AccountName
    )

    function InvokeImmediateTermination
    {
        $stdOutFile = Join-Path $env:TEMP -ChildPath "StdOut_$AECRequestId.log"
        $stdErrFile = Join-Path $env:TEMP -ChildPath "StdErr_$AECRequestId.log"

        $command = @"
        & {
            if (!(Get-Module -Name "ActiveDirectory"))
            {
                Import-Module ActiveDirectory
            }

            $UserDN = (Get-ADUser -Identity $AccountName).Distinguishedname

            Return $UserDN

            if (`$Error.Count -eq 0)
            {
                "!!Success!!"
            }
            else
            {
         # Can't easily use stdErrFile as it comes as CLIXML
    # and includes Warning/Verbose/Debug streams as well.
    # So we'll simply use stdOutFile
                `$Error
            }
        }
    "@

        Write-Debug $command

        $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($command)
        $encodedCommand = [Convert]::ToBase64String($commandBytes)

        Start-Process 'PowerShell.exe' `
            -ArgumentList "-Version 3.0 -NonInteractive -OutputFormat Text -EncodedCommand $encodedCommand" `
            -RedirectStandardOutput $stdOutFile `
            -RedirectStandardError $stdErrFile `
            -Wait

        $statusLine = Get-Content $stdOutFile| Select-Object -Last 2 | ? {
            $_.Contains("!!Success!!")
        }
           
        if ([string]::IsNullOrEmpty($statusLine))
        {
            # Communicate error back the parent workflow to abort the workflow
            throw $((Get-Content $stdOutFile) -join "`r`n")
        }
        else
        {
            Write-Debug "Script executed successfully."
        }

        Remove-Item $stdOutFile -Force
        Remove-Item $stdErrFile -Force
    }

    InvokeImmediateTermination

    ---------------------------------------------------------------

    Need your help!

    Thanks,

    Shashidhar

    Tuesday, April 10, 2018 5:09 PM

Answers

  • The following PowerShell cmdlets used to get AD User distinguishedName

    ======================================

    param
    (
     [parameter(mandatory = $true)] $AccountName
    )

    Function GetADObject

    {


    #$strFilter = $AccountName

    $strFilter = “((samaccountname=$AccountName))”

    $objDomain=$null

    $objSearcher=$null

    Try{

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher

    $objSearcher.SearchRoot = $objDomain

    $objSearcher.PageSize = 1000

    $objSearcher.Filter = $strFilter

    $objSearcher.SearchScope = “Subtree”

    $existingObject = $objSearcher.FindAll()

    If ($existingObject.count -ne 1)

    {

    throw (“Error getting the user in AD, User not found or more than one: ” + $strFilter)

    }

    Else

    {

    #This works only in PS 3 or greater – $myuser=$existingObject.GetDirectoryEntry()

    #use this –

    [String]$adspath=$existingobject[0].path

    $MyUser=[ADSI](“$ADSPath”)

    $userDN = $MyUser.distinguishedName

    Return $userDN

    }

    }

    Finally{

    #Dispose the searcher to prevent memory leak

    if ($objDomain -ne $null)

    {

    $objDomain.Dispose()

    }

    if ($objSearcher -ne $null)

    {

    $objSearcher.Dispose()

    }

    }

    }

    GetADObject



    • Marked as answer by Shashidhar J Wednesday, April 11, 2018 10:04 AM
    • Edited by Shashidhar J Wednesday, April 11, 2018 10:05 AM
    Wednesday, April 11, 2018 10:02 AM

All replies

  • Hello, are you expecting someone to do a code review for you? It will help if you demonstrate that you have done some level of troubleshooting :) While it's easy to spot the error in your script, have you even understood what the script does? Go look at the file mentioned in the $stdErrFile for detailed error, run the script in ISE and try, ook at the error on the workflow and so on. Hopefully you'll learn to catch fish yourself :)
    Wednesday, April 11, 2018 8:12 AM
    Owner
  • The following PowerShell cmdlets used to get AD User distinguishedName

    ======================================

    param
    (
     [parameter(mandatory = $true)] $AccountName
    )

    Function GetADObject

    {


    #$strFilter = $AccountName

    $strFilter = “((samaccountname=$AccountName))”

    $objDomain=$null

    $objSearcher=$null

    Try{

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher

    $objSearcher.SearchRoot = $objDomain

    $objSearcher.PageSize = 1000

    $objSearcher.Filter = $strFilter

    $objSearcher.SearchScope = “Subtree”

    $existingObject = $objSearcher.FindAll()

    If ($existingObject.count -ne 1)

    {

    throw (“Error getting the user in AD, User not found or more than one: ” + $strFilter)

    }

    Else

    {

    #This works only in PS 3 or greater – $myuser=$existingObject.GetDirectoryEntry()

    #use this –

    [String]$adspath=$existingobject[0].path

    $MyUser=[ADSI](“$ADSPath”)

    $userDN = $MyUser.distinguishedName

    Return $userDN

    }

    }

    Finally{

    #Dispose the searcher to prevent memory leak

    if ($objDomain -ne $null)

    {

    $objDomain.Dispose()

    }

    if ($objSearcher -ne $null)

    {

    $objSearcher.Dispose()

    }

    }

    }

    GetADObject



    • Marked as answer by Shashidhar J Wednesday, April 11, 2018 10:04 AM
    • Edited by Shashidhar J Wednesday, April 11, 2018 10:05 AM
    Wednesday, April 11, 2018 10:02 AM