Active Roles - powershell script module: "The Expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object"

I have a simple script that basically queries an OU in AD, obtains the samaccountnames of those users. And then deletes the contents of the users folder based on their samaccountname, excluding a few folders etc...

SCRIPT

$OUPath = '<domain name>/Student/ClassRoom9'

Get-QAduser -SearchRoot $OUPath -SearchScope OneLevel | Select-Object "samaccountname" | ForEach-Object { $Student = $_.samaccountname $studpath = "d:\script\$Student"

 

Remove-Item -recurse $studpath\* -exclude Desktop,Documents,Favorites,Music,Pictures,Videos

}

The script runs perfectly OK, and does the job. But when I view the "Run History". I get a "STATUS: FAILED"

And then the error as in the subject "The Expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object"

Anyone any ideas?

  • Obviously I'm new using Active Roles. Basically you need to make sure when creating the "Script Activity" make sure you select a "Function to run" and in the drop down add "onInit". So when you view the Script module, you will find:

    function onInit ($Request)

    {

    }

    Just keep that in the code, and it will stop the error from appearing.

  • Be careful with "OnInit".  If you are just using the script in a straight up script activity, it's not so important but OnInit when used in a script policy has a very specific meaning / usage.

    My Best Practice for Script Activities is to use a descriptive name for the function - i.e. on that describes its purpose.

  • The OnInit Event Handler, especially, can be dangerous.

    All of the code in an OnInit function executes every single time the script module is save and also when the Active Roles Administration Service is restarted. The script module does not have to be linked to a Policy or used in a Workflow or Scheduled Task. It just needs to be present in the configuration. If you have something bad in an OnInit function, you may need to start the Active Roles Administration Service in Safe Mode in order to remove it and fix the issue.