Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 为什么我不能在这个变量中选择任何内容?_Powershell - Fatal编程技术网

Powershell 为什么我不能在这个变量中选择任何内容?

Powershell 为什么我不能在这个变量中选择任何内容?,powershell,Powershell,我无法从$results2变量中选择任何值。当我单独显示变量时,它会显示它应该如何显示的所有信息,我只是不能通过管道将其显示并选择对象 $exists = $true while ($exists -eq $true) { $search = Read-Host -Prompt "Enter first name or surname name of user, or enter [x] to quit to main menu&

我无法从$results2变量中选择任何值。当我单独显示变量时,它会显示它应该如何显示的所有信息,我只是不能通过管道将其显示并选择对象

$exists = $true
            while ($exists -eq $true) {
                $search = Read-Host -Prompt "Enter first name or surname name of user, or enter [x] to quit to main menu"
                if ($search -ne 'x') {
                    $results = @(Get-ADUser -Filter "Name -like '*$search*'")
                    if ($results) {
                        $i=1
                        $results2 = $results | Select-Object @{ Name = "ID" ; Expression= {$Global:i;$Global:i++}}, Name, SamAccountName | Format-Table
                        $results2
                        $id = Read-Host -Prompt "Enter ID you want to reset"
                        $user = $results2 | Where-Object {$_.ID -eq "$id"}
                        $user
                        
$results2输出:

管道到格式-*cmdlet是您最不想做的事情

把格式表想象成一个层压机。你给它一张纸,它会把它吐出来——非常漂亮,容易阅读——但是你不能再编辑纸上的文字了

相反,当您准备好输出到屏幕时,只调用Format-*:

# this statement is supposed to process/calculate data we'll need later - NO FORMATTING HERE!
$results2 = $results | Select-Object @{ Name = "ID" ; Expression= {$Global:i;$Global:i++}}, Name, SamAccountName

# this statement is supposed to show the table in the host application - good time to use `Format-Table`
$results2 |Format-Table

# Make sure you trim the input for any excess whitespace
$id = Read-Host -Prompt "Enter ID you want to reset" |ForEach-Object Trim

# Same story here as above - separate data processing from formatting/presentation
$user = $results2 | Where-Object {$_.ID -eq "$id"}
$user |Format-Table
管道到格式-*cmdlet是您最不想做的事情

把格式表想象成一个层压机。你给它一张纸,它会把它吐出来——非常漂亮,容易阅读——但是你不能再编辑纸上的文字了

相反,当您准备好输出到屏幕时,只调用Format-*:

# this statement is supposed to process/calculate data we'll need later - NO FORMATTING HERE!
$results2 = $results | Select-Object @{ Name = "ID" ; Expression= {$Global:i;$Global:i++}}, Name, SamAccountName

# this statement is supposed to show the table in the host application - good time to use `Format-Table`
$results2 |Format-Table

# Make sure you trim the input for any excess whitespace
$id = Read-Host -Prompt "Enter ID you want to reset" |ForEach-Object Trim

# Same story here as above - separate data processing from formatting/presentation
$user = $results2 | Where-Object {$_.ID -eq "$id"}
$user |Format-Table

首先,您应该删除命令行中定义变量的格式表-您的代码似乎至少缺少3},请修理it@MathiasR.Jessen我故意忽略了它们,因为代码的其余部分对于problem@Olaf当我删除Format表时,它不会在下一个表中输出结果line@ConorTimms然后将下一行从$results2更改为$results2 | Format Table首先,您应该删除命令行中的Format Table,其中你定义变量-您的代码似乎至少缺少3},请修复it@MathiasR.Jessen我故意忽略了它们,因为代码的其余部分对于problem@Olaf当我删除Format表时,它不会在下一个表中输出结果line@ConorTimms然后将下一行从$results2更改为$results2 | Format Table