0

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

我无法将变量传递给我尝试在调用命令中使用 -ArgumentList 并使用 param() 捕获它的脚本块,但它似乎不起作用

我在创建 New-PSsession 时也遇到了另一个问题,该值似乎没有被存储,我不明白为什么。我使用远程会话的主要目的是传递退出代码的值,以便如果退出代码为 1,我可以将其显示为失败。

我收到以下错误:

[10:23:52][Step 1/2] Invoke-Command:无法使用指定的命名 par [10:23:52][Step 1/2] 米解析参数集。[10:23:52][步骤 1/2] 在 D:\TeamCityAgent2\work\a4e87a75117c6a1b\CheckLogsize.ps1:95 char:15 [10:23:52][步骤 1/2] + 调用命令 << << -ScriptBlock {$runscript} -ComputerName $hostName -Sessi [10:23:52][Step 1/2] on $remotesession [10:23:52][Step 1/2] + CategoryInfo : InvalidArgument: (: ) [Invoke-Command],参数 [10:23:52][Step 1/2] BindingException [10:23:52][Step 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
}
4

1 回答 1

0

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

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

否则powershell不会将其识别为数组

于 2013-07-24T08:06:07.630 回答