PowerShell PSScriptRoot 为空 [英] PowerShell PSScriptRoot is null

查看:31
本文介绍了PowerShell PSScriptRoot 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 $PSScriptRoot 时,它返回 null.我使用的是 PS 版本 4.

When I run $PSScriptRoot it returns null. I am using PS version 4.

$val  = Join-Path -Path $PSScriptRoot WebPlatformInstaller_amd64_en-US.msi

错误

Join-Path:无法将参数绑定到参数路径",因为它是一个空字符串.

Join-Path : Cannot bind argument to parameter 'Path' because it is an empty string.

推荐答案

如果使用 ISE,请使用:

If using ISE use:

$psISE.CurrentFile.FullPath

ISE 启动时,会创建 $psISE,可用于确定 ISE 实例的当前路径.这是在 3.0 版本中引入的.

When ISE is launched, $psISE is created and can be used to determine the current path of the ISE instance. This was introduced in version 3.0.

参见 ISE 对象模型层次结构

如果你想在 shell 或 ISE 中获取路径,你可以使用这样的东西:

If you wanted to get the path in either shell or ISE you could use something like this:

if ($psISE)
{
    Split-Path -Path $psISE.CurrentFile.FullPath        
}
else
{
    $global:PSScriptRoot
}

这篇关于PowerShell PSScriptRoot 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆