【问题标题】:Parameters will not be used with Invoke-Command remotely, -computername comes back null参数不会与 Invoke-Command 远程一起使用,-computername 返回 null
【发布时间】:2017-10-06 14:00:02
【问题描述】:

我想知道如何使用 Invoke-Command 传递参数以在远程运行的脚本中使用。

enter code here
Param(
[string]$cname = (Read-Host "Computer Name?") ,
[string]$event = (Read-Host "What event?") ,
[string]$eventfilter = (Read-Host "What key word are you looking for?") '
[string]$amount = (Read-Host "How many events do you want to see?")
)
Invoke-Command -cn $cname -ScriptBlock {Get-EventLog -cn $cname -LogName $event -Message "*$eventfilter*" -Newest $amount | FT -Wrap}

我想知道这是否可行以及我需要做什么来修复这个脚本? 谢谢

【问题讨论】:

  • 做一些研究似乎我需要使用参数通过 Invoke-Command cmdlet 传递变量。仍在试图弄清楚那部分。
  • Param( [string]$cname = 'Read-Host Computer Name?', [string]$event = 'Read-Host What event?', [string]$eventfilter = 'Read-Host你在找什么关键词?', [string]$amount = 'Read-Host 你想看多少个事件?' ) Invoke-Command -ScriptBlock {Get-EventLog -cn $cname -LogName $event -Message "$事件过滤器" | FT -Wrap}

标签: host invoke-command


【解决方案1】:
$cname = Read-Host "Computer Name?"
$event = Read-Host "What event?"
$eventfilter = Read-Host "What key word are you looking for?"
$amount = Read-Host "How many events do you want to see?"


Invoke-Command -cn $cname -ScriptBlock {Get-EventLog -cn $using:cname -LogName $using:event -Message "*$eventfilter*" -Newest $using:amount | FT -Wrap}

【讨论】:

  • A Using 变量只能与脚本工作流中的 Invoke-Command、Start-Job 或 InlineScript 一起使用。当它与 Invoke-Command 一起使用时,Using 变量只有在远程计算机上调用脚本块时才有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
相关资源
最近更新 更多