【问题标题】:PowerShell script will not run in orderPowerShell 脚本不会按顺序运行
【发布时间】:2018-04-27 02:11:03
【问题描述】:

如果我逐行运行 PowerShell 脚本,它运行良好,但是当尝试将脚本作为一个脚本运行时,对于下一个问题的用户搜索没有及时出现。请让我知道如何强制脚本的第三行按要求出现。

$name = Read-Host "What is the user's first name or letter?"

$list = Get-ADUser -Filter * | ? {$_.SamAccountName -match $name} | select SamAccountName | sort SamAccountName
$list


$DisableUser = Read-Host "Copy and paste the user here"
$t = $DisableUser
$year = Read-Host "Please input the year the user should be disabled, in this format (YYYY)"
$month = Read-Host "Please input the month the user should be disabled, in this format (MM)"
$day = Read-Host "Please input the day the user should be disabled, in this format (DD)"
$date = "$month/$day/$year"


$hour = Read-Host "Please input the hour of the day the user should be disabled, in this format (HH)"
$minute = Read-Host "Please input the minute the user should be disabled, in this format (MM)"
$seconds = Read-Host "Please input the second the user should be disabled, in this format (SS)"
$ampm = Read-Host "AM or PM?"
$Time = "${hour}:${minute}:${seconds} ${ampm}"


$dandt = "$date $Time"
$dandt

Write-host "$t will be disabled on this date, $dandt"

$answer = Read-Host "Is this correct? Please type Yes or No"
$l = $answer
    If ($l -like "y*")
    {Set-ADAccountExpiration $t -DateTime $dandt}
    ELSE { "Exiting"; Return}

【问题讨论】:

  • 活动目录中的名字是GivenName 而不是SamaccountName。您的搜索似乎有误。
  • 另外,另一个提示。您可能希望将搜索添加到 -Filter 参数中。这将有助于提高性能。目前,您正在拉取整个目录,然后进行过滤。

标签: powershell active-directory


【解决方案1】:

您正在组合输出流。 Read-HostWrite-Host 直接写入控制台,而 $list$dandt 单独输出到标准输出。它们是不同步的,因为它们是不同的输出流。解决方案基本上是通过一个流强制所有内容。由于您使用的是Read-Host,这意味着控制台流。

改变这个:

$list

到其中之一:

$list | Format-Table -AutoSize | Out-String | Write-Host
$list | Format-List | Out-String | Write-Host

还有这个:

$dandt

到这里:

Write-Host $dandt

也就是说,这根本不是我写这样的东西的方式。我宁愿使用 ADUC/ADAC 而不是这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2019-03-26
    相关资源
    最近更新 更多