【问题标题】:Using powershell to restart computers if -LastBootupTime = -gt 10 days如果 -LastBootupTime = -gt 10 天,则使用 powershell 重新启动计算机
【发布时间】:2017-08-01 06:59:16
【问题描述】:

我正在尝试编写一个脚本来检查网络上的计算机运行了多长时间,如果它们运行超过 10 天,它们需要重新启动。我打算每周日用任务管理器自动运行脚本。

感谢@vonPryz,我现在有了这样的东西:

$clients = get-content "C:\Documents\lijstcomputers.txt"

foreach ($client in $clients) {

    if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) {
        write-Host $client is online


        $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime

        $startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi  Win32_OperatingSystem -computer $client).lastbootuptime)


        if( $uptime.days -ge 10) {
            restart-computer -computername $client
            add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime"
        }
    }
    else {
    write-Host $client is offline
    }
}

但是现在我收到了这个错误:

    gcim : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and
 that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for publ
ic profiles limits access to remote computers within the same local subnet.
At line:1 char:25
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ConnectionError: (root\cimv2:Win32_OperatingSystem:String) [Get-CimInstance], CimException
    + FullyQualifiedErrorId : HRESULT 0x80338126,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
    + PSComputerName        : ASND0042

Cannot find an overload for "op_Subtraction" and the argument count: "2".
At line:1 char:1
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

【问题讨论】:

  • 那么问题是什么?如何启动计算机?如何将结果写入文件?还有什么?
  • 您可能想要查看导入/导出为 CSV(使用 Get-Help *csv*),或者您想要更正代码中的错误(您可以指定),但您还没有这样做.如果您希望导出为 CSV,那么这也不是堆栈溢出问题,因为快速的 google 搜索将为您提供所需的所有答案。
  • @vonPryz 自从我刚开始使用 powershell 以来,我只是找不到一种方法可以在一个脚本中完成所有这些工作。问题是您是否可以帮助我编写一个脚本来重新启动已经运行了 10 多天的计算机,并给我一个很好的、易于阅读的关于发生的事情的概述。提前致谢!
  • @MaximeFranchot 我尝试了很多东西,但似乎找不到正确的命令。 CSV 的东西只是一个加号。

标签: database windows powershell networking


【解决方案1】:

由于您已经了解了一些部分,这不仅仅是请给我 teh codez 并因此需要帮助。因此,让我们概述一下整个脚本的外观。

# Write computer names into a file, one per row for easier management
$clients = get-content "c:\ListOfComputers.txt"

foreach ($client in $clients) {
# If computer's not up, there's no need to check uptime
    if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) {
        write-Host $client is online

        # Get uptime
        $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime
        # Get start time
        $startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi  Win32_OperatingSystem -computer $client).lastbootuptime)

        # Restart the client if uptime's at least 10 days.
        if( $uptime.days -ge 10) {
            restart-computer -computername $client
        # Add client name, start date and uptime into a log file
            add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime"
        }
    }
    else {
    write-Host $client is offline
    }
}

可以通过添加一些错误处理以及正确的 CSV 导出来进一步改进这个框架。

【讨论】:

  • 这似乎可以正常工作,但更改后我收到此错误。 (见文章)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 2021-07-07
  • 1970-01-01
  • 2021-11-19
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多