1
votes

I have 3x scripts:

  • C:\Powershell\Test1.ps1: WRITE-HOST "My Name is" $MyInvocation.MyCommand.Name . "C:\Powershell\Test2.ps1"
  • C:\Powershell\Test2.ps1: WRITE-HOST "My Name is" $MyInvocation.MyCommand.Name . "C:\Powershell\Test3.ps1"
  • C:\Powershell\Test3.ps1: WRITE-HOST "My Name is" $MyInvocation.MyCommand.Name

This is the output: PS C:\Powershell> .\Test1.ps1 My Name is Test1.ps1 My Name is Test2.ps1 My Name is Test3.ps1

I would like Test1.ps1 to INCLUDE the code within itself instead of calling them as scripts inside itself.

If possible, this is the sort of output I'd like: PS C:\Powershell> .\Test1.ps1 My Name is Test1.ps1 My Name is Test1.ps1 My Name is Test1.ps1

Is this possible? The original Test1 script is called, so this should the name of the script throughout, regardless of what it then calls afterwards?

2
You basically want that $MyInvocation.MyCommand.Name contains a different script file then the file itself? Why do you want to do that? - Martin Brandl
$MyInvocation will not be consistent across multiple files. Maybe you can use another script to concatenate Test1.ps1, Test2.ps1 and Test3.ps1 ? They would all be running in the same context then. - sodawillow

2 Answers

0
votes

You could use Get-Content cmdlet I guess. Try something like this:

WRITE-HOST "My Name is $($MyInvocation.MyCommand.Name) `n`n $(Get-Content $($MyInvocation.MyCommand.Name))"
0
votes

You could do the following to read the contents of your scripts:

foreach ($File in (Get-Item -Path C:\PowerShell\* -Filter '*.ps1' | Select -ExpandProperty FullName))
{
    Write-Output "File: $File"
    Get-Content "$File"
}

The output would look like this:

PS C:\> foreach ($File in (Get-Item -Path C:\PowerShell\* -Filter '*.ps1' | Select -ExpandProperty FullName))
{
    Write-Output "File: $File"
    Get-Content "$File"
}
File: C:\PowerShell\test.ps1
test1 contents
File: C:\PowerShell\test2.ps1
test2 contents
File: C:\PowerShell\test3.ps1
test3 contents