简体   繁体   中英

Command not found when execute powershell script from IIS

I have a script that gets the citrix sessions for a relation on our platform.

The problem is that when I run the script from the console on the webserver, the executing of the script is succesfull and the result is as expected. When I run the script from my website (hosted on IIS) the result is as follows:

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

This is the script:

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

The identity of the IIS Application Pool is the same as the identity that I used to run the script from the console. That user has all the rights it needs to access the Citrix XenApp module.

The wierd thing is, when I say Get-Command Get-XASession -neq $null then the script says, hé I know that command lets go to execute it. When it is going to execute it then the sripts says, huh Get-XASession? never hurt of.

I spent hours and hours and I don't have a clue.

Please help me!

jisaak did put me in the right direction.

Dot sourcing could be the answer but that gives an error about user interaction.

This is wat I did: I opened the module and copied everything except the write-host lines into my script. I did run it and guess what, it works!

So if you experience the same problem as me: 1. try dot sourcing 2. try to copy the content of the module into your script.

All credits go to jisaak (Y) :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM