1

I am currently working on a powershell script that maps directories along with loading database software. I have this current vbscript I am converting to powershell that is suppose to validate a temporary file path , but I am getting a little confused on what I may need to take out and what I can leave in.

Here is the original vbscript ...

'
' assure that temp version of Perl is used
'
perlPath = basePath & "install\perl\bin;"
WshShell.Environment("Process")("PATH") = perlPath & WshShell.Environment("System")("PATH")
'
' assure that temp version of Perl Lib is used
'
perlLib = basePath & "\install\perl\lib;" & basePath & "\install\perl\site\lib;"
WshShell.Environment("Process")("PERL5LIB") = perlLib

Here is what I have written in powershell so far ...

#
# assure that Oracle's version of Powershell is used
# 
$psPath = $basePath + "install\powershell\bin;" 
$sysPath = $WshShell.Environment("System") | Where-Object { $_ -match "PATH" } | 
           foreach-object {$_.Substring(9)} | Out-String
$psPos = $sysPath.contains($psPath)
if( -not ($psPos)){
    [Environment]::SetEnvironmentVariable("PATH", ($psPath + $sysPath), "Process")  
} 
#
# assure that Oracle's version of Powershell Module is used
# 
$psMod =  $homePath + "\perl\lib;" + $homePath + "\perl\site\lib;" # still need to convert 
$sysMod = $Env:PSModulePath 
$psPos = $sysMod.contains($psMod)
if( -not ($psPos)){
    [Environment]::SetEnvironmentVariable("PATH", ($psPath + $sysChk), "Process")  
}} 

The same validation is done later in the script with the "System" variables. I do have a module that I will be using, but the rest are scripts. I guess I am not sure if what I am converting is the right way to verify these pathways exist and if not to add the new pathways.

1 Answer 1

1

First of all, you should use the Join-Path cmdlet for combining a path:

$psPath = Join-Path  $basePath "install\powershell\bin" 

You can access the Pathvariable using $env:Path split it using -split ';' and select the first path entry using [0]. All in all, I would define the three path you want to set, put them into an array and iterate over it.

$powershellBin = Join-Path $basePath  "install\powershell\bin" 
$perLib = Join-Path $homePath "\perl\lib" 
$perlSiteLib = Join-Path $homePath "\perl\site\lib"

@($powershellBin, $perLib, $perlSiteLib) | foreach {
    if (-not (($env:Path -split ';')[0].Equals($_)))
     {
        [Environment]::SetEnvironmentVariable("PATH", ("{0};{1}" -f $_, $env:Path), "Process")
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the great example! Didn't realize there was a Join-Path cmdlet in powershell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.