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
{

}

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
{

} 
  • It becomes a more useful answer if you describe what $PSScriptRoot provides. As well, noting that it only works in PowerShell 3.0 or higher as well. – Shawn Melton Nov 19 '15 at 18:40
  • Hi @ShawnMelton the $PSScriptRoot is a short way to use the command $MyInvocation.MyCommand.Path is most simple to remember but this command only works when you are using this one withing a .ps1 or .psm1 files, If you try to invoke this command like a cmdlet in command prompt line you will get an error because this one does not exist on command shell. Regards – jmonterohn Sep 11 '17 at 22:07

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.