locked
Help with workflow RRS feed

  • Question

  • Hi everyone, I'd like some help with workflows - I'm new to them.  Here's what I have.  Just trying to run a BPA scan of the AD DS model and export the results to a file, for each DC in my domain.  Unfortunately the results are not what I expected.  What I got are a bunch of files (expected) just named Server01 ADDS .csv, Server02 ADDS .csv (unexpected), and the contents of every one of those files are the exact same (unexpected), including the Source and ComputerName attributes.  So that right there was the smoking gun on how I knew this definitely didn't work even though I all of the data that I was (somewhat) expecting.  Here's the code that I have.

    $SystemTime = Get-Date -Format yyyy-MM-dd
    $DCs = (Get-ADComputer -Filter * -SearchBase 'OU=Domain Controllers,DC=abc,DC=Com').Name
    
    workflow BPA_WF
    {
        param($Computers)
        foreach –parallel ($Computer in $Computers)
        {
            sequence
            {
                $BPAType = 'ADDS'
                $Results = Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -ComputerName $Computer
                $Results | Get-BpaResult -Filter Noncompliant | Export-Csv -Path ".\InvokeBPAScansLogs\$Computer $BPAType $SystemTime.csv”
            }
        }
    }

    and then the command that I use to execute the workflow was:

    BPA_WF -Computers $DCs

    So a few things here:

    1. It seems as though the workflow completely ignored the $SystemTime, is that due to scope?

    2. The results (contents) of each file were the exact same, including the Source and ComputerName attributes.  No idea about that one.

    Any help is definitely appreciated!

    Friday, October 27, 2017 10:01 PM

All replies

  • Hi Thomas,

    Based on my research, you could have a try to change the order of the code to make the script works properly, for your reference:
    $DCs = (Get-ADComputer -Filter * -SearchBase 'OU=Domain Controllers,DC=abc,DC=com').Name
    
    workflow BPA_WF 
    {
        param ($computers)
        foreach ($Computer in $Computers)
        {
            $BPAType = 'ADDS'
            $SystemTime = Get-Date -Format yyyy-MM-dd
            $Results = Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -ComputerName $Computer
            $Results | Get-BpaResult -Filter Noncompliant | Export-Csv -Path ".\InvokeBPAScansLogs\$Computer $BPAType $SystemTime.csv"
        }
    }
    
    BPA_WF -Computers $DCs

    Also, there might have no needs to achieve this by using workflow, the following script might be more convenient, hope it is helpful to you:
    $dcs = Get-ADDomainController -Filter *
    $BPAType = 'ADDS'
    $SystemTime = Get-Date -Format yyyy-MM-dd
    foreach ($dc in $dcs)
    {
        $results = Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -ComputerName $dc
        $results | Get-BpaResult -Filter Noncompliant | Out-File ".\InvokeBPAScansLogs\$dc $BPAType $SystemTime.txt"
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

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

    Monday, October 30, 2017 7:02 AM
  • The reason why I would like to use a workflow is that BPA scans typically take 20-40 mins each, and I have a lot of DCs.  So I would like the scans to run concurrently.

    Thanks, I'll give your first suggestion a shot, but I was under the impression that workflows will return results in a non-consistent manner.  You would get results in different a different order every time you run it, hence the need for the sequence block.

    Monday, October 30, 2017 12:54 PM
  • "sequence" has no effect on the order here and serves no purpose.  Don't use "sequence."

    "-ComputerName" should be "-PsComputherName"


    \_(ツ)_/

    Monday, October 30, 2017 1:22 PM
  • Thanks jrv.  It seems that -PSComputerName isn't a valid parameter.  Intellisense in the ISE doesn't provide it as an option.  Additionally when I try to run it, I get the below results.  Also as reference here's what my code looks like now.

    $DCs = (Get-ADComputer -Filter * -SearchBase 'OU=Domain Controllers,DC=abc,DC=Com').Name
    
    workflow BPA_WF
    {
        param($Computers)
        foreach –parallel ($Computer in $Computers)
        {
            $SystemTime = Get-Date -Format yyyy-MM-dd
            $BPAType = 'ADDS'
            $Results = Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -PSComputerName $Computer
            $Results | Get-BpaResult -Filter Noncompliant | Export-Csv -Path ".\InvokeBPAScansLogs\$Computer $BPAType $SystemTime.csv” -NoTypeInformation
        }
    }

    PS C:\scripts\MonthlyADMaintenanceScripts> BPA_WF -Computers $DCs
    Microsoft.PowerShell.Utility\Write-Error : A parameter cannot be found that matches parameter name 'PSComputerName'.
    At BPA_WF:10 char:10
    + 
        + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException
        + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand
        + PSComputerName        : [localhost]
     
    Microsoft.PowerShell.Utility\Write-Error : A parameter cannot be found that matches parameter name 'PSComputerName'.
    At BPA_WF:10 char:10
    + 
        + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException
        + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand
        + PSComputerName        : [localhost]
     
    Microsoft.PowerShell.Utility\Write-Error : A parameter cannot be found that matches parameter name 'PSComputerName'.
    At BPA_WF:10 char:10
    + 
        + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException
        + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand
        + PSComputerName        : [localhost]
    When I run it with just -ComputerName, I don't get the error about the parameter not finding a match.  So I've reverted that change now, so the only changes to my original post that I've made is that I've completely removed the Sequence block and moved the $systemdate variable to the inside of the workflow.  The file names are now what I expect them to be, but the contents are still unchanged.  Different file names that relate to the server names, but the content inside of each of them reference the wrong DC.

    Monday, October 30, 2017 2:16 PM
  • If the command does not support workflow then it may not work correctly.  You can create it as an "inline" script to see if that helps.


    \_(ツ)_/

    Monday, October 30, 2017 2:21 PM
  • I ended up having to specify the entire path for export-csv instead of using the relative path with .\, assuming I did the inline script correctly, but getting a totally different issue now.

    Should I just go down the route of starting separate powershell processes instead of trying to use a workflow?

    $DCs = (Get-ADComputer -Filter * -SearchBase 'OU=Domain Controllers,DC=abc,DC=Com').Name
    
    workflow BPA_WF
    {
        param($Computers)
        foreach –parallel ($Computer in $Computers)
        {
            InlineScript
            {
                $SystemTime = Get-Date -Format yyyy-MM-dd
                $BPAType = 'ADDS'
                $Results = Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -ComputerName $using:Computer
                $Results | Get-BpaResult -Filter Noncompliant | Export-Csv -Path "C:\scripts\MonthlyADMaintenanceScripts\InvokeBPAScansLogs\$using:Computer $BPAType $SystemTime.csv” -NoTypeInformation
            }
        }
    }

    The error is as follows, as returned on each object in foreach loop:

    WARNING: [localhost]:Cannot find the Windows PowerShell data file 'DirectoryServices.psd1' in directory 'C:\Users\ttyg3da\Documents\en-US\', or in any parent culture directories.
    Get-BpaResult : Index was outside the bounds of the array.
    At BPA_WF:8 char:8
    + 
        + CategoryInfo          : NotSpecified: (:) [Get-BpaResult], IndexOutOfRangeException
        + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.BestPractices.Cmdlets.GetResultCommand
        + PSComputerName        : [localhost]
    I end up with a file with no file contents for each object in the array.

    Monday, October 30, 2017 4:04 PM
  • This might work better.

    InlineScript {
    	$SystemTime = Get-Date -Format yyyy-MM-dd
    	$BPAType = 'ADDS'
    	$csvfile = "C:\scripts\MonthlyADMaintenanceScripts\InvokeBPAScansLogs\$using:Computer $BPAType $SystemTime.csv"
    	Invoke-BpaModel -ModelId 'Microsoft/Windows/DirectoryServices' -ComputerName $using:Computer |
    		Get-BpaResult -Filter Noncompliant | 
    		Export-Csv $csvfile -NoType
    }
    


    \_(ツ)_/

    Monday, October 30, 2017 5:00 PM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 1, 2017 7:32 AM