首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Powershell ISE中,$PSScriptRoot的等效性是什么?

在Powershell ISE中,$PSScriptRoot的等效性是什么?
EN

Stack Overflow用户
提问于 2022-01-24 14:04:51
回答 1查看 1.2K关注 0票数 0

我正在处理Powershell ISE中的一些脚本,我需要PS脚本根在Powershell和Powershell ISE中的行为相同。我做了下面的例子来说明不同之处。

Caller.ps1

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
if ($psISE)
{
    $directory = Split-Path -Path $psISE.CurrentFile.FullPath 
    Write-Host "psISE : " $directory       
}
else
{
     $directory=$PSScriptRoot
     Write-Host "not psISE : " $directory
}

Powershell的结果

代码语言:javascript
运行
复制
PS C:\> .\Example\Caller.ps1
not psISE :  C:\Example
---------- in dir Scripts --------------
not psISE :  C:\Example\Scripts

Powershell ISE的结果

代码语言:javascript
运行
复制
PS C:\> C:\Example\Caller.ps1
psISE :  C:\Example
---------- in dir Scripts --------------
psISE :  C:\Example

第一次发布的例子和相关问题:PowerShell PSScriptRoot is null

EN

回答 1

Stack Overflow用户

发布于 2022-01-24 15:12:10

只要文件保存到磁盘上,$PSScriptRoot就可以在PowerShell ISE中工作。该文件可以使用F5运行,但不能使用F8。

示例代码演示的问题是,psISE.CurrentFile.FullPath不从Called.ps1文件中更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70835097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档