Tuesday, November 17, 2015

PowerShell Tips: How to fix “split-path: Cannot find a provider with the name”


If we invoke PowerShell function from another PowerShell script and PowerShell function contains retrieve script path then it will gives this error.



The reason is function executes from another PowerShell and that function does not have knowledge of execution context of parent script.  To fix that we can retrieve the script path from PowerShell script which invoke the function and pass it to the function as a parameter.

Modified Function

function Deploy{

    param([string]$scriptPath)

    Process{
    $CrmCommanFile = join-path $scriptPath "\CRMCommand.exe"
    #Do what you want
    }
}


Modified PowerShell script which invoke the function

#$ErrorActionPreference = "stop"

# Find the current directory
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

#Deploy CRM Customizations
. .\DeployScript.ps1
Deploy -scriptPath $scriptPath



1 comment:

  1. Thank you for your articles that you have shared with us. Hopefully you can give the article a good benefit to us. Stream

    ReplyDelete