【问题标题】:Pass parameter from one Powershell function to another将参数从一个 Powershell 函数传递到另一个
【发布时间】:2017-03-29 16:21:12
【问题描述】:

我是 powershell 和一般开发的新手。我正在尝试编写一个脚本,一旦文件超过一定大小,它将向联系人发送电子邮件。我有两个单独的功能,它们都单独工作(一个用于检查文件大小,一个用于生成文件以供 sendmail 使用),但我无法让它们交互。

我想执行函数 CheckSize 并且如果变量 $ExceedsSize 设置为 1 则调用函数 SendMail 否则脚本应该在没有其他操作的情况下完成。

我搜索了论坛,但找不到任何适用于我正在做的事情的内容。

   ##Check file to see if it is over a particular size and then send email once threshold is reached.

param( 
    [string]$SiteName = "TestSite",                                                 #Name of Site (No Spaces)
    [string]$Path = "\\NetworkPath\Directory",                                      #Path of directory to check
    [int]$FileSizeThreshold = 10,                                                   #Size in MB that will trigger a notification email
    [string]$Contacts = "MyEmail@email.com"
    )    

CLS

##Creates variable $ExceedsSize based on newest file in folder.
Function CheckSize {
IF ((GCI $Path -Filter *.txt | Sort LastWriteTime -Descending | Select-Object -first 1 | Measure-Object -property Length -sum).sum / 1000000 -gt $FileSizeThreshold) {$ExceedsSize = 1}
ELSE {$ExceedsSize = 0}

Write-Host $ExceedsSize
}


Function SendMail {
    Param([string]$Template, [string]$Contacts, [string]$WarnTime)

    $EmailLocation = "\\NetworkPath\Scripts\File_$SiteName.txt"

    #Will Generate email from params
        New-Item $EmailLocation -type file -force -value "From: JMSIssue@emails.com`r
To: $Contacts`r
Subject: $SiteName file has exceeded the maximum file size threshold of $FileSizeThreshold MB`r`n"

    #Send Email
    #CMD /C "$SendMail\sendmail.exe -t < $EmailLocation"

    }

【问题讨论】:

    标签: powershell


    【解决方案1】:

    Write-Host $ExceedsSize 之前或之后添加:

    return $ExceedsSize
    

    将此添加到底部:

    $var = CheckSize
    
    if ($var -eq 1){
        SendMail
    }
    

    说明
    你有两个函数,但实际上并没有运行它们。底部的部分就是这样做的。
    您的 CheckSize 函数不会为函数的其余部分返回 $ExceedsSize;默认情况下,它仍然在函数的范围内。 return x 表示将变量传递回主脚本。 $var = 表示它已分配给该变量。

    【讨论】:

    • 我完全删除了 Write-Host $ExceedsSize 并将其替换为 Return $ExceedsSize。在那之后,将你的 IF 添加到底部就非常有意义并且就像我想要的那样工作。我没有想到将变量分配给函数的输出。
    • 太好了,很高兴听到它现在可以工作了!根据另一个答案,另一种方法是从CheckSize 中调用SendMail。另一种方法 - 通常应该避免这种不好的做法 - 是分配一个变量全局范围:$global:ExceedsSize,然后检查SendMail 中的$ExceedsSize 的值。 about_scopes 值得一读。
    【解决方案2】:

    根据其他答案,您需要 return $ExceedsSize 而不是使用 Write-Host(请参阅此处了解为什么认为 Write-Host 有害:http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/)。

    您也可以在 CheckSize 函数中调用 SendMail 函数,例如:

     if ($ExceedsSize -eq 1){SendMail}
    

    您仍然需要在某个地方调用 CheckSize 函数:

    CheckSize
    

    您可能还需要考虑以内置 cmdlet 的动词-名词样式命名您的函数。这确实有助于使它们对您和其他人的使用更加明确。选择动词时,最好坚持认可列表:https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx

    并且还要使用相当独特的名称以避免可能的冲突。

    我的建议是:

    Get-NewestFileSize
    

    (虽然这就是它应该返回的)

    Send-CCSMail
    

    【讨论】:

    • 我从 Admin 到 Help Desk 再到 L2 Support,最终成为了一名开发人员。很长一段时间以来,我一直在想办法解决这个问题。动词-名词命名约定很有意义。
    猜你喜欢
    • 1970-01-01
    • 2014-10-23
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多