如何在 PowerShell 脚本中重用/导入脚本代码? [英] How can I re-use/import script code in PowerShell scripts?

查看:63
本文介绍了如何在 PowerShell 脚本中重用/导入脚本代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个与我的 以前的脚本,但这次我必须读取 CSV 文件而不是 XML 文件.我的计划是创建一个 PowerShell 脚本,该脚本具有两个脚本所需的通用功能,并在两个主文件中重复使用此通用脚本文件.

I have to create a PowerShell script which does exactly same thing as my previous script, but this time I have to read a CSV file instead of an XML file. My plan is to create a PowerShell script which has common functions required for both scripts and re-use this common script file in both main files.

假设我在 C:\ 驱动器的 2 个目录中创建了 2 个主要文件,并将我的公共文件和其他 3rd 方库保存在 D 的文件夹中:\ 驱动器,例如C:\script_1_folder\Script1.ps1C:\script_2_folder\Script2.ps1 和公共文件和第三个rd 方库将在<代码>D:\script_common.

Suppose I create 2 main files in 2 directories in C:\ drive and keep my common file and other 3rd party libraries in a folder of D:\ drive, e.g. C:\script_1_folder\Script1.ps1, C:\script_2_folder\Script2.ps1 and common file and 3rd party libraries will be in D:\script_common.

  1. 我如何在我的主文件中调用\重新使用公共文件(如何获取路径,我是否必须创建一个公共文件的实例以及我如何使用它)

  1. How do I call\re-use common file in my main files (how to get the path, do I have to create an instance of common file and how do I use it)

$script_path    = $myinvocation.invocationname;      
$script_folder  = split-path $script_path -parent;
write-host  $script_folder  
$script_name    = split-path $script_path -leaf;      
$current_folder = [system.io.directory]::getcurrentdirectory()    
[system.io.directory]::setcurrentdirectory($script_folder)
Set-Location $script_folder
add-type -path ".\AlexFTPS-1.1.0\AlexPilotti.FTPS.Client.dll"

$path = (split-path $MyInvocation.MyCommand.Path) 
$loggerPath = $path + "\Logger\release\Logger.ps1";
.$loggerPath; 
$logger = Logger;   
$logger.load($path + "\Logger\config\log4ps.xml","log.log"); 

对于我的问题,最好的方法是什么?

and what is the best way to do it with regard to my problem?

如何在 windows 临时文件夹中创建临时文件夹?

How do I create a temp folder in windows temp folder?

推荐答案

Powershell 中的通用代码

您可以将要包含的代码放在不同的 PS1 文件中,然后点源"该文件以将其包含在当前范围内:

Common Code In Powershell

You can just put the code you want to include in a different PS1 file, and then "dot source" that file to include it in the current scope:

. D:\script_common\MyCode.ps1

仅此而已.

您可以考虑改用模块,该模块可以使用 Import-Module cmdlet 包含在内.您可能已经使用它来处理诸如 Active Directory 之类的事情,您可以在其中执行以下操作:

You might consider using a module instead, which can be included using the Import-Module cmdlet. You might have used this to work with things like Active Directory, where you could do something like this:

Import-Module ActiveDirectory

在这种情况下,您只需要模块的名称,因为它位于一个特殊的目录中.

In that case, you only need the name of the module because it's in a special directory.

要在 Powershell 中编写自己的模块,请使用 .psm1 扩展名命名模块.通常,您不会在其中之一中执行自由浮动代码;您编写的函数随后可用于导入模块的代码.

To write your own modules in Powershell, you name the module with a .psm1 extension. Typically, you don't do free floating code in one of these; you write functions which are then available to the code which imports the module.

要从任何地方导入脚本模块,请使用完整路径:

To import a script module from anywhere, use the full path:

Import-Module D:\script_common\MyModule.psm1

模块路径

当您创建自己的模块时,您可以将它们保留在任何旧位置,然后通过它们的完整路径(如上)引用它们.还有几个 Powershell 查找模块的位置:

  • $PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules) -- 保留用于 Windows 附带的模块.不要把东西放在这里.
  • $Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
  • %ProgramFiles%\WindowsPowerShell\Modules -- 这在链接中没有提到,似乎更多地用于 Desired State Configuration 模块(可能是因为它适用于整个系统).
  • $PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules) -- Reserved for modules that ship with Windows. Do not put things here.
  • $Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
  • %ProgramFiles%\WindowsPowerShell\Modules -- this isn't mentioned in the link, and seems to be used more for Desired State Configuration modules (probably because it applies to the entire system).

这些是默认值,但 Powershell 使用自己的名为 PSModulePath 的环境变量来确定要查找的位置,与 PATH 非常相似,您可以将自己的文件夹添加到那个变量.

These are defaults, but Powershell uses its own environment variable called PSModulePath to determine where to look, and much like PATH you can add your own folder(s) to that variable.

这让您可以将模块保存在自己的位置.请参阅链接以获取有关如何构建文件夹以及如何命名的更多信息.

That lets you keep your modules in your own location. Do see the link for more info on how to structure your folders and how to do naming.

至于将您的模块和第 3 方"模块保持在同一位置,这取决于第 3 方的内容.它可能会在自己的位置安装自己的模块并修改路径,也可能只是让你把它们放在你想要的地方.

So as far as keeping your modules and "3rd party" modules in the same place, that depends on the 3rd party stuff. It may install its own modules in its own place and modify the path, or it may just let you put them wherever you want.

您可以使用 TEMPTMP 环境变量来获取临时文件夹的路径.要在 Powershell 中检索它们,请使用 $env:TEMP$env:TMP.

You can use the TEMP or TMP environment variables to get the path of the temp folder. To retrieve them in Powershell, use $env:TEMP or $env:TMP.

您必须想出一个唯一的文件夹名称才能在其中创建.一种方法可能是使用 GUID:

You'll have to come up with a unique name of a folder to create in there. One way to do that might be to use a GUID:

$dirName = [System.Guid]::NewGuid().ToString()
New-Item -Path "$($env:TEMP)\$dirName"

这篇关于如何在 PowerShell 脚本中重用/导入脚本代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆