Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I re-use/import script code in PowerShell scripts?

Tags:

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.

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

    What is the difference between

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

    and

    $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?

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

like image 893
bluelabel Avatar asked Nov 25 '14 23:11

bluelabel


People also ask

How do I run a script from another PS script?

You don't need Start-Process. PowerShell scripts can run other scripts. Just put the command that runs the second script as a command in the first script (the same way as you would type it on the PowerShell command line).

What are you doing when you use the import-Module cmdlet?

Description. The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module.

How do I run an import-module in PowerShell?

In PowerShell 2.0, you can import a newly-installed PowerShell module with a call to Import-Module cmdlet. In PowerShell 3.0, PowerShell is able to implicitly import a module when one of the functions or cmdlets in the module is called by a user.


2 Answers

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" 
like image 50
briantist Avatar answered Oct 22 '22 06:10

briantist


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 
like image 35
Paul Avatar answered Oct 22 '22 08:10

Paul