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

bluelabel

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?

briantist

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"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I execute scripts in a code created powershell shell that has Write-Host commands in it?

From Dev

How can I run a Powershell script in PHP?

From Java

How can I pass an argument to a PowerShell script?

From Dev

How I can do this powershell script better?

From Dev

How can I run a Powershell script in PHP?

From Dev

How can I re-execute an external script in Angular?

From Dev

How do I re-use code between content scripts in a Chrome extension?

From Dev

How can I pass batch arguments with dots into a powershell script?

From Dev

How can I to run php script from powershell-commandline?

From Dev

How can I source variables from a .bat file into a PowerShell script?

From Dev

How can I automate running a PowerShell script that prompts UI for choice?

From Dev

How can I output all the output of a called command in Powershell script

From Dev

How can I continue lines of a script block in PowerShell?

From Dev

How can I change the headings of hash table columns in powershell script

From Dev

How can I automate running a PowerShell script that prompts UI for choice?

From Dev

How can I stop a PowerShell script from running?

From Dev

How can I hardcode specific server names in powershell script

From Dev

How can i read the contents of a powershell script running in the background?

From Dev

How can I create a log of a powershell script running in the background?

From Dev

How can I optimize this Email list powershell script

From Dev

How can I make this PowerShell script into one line

From Dev

How can I conditionally include large scripts in my ssdt post deployment script?

From Dev

How can I make a firefox add-on contentscript inject and run a script before other page scripts?

From Dev

How can I code a template in a file instead of in a script?

From Dev

How can I run my python code as a script

From Dev

How can I make my batch script add code to itself

From Dev

How can I defer or async this JS script code?

From Dev

How can I dynamically set the tracking code for Google Universal Analytics while adhering to a CSP that disallows inline scripts?

From Dev

How can I generate my desired exit code on shell commands or one-line scripts?

Related Related

  1. 1

    How can I execute scripts in a code created powershell shell that has Write-Host commands in it?

  2. 2

    How can I run a Powershell script in PHP?

  3. 3

    How can I pass an argument to a PowerShell script?

  4. 4

    How I can do this powershell script better?

  5. 5

    How can I run a Powershell script in PHP?

  6. 6

    How can I re-execute an external script in Angular?

  7. 7

    How do I re-use code between content scripts in a Chrome extension?

  8. 8

    How can I pass batch arguments with dots into a powershell script?

  9. 9

    How can I to run php script from powershell-commandline?

  10. 10

    How can I source variables from a .bat file into a PowerShell script?

  11. 11

    How can I automate running a PowerShell script that prompts UI for choice?

  12. 12

    How can I output all the output of a called command in Powershell script

  13. 13

    How can I continue lines of a script block in PowerShell?

  14. 14

    How can I change the headings of hash table columns in powershell script

  15. 15

    How can I automate running a PowerShell script that prompts UI for choice?

  16. 16

    How can I stop a PowerShell script from running?

  17. 17

    How can I hardcode specific server names in powershell script

  18. 18

    How can i read the contents of a powershell script running in the background?

  19. 19

    How can I create a log of a powershell script running in the background?

  20. 20

    How can I optimize this Email list powershell script

  21. 21

    How can I make this PowerShell script into one line

  22. 22

    How can I conditionally include large scripts in my ssdt post deployment script?

  23. 23

    How can I make a firefox add-on contentscript inject and run a script before other page scripts?

  24. 24

    How can I code a template in a file instead of in a script?

  25. 25

    How can I run my python code as a script

  26. 26

    How can I make my batch script add code to itself

  27. 27

    How can I defer or async this JS script code?

  28. 28

    How can I dynamically set the tracking code for Google Universal Analytics while adhering to a CSP that disallows inline scripts?

  29. 29

    How can I generate my desired exit code on shell commands or one-line scripts?

HotTag

Archive