无法在我的脚本中使用远程会话

时间:2013-07-23 08:54:02

标签: powershell powershell-v2.0 powershell-remoting

嗨,这是我的脚本,我想捕获remoteexitcode并将变量作为参数传递,但它不起作用。

我无法将我的变量传递给脚本块我试图在invoke-command中使用-ArgumentList并使用param()捕获它但是它似乎不起作用

我还有另一个问题,即创建一个New-PSsession,价值似乎没有被存储,我不知道为什么。我使用远程会话的主要目的是传递退出代码的值,以便在退出代码为1时我可以将其显示为失败。

我收到以下错误:

  

[10:23:52] [步骤1/2] Invoke-Command:无法使用指定的命名par解析参数集   [10:23:52] [第1/2步] ameter。   [10:23:52] [第1/2步]在D:\ TeamCityAgent2 \ work \ a4e87a75117c6a1b \ CheckLogsize.ps1:95 char:15   [10:23:52] [第1/2步] + Invoke-Command<<<< -ScriptBlock {$ runscript} -ComputerName $ hostName -Sessi   [10:23:52] [步骤1/2] $ remotesession   [10:23:52] [第1/2步] + CategoryInfo:InvalidArgument:(:) [Invoke-Command],参数   [10:23:52] [第1/2步] BindingException   [10:23:52] [第1/2步] + FullyQualifiedErrorId:> AmbiguousParameterSet,Microsoft.PowerShell.Comma   [10:23:52] [第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 = "nitintce@gmail.com 
[String]$EmailSubj = "Log size warning"
[String]$Emailfrom = "no-reply@domain.com"
[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 "no-reply@hm.com" -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
}

1 个答案:

答案 0 :(得分:0)

你需要在括号中传递你的参数,如下所示:

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

否则,PowerShell不会将其识别为数组

相关问题