如何在 PowerShell 脚本中重用/导入脚本代码?

我必须创建一个 PowerShell 脚本,它与我的 前情提要完全相同,但是这次我必须读取 CSV 文件而不是 XML 文件。我的计划是创建一个 PowerShell 脚本,它具有两个脚本所需的公共函数,并在两个主文件中重用这个公共脚本文件。

假设我在 C:\驱动器的2个目录中创建2个主文件,并将我的公共文件和其他3路德方库保存在 D:\驱动器的一个文件夹中,例如 C:\script_1_folder\Script1.ps1C:\script_2_folder\Script2.ps1和公共文件,3路德方库将保存在 D:\script_common中。

  1. 如何在主文件中调用重用公共文件(如何获取路径,是否需要创建公共文件的实例以及如何使用它)

    有什么区别

    $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");
    

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

  2. 如何在 Windows 临时文件夹中创建临时文件夹?

108567 次浏览

You should be able to dot source the script like that:

. "C:\script_common\script.ps1"

after that you can use all the functions like they were in the script you are running.

But... the better way to do it would be to create a module with your common functions (How to here: Scripting Guy´s Blog. (TLDR Version: place functions into psm1 file, put into modulepath, import using Import-Module, profit)

For creating a folder:

New-Item C:\Temp\yourfolder -type directory

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

That's all there is to that.

Using a Module

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.

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

Module Paths

When you create your own modules, you can keep them any old place and then refer to them by their full path (as above). There are also several locations that Powershell looks for modules:

  • $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).

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.

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.

Creating a Temp Folder

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.

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"

Here is my attempt to create a template system in powershell : https://github.com/kayasax/PowershellTemplate
It allows to reuse functions you save in the repository by using tags in the template eg :
<include logging/log>
The content of the file log.ps1 found in the logging directory of the function repository will be inserted when tranforming template to script