我正在处理Powershell ISE中的一些脚本,我需要PS脚本根在Powershell和Powershell ISE中的行为相同。我做了下面的例子来说明不同之处。
Caller.ps1
if ($psISE)
{
$directory = Split-Path -Path $psISE.CurrentFile.FullPath
Write-Host "psISE : " $directory
}
else
{
$directory=$PSScriptRoot
Write-Host "not psISE : " $directory
}
write-host "---------- in dir Scripts --------------"
try {& "$directory\Scripts\Called.ps1"}
catch {"FAILED"}
Called.ps1
if ($psISE)
{
$directory = Split-Path -Path $psISE.CurrentFile.FullPath
Write-Host "psISE : " $directory
}
else
{
$directory=$PSScriptRoot
Write-Host "not psISE : " $directory
}
Powershell的结果
PS C:\> .\Example\Caller.ps1
not psISE : C:\Example
---------- in dir Scripts --------------
not psISE : C:\Example\Scripts
Powershell ISE的结果
PS C:\> C:\Example\Caller.ps1
psISE : C:\Example
---------- in dir Scripts --------------
psISE : C:\Example
第一次发布的例子和相关问题:PowerShell PSScriptRoot is null
发布于 2022-01-24 07:12:10
只要文件保存到磁盘上,$PSScriptRoot就可以在PowerShell ISE中工作。该文件可以使用F5运行,但不能使用F8。
示例代码演示的问题是,psISE.CurrentFile.FullPath不从Called.ps1文件中更新。
https://stackoverflow.com/questions/70835097
复制相似问题