2017-05-09 78 views
0

劇本我想建立一個小型webinterface給予我們的用戶處理鏈接克隆某些權限。 在網絡服務器(運行IIS的Windows Datacenter 2008 R2)上,我安裝了PowerCLI。該網站在我的用戶帳戶(域管理員)下運行,以排除任何權限問題。呼叫的PowerCLI從PHP

我的PHP文件看起來像這樣:

$PowerCliCommand="C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe -PSConsoleFile \"C:\\Progra~2\\VMware\\Infrastructure\\vSphere PowerCLI\\vim.psc1\" -file C:\inetpub\lcmgmt\listlcs.ps1"; 
echo $PowerCliCommand . "<br>"; 
exec($PowerCliCommand, $Output, $ErrorReturned); 
echo "<pre>"; 
print_r($Output); 

print_r($ErrorReturned); 
echo "</pre>"; 

我叫consolefile從\progra~2,爲\program files (x86)不停地纏着我的問題。

PHP在我的瀏覽器的輸出如下:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile C:\Progra~2\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -file C:\inetpub\lcmgmt\listlcs.ps1 
Array 
( 
    [0] => Connect-VIserver : The term 'Connect-VIserver' is not recognized as 
> the name of 
    [1] => a cmdlet, function, script file, or operable program. Check the spelling of th 
    [2] => e name, or if a path was included, verify that the path is correct and try agai 
    [3] => n. 
    [4] => At C:\inetpub\lcmgmt\listlcs.ps1:4 char:2 
    [5] => + Connect-VIserver -server "MyVSserver" 

PowerShell腳本叫這個樣子的:

Connect-VIserver -server "MyVSserver" 
$VDICluster=get-cluster -Name "MyCluster" 
get-vm -location $VDICluster -name "Server*" 

當調用完整的命令(在我的PHP輸出的第一行)從Web服務器上的命令提示符處運行,並在我的帳戶(IIS中使用的相同帳戶)上運行,一切正常,我可以獲取關於鏈接克隆的信息。 正如您所看到的,瀏覽器輸出報告PowerCLI命令未知。 我完全失去了,任何人都有這方面的經驗?

+0

「在IIS中使用相同的帳戶」?你的意思是你在正常的用戶帳戶下操作該deamon? – arkascha

+0

這聽起來像是PATH環境變量的問題。 – arkascha

+0

是的,我運行(純粹作爲測試,我將創建一個專門的服務帳戶,一旦他的作品)我的個人域管理員帳戶下的IIS網站排除任何權限限制。 我看不到路徑如何在這裏,我指定所有文件調用的完整路徑。 –

回答

1

看來在VMware的PowerCLI安裝程序沒有正確地添加自己的用戶/機器的環境變量,這是用來填充$ ENV:PSModulePath時PowerShell的負荷。這裏存儲的路徑決定了PowerShell可以在哪裏查找模塊,也有助於cmdlet自動加載。

要在跨會話持續的方式解決這個問題,你可以運行以下命令:

$Current = [Environment]::GetEnvironmentVariable('PSModulePath','Machine') 
$Current += ';C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules' 
[Environment]::SetEnvironmentVariable('PSModulePath',$Current,'Machine') 

的獲取PSModulePath在系統級水平的現有內容,如你在這裏看到:

enter image description here

並附加VMware PowerCLI模塊路徑,最後通過調用SetEnvironmentVariable方法進行設置。

+0

我跑了你建議改變Env變量的PS1腳本。 但是,Env變量未被更改。 我的PS1腳本如下所示:'$ Current = [Environment] :: GetEnvironmentVariable('PSModulePath','Machine') $ Current + ='; C:\ Program Files(x86)\ VMware \ Infrastructure \ vSphere PowerCLI \模塊' [環境] :: SetEnvironmentVariable('PSModulePath',$ Current,'Machine') write-host($ env:psmodulepath)',它只輸出 C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules \ –

+0

我應該提到 - 你只需要運行一次,以管理員身份提升PowerShell。在您的PowerCLI腳本中,如果它仍然輸出最後一行,則嘗試使第一行爲'$ env:PSModulePath = $ env:PSModulePath +'; C:\ Program Files(x86)\ VMware \ Infrastructure \ vSphere PowerCLI \ Modules '' - 我們在PS1腳本中使用這種格式的行來添加一個自定義模塊位置,該位置位於通常區域之外,並且運行良好。除此之外,我只能認爲這是與服務器OS/IIS/WMF版本有關...(如果可以的話,總是升級WMF)。 – Robin

+0

從高架PowerShell運行路徑擴展命令不會改變Web輸出中的任何內容。 將'$ env:PSModulePath = $ env:PSModulePath +'; C:\ Program Files(x86)\ VMware \ Infrastructure \ vSphere PowerCLI \ Modules''添加到我的腳本中,但是解決了所有問題。我現在可以從PHP運行PowerCli命令。 非常感謝! –