如何在另一个*.ps1中运行powershell脚本

如何在另一个*.ps1中运行powershell脚本,powershell,Powershell,假设我以前在代码中建立了get game函数 并且已经创建了C:\Stock\u Market\$Username.ps1,并且该文件位于同一个C:\Stock\u Market文件夹中,为什么系统找不到$Username.ps1 我如何从$Username.ps1调用命令以在该文件中运行 function Log-In { Clear-Host $Username = Read-Host "Username" $Password = Read-Host "Passwo

假设我以前在代码中建立了get game函数 并且已经创建了C:\Stock\u Market\$Username.ps1,并且该文件位于同一个C:\Stock\u Market文件夹中,为什么系统找不到$Username.ps1

我如何从$Username.ps1调用命令以在该文件中运行

function Log-In {
    Clear-Host

    $Username = Read-Host "Username"
    $Password = Read-Host "Password"
    $filepath = "C:\Stock_Market\$Username.ps1"

    If (Test-Path $filepath) {
        ./$Username.ps1
    }
    Else {
        Write-Host "$Username does not exist"
    }

    # Logging in

    if ($Password -eq $realpass) {
        get-game
    }
    Else {
        Write-Host "Wrong Password!"
        pause
        Clear-Host
        Log-In
    }
}

为此,您需要在脚本名称前面使用符号and,这将作为命令而不是cmdlet或字符串调用字符串

它被称为呼叫接线员


有关更多信息和语法,请参阅。

就地调用它
/$Username.ps1
或它自己的作用域
&./$Username.ps1
我猜。因为你没有从c:\stock\u market文件夹运行脚本,所以
\
引用了错误的位置?谢谢你,那个符号起了很大的作用!只需设计一个具有所需功能的脚本
$something=Function InScript
然后您可以在脚本的后面使用
$something