如何在文件服务器资源管理器分类脚本中使用的 powershell 脚本中执行导入模块

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

我正在尝试在 windows 服务器 2012 R2 上的文件服务器资源管理器的分类规则中使用活动目录 powershell 模块。

当我尝试执行时:

Import-Module ActiveDirectory

它将崩溃(我假设)并且不再更新分类 属性。

我尝试设置脚本参数 -ExecutionPolicy Unrestricted,但这没有帮助。

有人知道如何让它工作吗?

提前致谢,

埃里克

更新添加的非工作代码:

# 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";
    }
}

注意:这在 powershell 控制台中工作得很好,这不是问题所在。 运行将代码用作文件服务器资源管理器的分类器。

我现在通过使用 Get-ADUser 的结果创建一个 CSV 文件并将其加载到脚本中来解决这个问题(所以我不需要任何非标准模块)。但是,如果不依赖于某些外部任务,只 运行 会更好:-)

已解决(现在感觉有点笨:-(),仅供有相同问题的人参考。

分类脚本是从文件服务器资源管理器服务(不是您正在查看的 UI)执行的,它是 运行 系统帐户下的。

所以你要么修改服务在哪个账户下运行要么赋予账户访问你需要访问的对象的权限。在我的例子中是 Active Directory。