0

我过去创建了一个 PS 脚本,我希望它更具交互性。

这是我创建的脚本。

$csv = Import-CSV 'C:\Users\w0vlnd\Desktop\Powershells\Computers in a specific workgroup or domain.csv'

$ADprops = @(
    'DisplayName'
    'Mail'
    'LastBadPasswordAttempt'
    'AccountExpirationDate'
    'PasswordLastSet'
    'Enabled'
)


 
$filter = @(
    $ADprops
    @{ 
        Name = 'Account active'
        Expression = {$Account}
    }, @{

        Name = 'Password Changeable'
        Expression = { $changeable }
    }, @{
        Name = 'Password Expires'
        Expression = { $expires }
    }, @{
        Name = 'Last logon'
        Expression = { $lastlogon }
    }, @{
        Name = 'PC Name'
        Expression = { $pcName }
    }, @{
        Name = 'System Boot Time'
        Expression = { $Boot }

     
    }
)



Clear-Host
do
{
    
    Write-Host "Enter the user ID: " -ForegroundColor Cyan -NoNewline
    $UserName = Read-Host
    Write-Host ""
    
    #Computer Info# 
    $pcName = $csv.Where({ $_."User ID" -match $Username })."PC Name"

    
    $boot = SystemInfo /s $pcName | Select-String -Pattern 'System Boot Time'|
    ForEach-Object { ($_ -split '\s{2,}')[1] }
    
    #Account and passowrd information# 
    $Account, $expires, $changeable, $lastlogon = net user $Username /domain |
    Select-String -Pattern 'Account active|Password Changeable|Password Expires|Last logon'|
    ForEach-Object { ($_ -split '\s{2,}')[1] }

    Get-ADUser -Identity $Username -Properties $ADprops |
    Select-Object $filter

} while ($Username -notcontains $Processes)

我想把它放到这个脚本中,但我正在弄清楚如何做。与第一个脚本一样,用户必须首先填写用户 ID。之后他应该得到选项 U,P,... 每个选项都应该执行一个命令。您可以在第一个脚本中找到哪个命令的示例,例如:#computer info# 如果我已经运行了一个命令,我将自己解决其余的问题。我怎样才能做到这一点?选择该选项后,菜单也应返回。

提前致谢!

param (
    [string]$Title = 'UserInfo V3.1'
)
Clear-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' computer information."
Write-Host "Press 'G' group memberships."
Write-Host "Press 'R' search for other user ID."
Write-Host "Press 'Q' to Quit."
}

Show-Menu –Title 'UserInfo V3.1'
$selection = Read-Host "Please make a selection"
switch ($selection)
{
   'U' {
     'You chose option #U'
 } 'P' {
     'You chose option #P'
 } 'C' {
     'You chose option #C'
 } 'G' {
     'You chose option #G'
 } 'Q' {
     'You chose option #Q'
     return
 }
4

1 回答 1

0

我会使用一种PromptForChoice方法,如果用户选择了错误的选项,则会再次提出问题:

$repeat = $true
while ($repeat)
{
   [System.Management.Automation.Host.ChoiceDescription[]]$choicelist = 
               "&User infos", 
               "&Account and password infos", 
               "&Computer infos", 
               "&Group membership",
               "&Search for another user", 
               "&Quit"
   switch ($Host.UI.PromptForChoice("Please select", "What do you want ?", $choicelist, 0))
   {
       0 {
           "User infos here"
           break
       }
       1 {
           "Account & password infos here"
           break
       }
       2 {
           "Computer infos here"
           break
       }
       3 {
           "Group membership infos here"
           break
       }
       4 {
           "Search for another user Id here"
           break
       }
       5 {
           "Exiting."
           $repeat = $false
       }
   }
}

注意&要使用的字母前的符号(我没有使用与您的代码相同的字母)。该符号可以用在选项文本中,例如"Account and &password infos"表示您需要使用字母p来选择该选项。

返回的值PromptForChoice是选择数组的选定索引。

于 2022-01-19T15:56:10.220 回答