2014-02-27 230 views
1

我有一個powershell腳本,它使用'quser'命令來提取有關用戶登錄到一系列終端服務器的數據。從windows批處理腳本傳遞參數到powershell腳本

我想給輸出文件添加一個時間戳,這個時間戳變量是在一個windows批處理文件中創建的,然後調用powershell腳本傳遞計算機名和時間戳,但powershell腳本與'Missing')'錯誤功能參數表」

param(
[CmdletBinding()] 
[Parameter(ValueFromPipeline=$true, 
      ValueFromPipelineByPropertyName=$true)] 
[string[]]$ComputerName = 'localhost' 
[string[]]$timestamp <========= this is the line I have added 
) 

如果我刪除我添加的行(標註在上面的代碼),腳本運行良好

回答

1

您需要添加參數之間的逗號:

param(
[CmdletBinding()] 
[Parameter(ValueFromPipeline=$true, 
      ValueFromPipelineByPropertyName=$true)] 
[string[]]$ComputerName = 'localhost', 
[string[]]$timestamp 
) 

另外,除非你想要多個時間戳,你可能只是希望它是一個字符串而不是一個字符串數組(所以[string]$timestamp)。

我得到的錯誤信息看起來像這樣(除了它是紅色的)。在本地主機行末尾的第一個錯誤點則是由當時似乎是一個僞)一個連鎖的錯誤:

PS C:\>  param(
>> [CmdletBinding()] 
>> [Parameter(ValueFromPipeline=$true, 
>>   ValueFromPipelineByPropertyName=$true)] 
>> [string[]]$ComputerName = 'localhost' 
>> [string[]]$timestamp 
>>) 
>> 
At line:5 char:38 
+ [string[]]$ComputerName = 'localhost' 
+          ~ 
Missing ')' in function parameter list. 
At line:7 char:1 
+) 
+ ~ 
Unexpected token ')' in expression or statement. 
    + CategoryInfo   : ParserError: (:) [], ParentContainsErrorRecordException 
    + FullyQualifiedErrorId : MissingEndParenthesisInFunctionParameterList 

我使用PowerShell 3在這裏。其他版本可能會顯示不同的錯誤。

+0

謝謝:)大量Doh對我來說:( – nyehus

+0

沒問題。順便說一句,錯誤信息應該顯示你本地主機和'〜'在問題出現的地方(即逗號丟失的地方)。你使用ISE來編輯腳本,它在編輯器中顯示一個紅色的'〜'下劃線,錯誤號是 – Duncan

+0

沒有看到 - 這是整個消息 - 'Missing')'在函數參數列表中。 在C:\ ts_users \ GET-LoggedOnUser.ps1:35焦炭:5 + <<<< [字符串] $時間戳 + CategoryInfo:ParserError:(CloseParenToken:TokenId)[],括號 tContainsErrorRecordException + FullyQualifiedErrorId:MissingEndParenthesisInFunctionParameterList ',我想<<<<表明錯誤的位置是 – nyehus

相關問題