首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell脚本将不会按顺序运行

PowerShell脚本将不会按顺序运行
EN

Stack Overflow用户
提问于 2018-04-27 01:09:32
回答 1查看 511关注 0票数 0

如果我逐行运行PowerShell脚本,它就可以正常工作,但是当我尝试将脚本作为一个脚本运行时,对用户的搜索不会及时出现在下一个问题中。请让我知道我如何才能强制脚本的第三行出现,而不是要求以后的方式。

代码语言:javascript
复制
$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}
EN

回答 1

Stack Overflow用户

发布于 2018-04-27 02:11:04

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

更改此设置:

代码语言:javascript
复制
$list

其中之一:

代码语言:javascript
复制
$list | Format-Table -AutoSize | Out-String | Write-Host
$list | Format-List | Out-String | Write-Host

还有这个:

代码语言:javascript
复制
$dandt

要这样做:

代码语言:javascript
复制
Write-Host $dandt

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50048566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档