I've done lot of Googleing but have not found a solution to my problem. I'm rebuilding a tool we have at work for our ServiceDesk but have run in to a problem.
I use menus and submenus with a switch-block. The menues themself are functions within the same file. However apart from the "numbering" of submenus I also have a few other commands set as functions that I want to work as well. I've tried declaring functions above the switch in the same document and also tried using a differet file to call on these functions but I can't make it work.
Function sys
{
Param (
[string]$computer
)
systeminfo /s $computer
}
# Visa huvudmeny
Function ShowMenu
{
Write-Host "
Kommandon:
1 - Alternativ för datorer
2 - Alternativ för användare
3 - Övriga alternativ
"
}
# Visa Datorer-meny
Function subComputer
{
Write-Host "
Kommandon:
rem [datornamn] - se vem som är inloggad, tryck på y för att koppla upp dig
rdp [datornamn] - Starta rdp och koppla upp mot dator
rc [datornamn] - mappa upp användarens dator
mac [datornamn] - hämta datorns MAC-adress sys [datornamn] - se information om datorn/systemet
update [datornamn] - kör GPUpdate /force på användarens FMAP
reboot [datornamn] - Starta om användarens FMAP
ping [datornamn] - pinga användares dator
pingt [datornamn] - pinga användares dator (pingsnurra), avbryt med Ctrl-C
q - tillbaka till huvudmenyn
"
}
# Visa Användare-meny
Function subUser
{
Write-Host "
Kommandon:
password - starta lösenordsgeneratorn
q - tillbaka till huvudmenyn
"
}
# Visa Övrigt-meny
Function subMisc
{
Write-Host "
Kommandon:
remlog - Visa loggfilen för rem
rdplog - Visa loggfilen för rdp
archive - Arkivera loggfilerna
gw - öppna fönstret för att kunna pinga gateway
password - starta lösenordsgeneratorn
q - tillbaka till huvudmenyn
"
}
do
{
Clear-Host
ShowMenu
$menuItem = Read-Host
switch($menuItem)
{
"1"
{
do{
Clear-Host
subComputer
$subComputer = Read-Host
switch($subComputer)
{
"1"{subComputer}
"2"{subUser}
"3"{subMisc}
}
}
until ($subComputer -eq "q")
}
"2"
{
do{
Clear-Host
subUser
$subUser = Read-Host
switch($subUser)
{
"1"{subComputer}
"2"{subUser}
"3"{subMisc}
}
}
until ($subUser -eq "q")
}
"3"
{
do{
Clear-Host
subMisc
$subMisc = Read-Host
switch($subMisc)
{
"1"{subComputer}
"2"{subUser}
"3"{subMisc}
}
}
until ($subMisc -eq "q")
}
default{. 'C:\Temp\SDV 4.2 Lite\WindowsPowerShell\moduler\func.ps1'}
}
}
until ($menuItem -eq "q")
Right now I have placed the external functionfile within the default but that does not work. I don't know where to put it to make it call my other functions. It accepts 1, 2, 3 and q with no problem.
You need to dot-source the func.ps1
file before your code reaches the point where it needs to invoke the functions - doing it before entering the loop is probably what you want here:
# dot-source function definitions, discard any output from the script itself
$null = . 'C:\Temp\SDV 4.2 Lite\WindowsPowerShell\moduler\func.ps1'
do
{
# ...
}until($menuItem -eq "q")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.