Search code examples
powershellwindows-server-2012-r2

How to perform Import-Module in powershell script used in File Server Resource Manager Classification script


I am trying to use the active directory PowerShell module inside a classification rule in File server resource manager on windows server 2012 R2.

When I try to just perform:

Import-Module ActiveDirectory

It will crash (I assume) and not update the classification property anymore.

I tried setting the script parameter -ExecutionPolicy Unrestricted, but that didn't help.

Anyone know how to get it to work?

non working code:

# Global variables available:
# $ModuleDefinition  (IFsrmPipelineModuleDefinition)
# $Rule  (IFsrmClassificationRule)
# $PropertyDefinition  (IFsrmPropertyDefinition)
#
# And (optionally) any parameters you provide in the Script parameters box below,
# i.e. "$a = 1; $b = 2" . The string you enter is treated as a script and executed so the
# variables you define become globally available

# optional function to specify when the behavior of this script was last modified
# if it consumes additional files. emit one value of type DateTime
#
# function LastModified
# {
# }

# required function that outputs a value to be assigned to the specified property for each file classified
# emitting no value is allowed, which causes no value to be assigned for the property
# emitting more than one value will result in errors during classification
# begin and end are optional; process is required
#
function GetPropertyValueToApply
{
    # this parameter is of type IFsrmPropertyBag
    # it also has an additional method, GetStream, which returns a IO.Stream object to use for
    # reading the contents of the file. Make sure to close the stream after you are done reading
    # from the file
    param
    (
        [Parameter(Position = 0)] $PropertyBag
    )
    
    process
    {
        Import-Module activedirectory   
        $users = Get-ADUser -filter * -Properties SID,Department
        return "dummy result";
    }
}

As note: This works perfectly fine in a PowerShell console; that isn't the issue. It's running the code as classifier for the file server resource manager.

Worked my way around it now by just creating a CSV file with the result of the Get-ADUser and loading that inside the script for now (so I don't require any non standard modules). But it would be nicer to just run this without a dependency on some external task.


Solution

  • The classification script is executed from a the File Server Resource Manager Service (not from the UI you are looking at), which is running under the system account.

    So you either need to modify under which account the service is running or give the account rights to access the objects you require to access. In my case Active Directory.