Call Powershell script from another Powershell script

powershell

I have a problem.
I develop some powershell scripts to perform actions on my Databases.
In theory this PS should execute when them are called by the main powershell like the calls in .bat files.

My idea is the team support have in one folder all PS1 scripts and they only run the main PS1 and provide the credentials and other values requires to run the scripts.

I tried using the Invoke-expression or Invoke-command to call the others PS1 but the main script find the files but don't execute any action.
I would like to know if you know what cause the others scripts don't run

I have the main PS1 named CREATE_CNFMGR_SCHEMA.ps1 within this PS1 I call another PS1 named for example testing.ps1 or FileStreamAccess_Validation.ps1
The script code find the PS1 files but not perform anything.

This is the code to call the others PS1 in my main PS1:

param(
[String] $pathfolder, 
[Parameter(Position=0, Mandatory=$true)]
[string] $svrinstance,
[Parameter(Position=1, Mandatory=$true)]
[string] $dbuser,
[Parameter(Position=2, Mandatory=$true)]
[string] $dbusupassw,
[Parameter(Position=3, Mandatory=$true)]
[string] $dbname
)

$sqlinstance    = $svrinstance
$userdb         = $dbuser
$passdb         = $dbusupassw  
$namedb         = $dbname

try
{
 Write-Output "Baseline Script to Set Up Confirmation Manager Database"
    $pathfolder = Split-Path -Parent $MyInvocation.MyCommand.Definition

if($pathfolder -ne $null)
{
    Write-Host $pathfolder
    [string]$psfile = "c:\temp\log\FileStreamAccess_Validation.ps1"
    Write-Host $psfile
    Invoke-Command -filepath $psfile    
    Invoke-Expression $psfile       
 }  

}
catch
{

}

Best Answer

I solve my case using $PSScriptRoot, I hope my answer can help somebody more

The final code ends:

param(
[String] $pathfolder, #variable used to get the actual location of the folder from where is execute the PS1 file. 
[Parameter(Position=0, Mandatory=$true)]
[string] $svrinstance,
[Parameter(Position=1, Mandatory=$true)]
[string] $dbuser,
[Parameter(Position=2, Mandatory=$true)]
[string] $dbusupassw,
[Parameter(Position=3, Mandatory=$true)]
[string] $dbname
)

$sqlinstance    = $svrinstance
$userdb         = $dbuser
$passdb         = $dbusupassw  
$namedb         = $dbname

try
{

 Write-Output "Baseline Script to Set Up Confirmation Manager Database"
 $pathfolder = Split-Path -Parent $MyInvocation.MyCommand.Definition

 if($pathfolder -ne $null)
 {
    Write-Host $pathfolder
    $psfile = $PSScriptRoot + ".\FileStreamAccess_Validation.ps1"
    .$psfile $sqlinstance $userdb $passdb

 }

}
catch
{

}