简体   繁体   English

通过Powershell一次重新启动一台AD计算机

[英]Reboot AD computers one at a time through powershell

I need to reboot all computers in AD one at a time(excluding my computer), confirming that each one has restarted before restarting the next one. 我需要一次重新启动AD中的所有计算机(我的计算机除外),确认每台计算机都已重新启动,然后再重新启动下一台计算机。 Once a system has rebooted, I want to see it's IP address and name. 系统重新启动后,我想查看它的IP地址和名称。 Below is my attempt but I think I'm doing something wrong since its not working. 下面是我的尝试,但是我认为我做错了,因为它不起作用。 The main error I receive is: 我收到的主要错误是:

 Get-WMIObject: cannot validate argument on parameter 'computerName'. The argument is null or empty
 + $serverobj = gmi Win32_operatingsystem -computername $i.server

Code: 码:

$servers= Get-ADComputer -Filter * | Select-Object -ExpandProperty name

foreach($i in $servers) {
 Write-Host "Rebooting:" $i.server "..."

 $serverObj = gwmi Win32_operatingsystem -computer $i.server

 $status = $serverObj.reboot()

 if ($status.ReturnValue = "0") {
      Write-Host "Reboot successful."
 } else {
      Write-Host "Reboot failed."
 }
do {
    Start-Sleep -s 2 }  # this will wait for 2 seconds 
while (Test-Connection $servers -count 1 | select @{Name="Computername";Expression={$_.Address}},Ipv4Address)
}

This one seems simple enough for your immediate problem: 对于您眼前的问题,这似乎很简单:

$serverObj = gwmi Win32_operatingsystem -computer $i.server

should be 应该

$serverObj = gwmi Win32_operatingsystem -computer $i

$i has no properties beyond what a [string] has. $i除了[string]之外,没有其他属性。 PowerShell, by default, will allow you to attempt to get properties that do not exist and a null will be returned in that case. 默认情况下,PowerShell将允许您尝试获取不存在的属性,在这种情况下将返回null。 Get-WMIObject however cannot work with that for the parameter -Computer . 但是, Get-WMIObject不能与参数-Computer The error was pretty specific. 该错误非常具体。

There is a way you can try and catch these issues but using Set-StrictMode -Version 2.0 for example. 您可以尝试捕获这些问题,但是可以使用Set-StrictMode -Version 2.0的方法。 From TechNet TechNet

2.0 2.0

-- Prohibits references to uninitialized variables (including uninitialized variables in strings). -禁止引用未初始化的变量(包括字符串中的未初始化的变量)。

-- Prohibits references to non-existent properties of an object. -禁止引用对象的不存在的属性。

-- Prohibits function calls that use the syntax for calling methods. -禁止使用语法作为调用方法的函数调用。

-- Prohibits a variable without a name (${}). -禁止不带名称($ {})的变量。


You refer to the main error you are receiving ... does that mean you have more? 您指的是您收到主要错误 ...这是否意味着您还有更多错误?

Your while condition seems flawed. while条件似乎有缺陷。 You should be trying to evaluate a boolean expression but you are just returning data. 您应该尝试对布尔表达式求值,但是您只是在返回数据。 Even if the ping failed data should be returned and that could invalidate the logic of your loop. 即使应返回ping失败的数据,这也可能使循环逻辑无效。 Consider using the switch -Quiet for test-connection . 考虑使用switch -Quiet进行test-connection You are also checking the pings of all $servers instead of just $i . 您还要检查所有 $servers的ping信息,而不只是$i

while (!(Test-Connection $i -count 1 -Quiet))

maybe... 也许...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM