【问题标题】:Powershell script won't listen to if statementPowershell脚本不会听if语句
【发布时间】:2019-10-04 20:28:16
【问题描述】:

我真的很讨厌编写脚本,我需要你的帮助。我从互联网上的几个地方拼凑了这个脚本,它可以工作,直到我启用我的 IF 语句......

我只是想从 UNC 路径获取文件夹的文件计数,如果超过指定数量,我希望它发送一封电子邮件,让我知道当前计数。

但是,如果我取消注释 if ($count -gt 50) 部分,那么如果计数超过 50,我将不会收到电子邮件。

我不知道如何使“.Count”成为我在脚本其他地方使用的变量。有人可以帮忙吗?

然后我需要弄清楚如何运行它。正在考虑只是 Windows 中的计划任务,并让它每隔几分钟运行一次,但如果你有更好的想法,我想听听!

$FolderList = @(
    "\\server\path\test"
        )
$Body = ($FolderList | ForEach-Object {
    "Check to see if Sweep Service is running, file count for '$($_)':  " + (Get-ChildItem -Path $_ -File -ErrorAction SilentlyContinue | Measure-Object).Count
}) -join "`r`n"

#if ($count -gt 50)
#{
    $From = "me@you.com"
    $To = "me@you.com"
    $Subject = "Sweep Checker"
    $SmtpServer = "webmail.you.com"
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer
#}

【问题讨论】:

  • 在取消注释 if 语句后是否添加一个结束括号?如果是这样,请add 为您的代码加上一个大括号(缩进 if 块也有帮助)。
  • 是的,对不起。我将结束大括号注释掉了,但它给了我一个错误,所以当我注释掉 if 语句时,我开始删除结束大括号进行测试。
  • 我调整了缩进,以便更容易看到 在 if 循环中的位,如果没有注释掉的话
  • 你了解变量赋值的工作原理吗?您创建一个名为 $count 的变量,就像创建一个名为 $body 的变量一样:您使用 = 符号为其分配一个值。
  • 这个 answer 可能有助于在 powershell 中计数。

标签: powershell powershell-4.0


【解决方案1】:

您的主要问题似乎是没有将文件数保存到任何变量中。相反,您将值保存为字符串的一部分 - 而不是数字。 [咧嘴一笑]

以下代码将当前目录的文件计数显式放入 var,将其添加到 total 计数中,然后使用当前计数为您的 msg 正文构建输出字符串.

$FolderList = @(
    $env:TEMP
    $env:USERPROFILE
    $env:ALLUSERSPROFILE
        )
$TriggerFileCount = 20
$TotalFileCount = 0

$Body = foreach ($FL_Item in $FolderList)
    {
    $CurrentFileCount = @(Get-ChildItem -LiteralPath $FL_Item -File -ErrorAction SilentlyContinue).Count
    $TotalFileCount += $CurrentFileCount

    # send out to the "$Body" collection
    'Check to see if Sweep Service is running, file count for [ {0} ] = {1}' -f $FL_Item, $CurrentFileCount
    }

if ($TotalFileCount -gt $TriggerFileCount)
    {
    $SMM_Params = @{
        From = 'NotMe@example.com'
        To = 'NotYou@example.com'
        Subject = 'Sweep Checker'
        Body = $Body -join [System.Environment]::NewLine
        SmtpServer = $SmtpServer
        }

    $SMM_Params

    #Send-MailMessage @SMM_Params
    }

输出...

Name                           Value
----                           -----
Subject                        Sweep Checker
From                           NotMe@example.com
To                             NotYou@example.com
SmtpServer                     webmail.you.com
Body                           Check to see if Sweep Service is running, file count for [ C:\Temp ] = 22...

$Body变量的全部内容...

Check to see if Sweep Service is running, file count for [ C:\Temp ] = 22
Check to see if Sweep Service is running, file count for [ C:\Users\MyUserName ] = 5
Check to see if Sweep Service is running, file count for [ C:\ProgramData ] = 0

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多