PowerShell脚本不会按顺序运行

时间:2018-04-26 17:09:32

标签: powershell active-directory

如果我逐行运行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}

1 个答案:

答案 0 :(得分:1)

您正在组合输出流。 import bs4Read-Host直接写入控制台,而Write-Host$list自行输出到标准输出。它们会因为它们的输出流不同而失去同步。解决方案基本上是通过一个流强制一切。由于您使用$dandt,这意味着控制台流。

改变这个:

Read-Host

其中一个:

$list

而且:

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

对此:

$dandt

那就是说,这根本不是我怎么写这样的东西。我宁愿使用ADUC / ADAC。