首页 > 解决方案 > 如何在linux中调用powershell函数

问题描述

PowerShell在 Linux 上安装并创建了如下所示的自定义函数并将其另存为test.ps1,现在如何ttr通过将值传递给$file和来调用该函数$name

#!/usr/bin/env pwsh

function ttr {
    Param(
             $file,
             $name
         )
    Write-Host $file
    Write-Host $name
}

标签: powershell

解决方案


我确信可能有其他方法,但这个对我有用

将以下文件保存在/tmp/testscript.ps1

#!/usr/bin/env pwsh
function New-ttr{

    Param(
             $file,
             $name
         )
    write-host $file
    write-host $name
}

创建了另一个像下面这样的脚本并保存在/tmp/callscript.ps1

#!/usr/bin/env pwsh                                                                                                                                                                                                
import-module /tmp/testscript.ps1     
New-ttr -file "Test call in PSCore" -name "Testing"

然后

chmod 777 callscript.ps1

./tmp/callscript.ps1

推荐阅读