2017-10-06 128 views
0

我想知道如何使用Invoke-Command傳遞參數以在腳本內使用以遠程運行。參數不會與Invoke-Command遠程使用,-computername返回null

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} 

我想知道這是否可能,我需要做什麼來修復這個腳本? 感謝

+0

做一些研究似乎我需要使用的參數通過調用命令cmdlet來傳遞變量。仍試圖找出那部分。 – vnavna

+0

Param( [string] $ cname ='讀取主機計算機名?', [string] $ event ='讀取主機什麼事件?', [string] $ eventfilter ='讀取主機什麼關鍵字是', [string] $ amount ='Read-Host你想看多少事件?' )Invoke-Command -ScriptBlock {Get-EventLog -cn $ cname -LogName $ event -Message * $ eventfilter *「| FT -Wrap} – vnavna

回答

0
$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} 
+0

$ using:命令修復了這個問題。 – vnavna

+0

使用變量只能用於腳本工作流中的Invoke-Command,Start-Job或InlineScript。當它與Invoke-Command, 一起使用時,只有在遠程計算機上調用腳本塊時,Using變量纔有效。 – vnavna