2013-07-23 22 views
0

嗨,這裏是我的腳本,我想捕獲remoteexitcode並將變量作爲參數傳遞,但它不起作用。無法在我的腳本中使用遠程會話

我不能到我的變量傳遞給腳本塊我試圖在調用命令使用-ArgumentList和使用參數()抓住它,但它似乎沒有工作

我也是在創建New-PSsession時出現另一個問題,該值似乎沒有被存儲,我不明白爲什麼。主要目的,我使用遠程會話是通的退出代碼值,以便我可以顯示它作爲一個失敗,如果退出代碼爲1

我得到以下錯誤:

[10:23:52][Step 1/2] Invoke-Command : Parameter set cannot be resolved using the specified named par [10:23:52][Step 1/2] ameters. [10:23:52][Step 1/2] At D:\TeamCityAgent2\work\a4e87a75117c6a1b\CheckLogsize.ps1:95 char:15 [10:23:52][Step 1/2] + Invoke-Command <<<< -ScriptBlock {$runscript} -ComputerName $hostName -Sessi [10:23:52][Step 1/2] on $remotesession [10:23:52][Step 1/2] + CategoryInfo : InvalidArgument: (:) [Invoke-Command], Parameter [10:23:52][Step 1/2] BindingException [10:23:52][Step 1/2] + FullyQualifiedErrorId : >AmbiguousParameterSet,Microsoft.PowerShell.Comma [10:23:52][Step 1/2] nds.InvokeCommandCommand

param(
[String]$H = "null" 
) 

[String]$hostName = [String]$H 
Get-Date 

[int]$logsize1 = 0 
[int]$logsize2 = 0 
[int]$logsizediff = 0 
[array]$Emaillist = "[email protected] 
[String]$EmailSubj = "Log size warning" 
[String]$Emailfrom = "[email protected]" 
[String]$EmailBody = "Stopping Apache and TC service due excessive logging" 
[String]$logfolder = "D:\hmonline\servers\tcserver\hmonline-instance\logs\hybris.log" 

$runscript = { 

param([String]$rechostName, [int]$reclogsize1, [int]$reclogsize2, [int]$reclogsizediff, [String]$reclogfolder) 

Function checklogsize 
{ 

If(($reclogsize2 -gt 40000000) -or $logsizediff -gt 20000000) 
{ 
    Send-MailMessage -From "[email protected]" -Subject "$recEmailSubj on $rechostName" -To $recEmaillist -Body ` 
    " 
    <head> 
    </head> 
    <body>  
     <h2> Log Size Exceeded the limit or there was a Spike in the log </h2> 
     <table border=2 class=logdetails align=center> 
      <tr> 
       <th> Log Size </th> <td> $reclogsize2 </td> 
      </tr> 
      <tr> 
       <th> Log growth in last 20 mins </th> <td> $reclogsizediff </td> 
      </tr> 
      <tr> 
       <th> Log Location </th> <td> $reclogfolder </td> 
      </tr> 
     </table> 
    </body> 
" -BodyAsHtml -Priority Normal -SmtpServer smtp.hm.com 
    Write-Error -Message "Log sizes have exceeded the limit or growing too fast" -Category LimitsExceeded 
    Exit 1 
} 
Else 
{ 
    Get-Date 
    Write-Host -NoNewline "Log sizes are stable" 
    Exit 0 
} 
} 

$reclogsize1=(Get-Item $reclogfolder).length 
Start-Sleep 300 
$reclogsize2=(Get-Item $reclogfolder).length 
$reclogsizediff = $reclogsize2 - $reclogsize1 
Write-Host $reclogsize1 
Write-Host $reclogsize2 
Write-Host $reclogsizediff 

checklogsize 
} 



If($hostName -eq "secc1794") 
{ 
$remotesession = New-PSSession -ComputerName $hostName 

Invoke-Command -ScriptBlock {$runscript} -ComputerName $hostName -Session $remotesession -ArgumentList $hostName, $logsize1, $logsize2, $logsizediff, $logfolder 

$remotelastexitcode = invoke-command -ScriptBlock {$lastexitcode} -Session $remotesession 
Invoke-Command -ComputerName $hostName -ScriptBlock {$runscript} 
Write-Host -NoNewline "Log size1 is:" 
Write-Host $logsize1 
$remotelastexitcode 
} 

Else 
{ 
Write-Error -Message "Unknow Host stopping process" -Category ObjectNotFound 
Exit 0 
} 

回答

0

你需要通過你的論點brakets,像這樣:

Invoke-Command -ScriptBlock {$runscript} -ComputerName $hostName -Session $remotesession -ArgumentList ($hostName, $logsize1, $logsize2, $logsizediff, $logfolder) 

否則PowerShell不承認它是一個數組

相關問題