[英]How to call/use one ps.1 script functions, variables and classes to another ps.1 files without using psm1 and full directory path?
D:\\NoName\\testfolder1\\test1.ps1
:
function restapi {
Write-Host "Initiating Rest Call"
$test_value = "1"
}
D:\\NoName\\Testfolder1\\testfolder2\\test2.ps1
:
Invoke-Expression -Command D:\NoName\testfolder1\test1.ps1
function Invoke-Rest {
restapi
Write-Host "Invoking rest call and value of test is $test_value"
}
我如何在不提供完整路径的情况下调用/导入test1.ps1
文件,就像我们在Python中那样:
import testfolder1
from .testfolder1 import test1
PowerShell本质上具有两种导入机制:
使用模块名称(如果模块位于$env:PSModulePath
) Import-Module
模块 (通过Import-Module
):
Import-Module BitsTransfer
或使用完整/相对路径:
Import-Module 'C:\\some\\folder\\foo.psm1' Import-Module '.\\subfolder\\bar.psm1'
斑点采购常规PowerShell脚本,也与完整或相对路径:
. 'C:\\some\\folder\\foo.ps1' . '.\\subfolder\\bar.ps1'
在您的情况下,您可能需要后者:
. "$PSScriptRoot\..\testfolder1\test1.ps1"
function Invoke-Rest {
restapi
Write-Host "Invoking rest call and value of test is $test_value"
}
请注意, 自动变量 $PSScriptRoot
在PowerShell v3之前不可用。 在早期版本中,您需要这样确定自己的目录:
$scriptdir = Split-Path $MyInvocation.MyCommand.Path -Parent
. "$scriptdir\..\testfolder1\test1.ps1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.