2016-09-10 22 views
1

我與調用命令奮力遠程執行自定義函數,並顯示在本地主機的輸出來執行。我已經閱讀了很多關於invoke-Command的帖子,但是我還沒有找到解決這個問題的方法。PowerShell的:使用調用命令的自定義功能在遠程計算機和顯示輸出在主機

首先自定義函數正在工作。它被稱爲get-openfiles。代碼如下:

function get-openfiles{                     
param(                         
[email protected]($env:computername),                  
$verbose=$false)                       
$collection = @()                       
foreach ($computer in $computername){                  
$netfile = [ADSI]"WinNT://$computer/LanmanServer"               

$netfile.Invoke("Resources") | foreach {                 
try{                          
$collection += New-Object PsObject -Property @{               
Id = $_.GetType().InvokeMember("Name", ‘GetProperty’, $null, $_, $null)         
itemPath = $_.GetType().InvokeMember("Path", ‘GetProperty’, $null, $_, $null)        
UserName = $_.GetType().InvokeMember("User", ‘GetProperty’, $null, $_, $null)        
LockCount = $_.GetType().InvokeMember("LockCount", ‘GetProperty’, $null, $_, $null)      
Server = $computer                      
}                           
}                           
catch{                         
if ($verbose){write-warning $error[0]}                 
}                           
}                           
}                           
Return $collection                      
} 

我已經使用了下面的方法,但都沒有工作。下面的一些例子可以用於內置函數,但是我的條件是我必須使用自定義函數。

# NOT WORKING #1 
$s = New-PSSession -ComputerName remote-STL 
$CifServer = "fs-STL-01" 
function get-openfiles{..} 
invoke-command -Session $s -ScriptBlock ${function:get-openfiles} -ArgumentList $CifServer 

# NOT WORKING #2 
$s = New-PSSession -ComputerName remote-STL 
$CifServer = "fs-STL-01" 
invoke-command -Session $s -ScriptBlock {Import-Module C:\scripts\grp-functions.psm1} 
invoke-command -Session $s -ScriptBlock {get-openfiles -computername $args[0]} -ArgumentList $CifServer 

# Not working #3 
$s = New-PSSession -ComputerName remote-STL 
$CifServer = "fs-STL-01" 
invoke-command -Session $s -ScriptBlock {Import-Module C:\scripts\grp-functions.psm1} 
invoke-command -Session $s -ScriptBlock { param($CifServer) get-openfiles -computername $Cifserver } -ArgumentList $CifServer 

# Not working #4 
$CifServer = "fs-STL-01" 
function get-openfiles{..} 
invoke-command -ComputerName remote-STL -ScriptBlock ${function:get-openfiles} -ArgumentList $CifServe 

# not working #5 
$CifServer = "fs-STL-01" 
invoke-command -ComputerName remote-STL -ScriptBlock {get-openfiles -computername $args[0]} -ArgumentList $CifServer 

感謝您的關注!

+0

兩個使用$ {功能:GET-openFiles散}的(也許)發送的函數定義,但你這樣做後不調用該函數。我認爲你需要把它和腳本塊合併爲一個參數給cifserver,然後調用get-openfiles – TessellatingHeckler

+0

如果你從第一個示例中刪除了'-Session $ s'(並且首先創建會話),並且交換它用於'-C計算機名'fs-STL-01',它很可能會工作。該語法適用於我。 – Robin

+0

嗨,羅賓。爲了清楚起見,你能否給我提供適合你的完整語法?我不明白你是如何交換和爲你工作的。 – LeonC

回答

2

你應該在腳本塊中發送整個函數。

invoke-command -ComputerName computername -ScriptBlock {function get-whatever {dir}; get-whatever } -Credential (get-credential youruser) 

這個例子運行只是dir命令的get-whatever函數,它是概念驗證。將其更改爲所需的代碼。

+0

我已經應用於概念驗證。它的工作部分......這意味着遠程TS服務器採用了自定義功能,但它返回了遠程TS服務器的結果,而不是Cifs服務器的結果。這裏是我的代碼: '$ cifServer = 「FS-STL-01」'' $ REMOTESERVER = 「遠程STL」'' $腳本塊= {功能得到-openFiles散{..}; get-openfiles}' 'invoke-command -ComputerName $ remoteServer -ScriptBlock $ scriptblock -ArgumentList $ CifServer' – LeonC

相關問題