Powershell-包含代码的不同方法?

Powershell-包含代码的不同方法?,powershell,scripting,include,Powershell,Scripting,Include,我有3个脚本: C:\Powershell\Test1.ps1: WRITE-HOST“我的名字是”$MyInvocation.MyCommand.Name . “C:\Powershell\Test2.ps1” C:\Powershell\Test2.ps1: WRITE-HOST“我的名字是”$MyInvocation.MyCommand.Name . “C:\Powershell\Test3.ps1” C:\Powershell\Test3.ps1: WRITE-HOST“我的

我有3个脚本:

  • C:\Powershell\Test1.ps1:
    
    WRITE-HOST“我的名字是”$MyInvocation.MyCommand.Name
    . “C:\Powershell\Test2.ps1”
    
  • C:\Powershell\Test2.ps1:
    
    WRITE-HOST“我的名字是”$MyInvocation.MyCommand.Name
    . “C:\Powershell\Test3.ps1”
    
  • C:\Powershell\Test3.ps1:
    
    WRITE-HOST“我的名字是”$MyInvocation.MyCommand.Name
    
这是输出:

PS C:\Powershell>\Test1.ps1
我的名字是Test1.ps1
我的名字是Test2.ps1
我的名字是Test3.ps1

我希望Test1.ps1将代码包含在自身中,而不是将它们作为脚本调用在自身中

如果可能,这是我想要的输出类型:

PS C:\Powershell>\Test1.ps1
我的名字是Test1.ps1
我的名字是Test1.ps1
我的名字是Test1.ps1


这可能吗?最初的Test1脚本被调用,因此无论它之后调用什么,这都应该是整个脚本的名称?

我想您可以使用Get-Content cmdlet。试着这样做:

WRITE-HOST "My Name is $($MyInvocation.MyCommand.Name) `n`n $(Get-Content $($MyInvocation.MyCommand.Name))"
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

我想您可以使用Get-Content cmdlet。试着这样做:

WRITE-HOST "My Name is $($MyInvocation.MyCommand.Name) `n`n $(Get-Content $($MyInvocation.MyCommand.Name))"
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

您可以执行以下操作来读取脚本的内容:

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

WRITE-HOST "My Name is $($MyInvocation.MyCommand.Name) `n`n $(Get-Content $($MyInvocation.MyCommand.Name))"
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

您可以执行以下操作来读取脚本的内容:

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

WRITE-HOST "My Name is $($MyInvocation.MyCommand.Name) `n`n $(Get-Content $($MyInvocation.MyCommand.Name))"
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

您基本上希望
$MyInvocation.MyCommand.Name
包含不同的脚本文件,而不是文件本身?为什么要这样做?
$MyInvocation
在多个文件中不一致。也许您可以使用另一个脚本来连接
Test1.ps1
Test2.ps1
Test3.ps1
?它们将在相同的上下文中运行。您基本上希望
$MyInvocation.MyCommand.Name
包含不同的脚本文件,而不是文件本身?为什么要这样做?
$MyInvocation
在多个文件中不一致。也许您可以使用另一个脚本来连接
Test1.ps1
Test2.ps1
Test3.ps1
?那么,它们都将在相同的上下文中运行。