从内部方法访问$ args / params

时间:2016-02-08 14:57:50

标签: powershell

我正在研究PowerShell脚本的错误处理方法。我通过catch上的try / catch传递错误,但是我想从命令行中调用原始params来创建错误日志和错误电子邮件。

这是我到目前为止所拥有的:

# --params--
param(
  [string]$Directory,
  [string]$ArchiveDirectory,
  [string]$ErrorDirectory,
  [string]$ErrorEmailFrom,
  [string]$ErrorEmailTo,
  [string]$ErrorEmailSubject,
  [string]$ErrorSMTP,
  [string]$FTPSite,
  [string]$FTPUser,
  [string]$FTPPass,
  [string]$FTPRemoteDir
)

# list of arguments for debug
$paramList = $args

# --functions--

function Handle-MyError
{
  Write-Host "handle-error"
  Write-Host $args[0];  # this is the exception passed in

  # -Email alert-
  $subject = $ErrorEmailSubject + $FTPSite 
  # build message
  $message =  Get-Date -Format "yyyy-mm-dd hh:mm:ss" 
  $message += "`r`nError: " + $FTPSite + " : " + $args[0]
  $message += "`r`nParameters:`r`n" 

  # Grab each parameter value, using Get-Variable
  for ($i=0;$i -lt $paramList.Length; $i++)
  {
    $message += $paramList[$i]
  }

  # send email
  $smtp = New-Object Net.Mail.SmtpClient($ErrorSMTP)
  $smtp.Send($ErrorEmailFrom, $ErrorEmailTo, $subject, $message)

  # drop error file
  $theDate = Get-Date -Format "yyyymmdd"
  $errorFile = $ErrorDirectory + "\" + $theDate + "_ERROR.txt"
  Write-Host $errorFile
  $message | Out-File $errorFile -Append
}

和我的try / catch

catch [Exception]
{
  Write-Host "SPOT 1"
  Handle-MyError $_.
}

在顶部,我尝试将原始$args保存为$paramList以便稍后循环,但它无效。在Handle-MyError方法中,$args成为传递的错误,因此我想如果我将原始$args保存为$paramList我可以稍后访问它,但它很难...想法?

2 个答案:

答案 0 :(得分:3)

有几种方式,按最差到最好:

Get-VariableScope参数一起使用。范围编号可以不同,但​​应至少为2(Script->Catch->Handle-MyError

function Handle-MyError
{
   Write-Host (Get-Variable -Name ErrorEmailFrom -ValueOnly -Scope 2)
}

使用$Script:前缀

function Handle-MyError
{
   Write-Host $Script:ErrorEmailFrom
}

使用$PSBoundParameters

# list of arguments for debug
$paramList = $PsBoundParameters

function Handle-MyError
{
   Param
   (
       $Exception,
       $Cfg
   )

   Write-Host $Cfg.ErrorEmailFrom
}

catch [Exception]
{
    Write-host "SPOT 1"
    Handle-MyError -Exception $_ -Cfg $paramList
}

使用splatting:

$paramList = $PsBoundParameters

function Handle-MyError
{
    Param
    (
        $Exception,
        $ErrorDirectory,
        $ErrorEmailFrom,
        $ErrorEmailTo,
        $ErrorEmailSubject,
        $ErrorSMTP
    )

    Write-Host $ErrorEmailFrom
}

catch [Exception]
{
    Write-host "SPOT 1"
    Handle-MyError @paramList -Exception $_
}

答案 1 :(得分:0)

这是@beatcracker提供帮助后的最终代码。 我结合了两块拼图。

  1. 我需要将初始参数保存在本地var和Two,($paramList = $PsBoundParameters)
  2. 使用.GetEnumerator()

    访问此var / list
    # --params--
    param(
        [string]$Directory,
        [string]$ArchiveDirectory,
        [string]$ErrorDirectory,
        [string]$ErrorEmailFrom,
        [string]$ErrorEmailTo,
        [string]$ErrorEmailSubject,
        [string]$ErrorSMTP,
        [string]$FTPSite,
        [string]$FTPUser,
        [string]$FTPPass,
        [string]$FTPRemoteDir
    )
    # set params as var for debug later
    $paramList = $PsBoundParameters
    
    # --functions--
    
    function Handle-MyError
    {
        Write-Host "handle-error"
        #write-host "Exception:" $args[0];  # this is the exception passed in
    
        # -Email alert-
    
        # build subject
        $subject = $ErrorEmailSubject + " " + $FTPSite 
    
        # build message
        $message =  Get-Date -format s
        $message += "`r`nError Message: " + $args[0]
        $message += "`r`nParameters:`r`n" 
    
        $paramList.GetEnumerator() | ForEach-Object `
        {
            #Write-Host $_.Key "=" $_.Value
            if ($_.Key -ne "FTPPass"){
                $message += "`r`n" + $_
            }
        }
    }