如何在PowerShell中自动加载模块

问题描述 投票:0回答:1

在 Windows PowerShell(版本 5.1.19041.4894)中,我应该怎么做才能加载模块?这 位于

$Env:PSModulePath
D:\Users\theking2\OneDrive\Documents\WindowsPowerShell\Modules;.....

我期望加载的模块位于

D:\Users\theking2\OneDrive\Documents\WindowsPowerShell\Modules\WatchFile
中,称为
WatchFile.psm1
并以

开头
# Define the file path to monitor
function Watch-File {
    [cmdletbinding()]
    Param (
        [string]
        $Path
    )

但是,命令

Watch-File
不会被识别为 cmdlet、函数、脚本文件或可操作程序的名称。

powershell powershell-module
1个回答
0
投票

您应该按照以下步骤让模块自动加载...

1.保存扩展名为.psm1的PowerShell脚本。 脚本和保存脚本的目录使用相同的名称。

2.你应该导出该函数

Export-ModuleMember -Function myfunc

下面是一个例子:

Function get-dt(){
write-host "$(get-date)"
}


Export-ModuleMember -Function get-dt

将以上代码另存为

myscripts.psm1
到文件夹中
myscripts

© www.soinside.com 2019 - 2024. All rights reserved.