Powershell Show-Menu 脚本未从 get-ADuser 获得任何结果
Powershell Show-Menu script not getting any result from get-ADuser
我没有从用户的输入中得到任何结果..但是这个消息:
Get-ADUser:无法验证参数 'Identity'
的参数
抱歉,我只是 PS 的新手。我在这里做错了什么?
我正在尝试将用户的输入获取到 Get-ADUser -Identity $Username 但它似乎没有接受输入..
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$UserName = Read-Host
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
do
{
Show-Menu
$Selection = Read-Host "Please make a selection"
switch ($Selection)
{
'U' {Get-ADUser -Identity $Username}
}
Pause
}
until ($Input -eq 'q')
您的代码有 2 个问题,第一个问题,$UserName
已定义且仅存在 在 until ($input -eq 'q')
的 scope of your function Show-Menu
, to correct this, you can have the call to Read-Host
inside your function but don't have it assigned to any variable so we can capture the user name on each function call ($userName = Show-Menu
). The second problem is your use of the automatic variable $input
中.
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
Read-Host # This can be output from your function
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
do {
$userName = Show-Menu # which is captured here
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')
我没有从用户的输入中得到任何结果..但是这个消息: Get-ADUser:无法验证参数 'Identity'
的参数抱歉,我只是 PS 的新手。我在这里做错了什么? 我正在尝试将用户的输入获取到 Get-ADUser -Identity $Username 但它似乎没有接受输入..
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
$UserName = Read-Host
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
do
{
Show-Menu
$Selection = Read-Host "Please make a selection"
switch ($Selection)
{
'U' {Get-ADUser -Identity $Username}
}
Pause
}
until ($Input -eq 'q')
您的代码有 2 个问题,第一个问题,$UserName
已定义且仅存在 在 until ($input -eq 'q')
的 scope of your function Show-Menu
, to correct this, you can have the call to Read-Host
inside your function but don't have it assigned to any variable so we can capture the user name on each function call ($userName = Show-Menu
). The second problem is your use of the automatic variable $input
中.
function Show-Menu {
param (
[string]$Title = 'UserInfo V3.1'
)
Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
Read-Host # This can be output from your function
Write-Host ""
Write-Host "================ $Title ================"
Write-Host "Press 'U' for general user info."
Write-Host "Press 'P' for account and password information."
Write-Host "Press 'C' for computer info."
Write-Host "Press 'G' for Virtual Applications (AVC)."
write-Host "Press 'S' for SNOW info details"
Write-Host "Press 'Q' to Quit."
write-Host "=============================================="
}
do {
$userName = Show-Menu # which is captured here
$Selection = Read-Host "Please make a selection"
switch ($Selection) {
'U' { Get-ADUser -Identity $Username; pause }
}
} until ($Selection -eq 'q')