Powershell profile.ps1如何导入脚本和函数

Powershell profile.ps1如何导入脚本和函数,powershell,Powershell,我有一些自己制作的函数和脚本。问题是,我不确定保存在哪里以及如何加载 在本地配置文件中,我有以下内容: C:\Users\Plazma\Documents\WindowsPowerShell\Microsoft.PowerShell\U profile.ps1 . c:\Users\Plazma\Documents\WindowsPowerShell\Get-serialnumber.ps1 . c:\Users\Plazma\Documents\WindowsPowerShell\Invoke

我有一些自己制作的函数和脚本。问题是,我不确定保存在哪里以及如何加载

在本地配置文件中,我有以下内容: C:\Users\Plazma\Documents\WindowsPowerShell\Microsoft.PowerShell\U profile.ps1

. c:\Users\Plazma\Documents\WindowsPowerShell\Get-serialnumber.ps1
. c:\Users\Plazma\Documents\WindowsPowerShell\Invoke-AutoIPAssigning.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Reset-Password.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Send-Email.ps1
为此:

Import-Module c:\Users\Plazma\Documents\WindowsPowerShell\Get-serialnumber.ps1 -Force
Import-Module c:\Users\Plazma\Documents\WindowsPowerShell\Invoke-AutoIPAssigning.ps1 -Force
Import-Module C:\Users\Plazma\Documents\WindowsPowerShell\Reset-Password.ps1 -Force
Import-Module C:\Users\Plazma\Documents\WindowsPowerShell\Send-Email.ps1 -Force
在这些文件中,我有函数/脚本。这是一个很好的实践,或者我应该以另一种方式导入它?是否使用ForEach加载此位置中的所有文件?
如果一个函数将使用另一个函数(例如总是在重置密码后发送电子邮件),我应该如何导入它

我从未见过用于从脚本导入定义的
导入模块,我甚至不知道这是否可行。通常,人们会从他们的
$profile
中点源
库脚本,如下所示:

Microsoft.Powershell\u profile.ps1

. c:\Users\Plazma\Documents\WindowsPowerShell\Get-serialnumber.ps1
. c:\Users\Plazma\Documents\WindowsPowerShell\Invoke-AutoIPAssigning.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Reset-Password.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Send-Email.ps1
Dot sourcing在调用会话的上下文中执行脚本,因此在脚本范围内定义的任何内容(如函数或变量)都可以在父会话中使用



但是,如果您在网络中携带大量这些脚本,最好的解决方案是将它们写入(一个或多个)模块中,因为模块更便于携带,如果您不想上传到公众中,您也可以将它们写入。

我从未见过用于从脚本导入定义的
导入模块
,我甚至不知道这是否有效。通常,人们会从他们的
$profile
中点源
库脚本,如下所示:

Microsoft.Powershell\u profile.ps1

. c:\Users\Plazma\Documents\WindowsPowerShell\Get-serialnumber.ps1
. c:\Users\Plazma\Documents\WindowsPowerShell\Invoke-AutoIPAssigning.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Reset-Password.ps1
. C:\Users\Plazma\Documents\WindowsPowerShell\Send-Email.ps1
Dot sourcing在调用会话的上下文中执行脚本,因此在脚本范围内定义的任何内容(如函数或变量)都可以在父会话中使用


然而,如果你在网络上有很多这样的脚本,最好的解决方案是将它们写入(一个或多个)模块中,因为模块更易于移植,如果你不想上传到公众中,你也可以使用它们