繁体   English   中英

将参数从Windows批处理脚本传递到Powershell脚本

[英]passing parameters from a windows batch script into a powershell script

我有一个Powershell脚本,它使用'quser'命令提取有关登录到一系列终端服务器上的用户的数据。

我想在输出文件中添加时间戳,该时间戳变量是在Windows批处理文件中创建的,然后调用powershell脚本传递计算机名和时间戳,但是powershell脚本在函数参数列表中出现“ Missing')'错误

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

如果我删除添加的行(在上面的代码中标记),则脚本可以正常运行

您需要在参数之间添加逗号:

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。 其他版本的错误显示可能有所不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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