从 IIS 执行 powershell 脚本时找不到命令

Command not found when execute powershell script from IIS

我有一个脚本可以获取我们平台上关系的 citrix 会话。

问题是当我从网络服务器上的控制台运行脚本时,脚本执行成功并且结果符合预期。 当我 运行 来自我的网站(托管在 IIS 上)的脚本时,结果如下:

"The term 'Get-XASession' is not recognized as the name of a cmdlet, function, script file, or operable program."

这是脚本:

    #---------------------------------------------------------- 
    # PARAMS TO CALL SCRIPT WITH
    #---------------------------------------------------------- 
    Param(
        [string]$relationId,
        [string]$xaVersion,
        [string]$xaServer
    )

    #---------------------------------------------------------- 
    # LOAD ASSEMBLIES AND MODULES 
    #---------------------------------------------------------- 
    try
    {
        Import-Module "C:\Program Files\Citrix\XenApp 6.5 Server SDK\Citrix.XenApp.Sdk.ps1"

        . "D:\scripts\include\functions.ps1"
    }
    catch
    {
        return "[ERROR]`t Import module XenApp 6.5 Server SDK gives an error  $($_.Exception)"
    }

    #---------------------------------------------------------- 
    #START 
    #---------------------------------------------------------- 
    Set-ExecutionPolicy Unrestricted

    try
    {
        if ($xaVersion -eq "XA65")
        {
            $sessions = Get-XASession -ComputerName $xaServer | Where-Object { $_.AccountName -like "ASPECT$relationId*" }
            $listSessions = @()
            foreach($se in $sessions)
            {
                New-Object psobject -Property @{AccountName = $se.AccountName; SessionId = $se.SessionId;  Name = $se.BrowserName; ClientName = $se.ClientName; ServerName = $se.ServerName; LogonTime = $se.LogonTime; Status = $se.State}
            }
            return $listSessions
        }
    }
    catch
    {
        return "[ERROR]`t Sessies ophalen voor gebruiker $samAccountName met XAversie $xaVersion gives the following error $($_.Exception)"
    }

IIS 应用程序池的标识与我用于从控制台运行 脚本的标识相同。 该用户拥有访问 Citrix XenApp 模块所需的所有权限。

奇怪的是,当我说 Get-Command Get-XASession -neq $null 然后脚本说,我知道命令让我们去执行它。当它要执行它时,脚本会说,是吧 Get-XASession?从来没有伤害过。

我花了几个小时,但我一点头绪都没有。

请帮帮我!

jisaak 确实指引了我正确的方向。

点源可能是答案,但这会导致用户交互错误。

这就是我所做的:我打开模块并将除 write-host 行之外的所有内容复制到我的脚本中。我做了 运行,你猜怎么着,它有效!

所以如果你遇到和我一样的问题: 1. 尝试点采购 2. 尝试将模块的内容复制到您的脚本中。

所有学分归于 jisaak (Y) :)