none
Executing powershell script from php page with PSSession RRS feed

  • Question

  • Hello everyone,

    I am encountering a problem to execute a ps script from my web page. I use IIS web server with php.

    My web page allow to create new users in the Active Directory (not the same server than IIS) with my ps script. When I use the script manually (ps CLI), users are created. From the exec() fonction of php, users aren't created. This is the error returned from php exec function:

    Array ( [0] => New-PSSession : [****] La connexion au serveur distant **** a �chou� [1] => avec le message d'erreur suivant: Une ouverture de session sp�cifi�e n'existe [2] => pas. Elle est peut-�tre d�j� termin�e. Pour plus d'informations, voir la [3] => rubrique d'aide about_Remote_Troubleshooting. [4] => Au caract�re C:\Users\****\WIFI_INVITE\add_user.ps1:7 [5] => : 6 [6] => + $s = New-PSSession -computerName **** [7] => + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [8] => + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:Re [9] => moteRunspace) [New-PSSession], PSRemotingTransportException [10] => + FullyQualifiedErrorId : 1312,PSSessionOpenFailed [11] => Invoke-Command : Impossible de valider l'argument sur le param�tre �Session�. [12] => L'argument est Null ou vide. Indiquez un argument qui n'est pas Null ou vide [13] => et r�essayez. [14] => Au caract�re C:\Users\****\WIFI_INVITE\add_user.ps1:9 [15] => : 25 [16] => + Invoke-Command -Session $s -Scriptblock $sb [17] => + ~~ [18] => + CategoryInfo : InvalidData : (:) [Invoke-Command], ParameterBin [19] => dingValidationException [20] => + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power [21] => Shell.Commands.InvokeCommandCommand [22] => [23] => Remove-PSSession : Impossible de valider l'argument sur le param�tre �Id�. [24] => L'argument est Null. Sp�cifiez une valeur valide pour l'argument, puis [25] => r�essayez. [26] => Au caract�re C:\Users\****\WIFI_INVITE\add_user.ps1:10 [27] => : 18 [28] => + Remove-PSSession $s [29] => + ~~ [30] => + CategoryInfo : InvalidData : (:) [Remove-PSSession], ParameterB [31] => indingValidationException [32] => + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power [33] => Shell.Commands.RemovePSSessionCommand [34] =>

    )

    This error (french langage), tell : failed to connect to my active directory server. It's seems PSSession doesn't work with php. This script is executed by Administrator (local machine).

    This is my ps script (on the web server):

    Param(
    [string]$nom,
    [string]$prenom,
    [string]$password
    )
    
    $s = New-PSSession -computerName ****
    $sb = [scriptblock]::Create("C:\Users\****\add_user.ps1 -nom $nom -prenom $prenom -password $password")
    Invoke-Command -Session $s -Scriptblock $sb
    Remove-PSSession $s

    Please can you help me ? I am sorry about my bad english langage but i need to fix this error quickly.

    Best regard.

     
    • Edited by Love powershell Thursday, October 19, 2017 8:27 AM
    • Moved by Bill_Stewart Wednesday, November 29, 2017 6:42 PM Unanswerable drive-by question
    Thursday, October 19, 2017 8:24 AM

All replies

  • Param (
    	[string]$nom,
    	[string]$prenom,
    	[string]$password
    )
    
    $sb = {
    	C:\Users\****\add_user.ps1 -nom $using:nom -prenom $using:prenom -password $using:password
    }
    Invoke-Command -Scriptblock $sb -computerName ****


    \_(ツ)_/

    Thursday, October 19, 2017 9:22 AM