简体   繁体   English

从 PowerShell 中的不同文件调用 function

[英]Calling function from different file in PowerShell

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.我正在重建我们为 ServiceDesk 工作的工具,但遇到了问题。

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.它可以毫无问题地接受 1、2、3 和 q。

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:您需要在代码到达需要调用函数的位置之前func.ps1文件进行点源化 - 在进入循环之前执行此操作可能是您想要的:

# 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM