Offload your powershell code

Here’s an easy way to offload your powershell code onto any machine with an internet connection, and to create command collection $variables:

#Establish your root, can be .\ for relative path
$root = 'C:\yourRoot'        

#Set your local file(s)
$LocalFile = "$root\yourcodefile"        

#Test the location of your code file(s)
$test = Test-Path -Path $LocalFile 

#If did not pass
if (!$test) { 

#Specify location of remote file
$RemoteFile = 'http://cdn.yoursite.com/yourCodeFile(s)'                         

#Start downloading
Invoke-WebRequest -Uri $RemoteFile -OutFile $LocalFile
 
}  

#If sfx, then self-extract silently
Start-Process -FilePath $LocalFile -ArgumentList "-o$root-y"  -Wait 

#Set location to root now
Set-Location -Path $root

#Your code collection should have a .\global.psm1
Import-Module ".\global.psm1" -DisableNameChecking 

#Start storing the outputs of your global importation, as global iterable collections. You can call these collections anytime in your session through the variables.

Set-Variable global -Scope Global -Value (Get-Module global) 
Set-Variable commands -Scope Global -Value ($global.ExportedCommands) 
Set-Variable commandsList -Scope Global -Value ($commands.Keys)
0
A file remover function in PowerShell Automating the import-module call

No Comments

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *