0

I've created a PS script in the past and I would like it to be more interactive.

This is the script I've created.

$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)

And I want to put it in to this script but I'm figuring out how. As in the first script the user must first fill in the user ID. Afterwards he get should get the options U,P,... Each option should be executing a command. An example of which command you can find in the first script like: #computer info# IF I haver already one command running I'll figure the rest out myself. How can I do this? Also the menu should come back after the option has been chosen.

Thanks in advance!

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
 }
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Toon
  • 29
  • 8

1 Answers1

0

I would use a PromptForChoice method, if a user select a wrong option, question will be asked again :

$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
       }
   }
}

Notice the & sign before the letter to be used (I have not used same letters as your code). This sign could be used inside the choice text, for example "Account and &password infos" means that you need to use the letter p to select this option.

The value returned by PromptForChoice is the selected index of the choice array.

CFou
  • 978
  • 3
  • 13