Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从PowerShell命令行回显预定义变量?_Powershell_Variables_Echo_Predefined Variables_Write Host - Fatal编程技术网

如何从PowerShell命令行回显预定义变量?

如何从PowerShell命令行回显预定义变量?,powershell,variables,echo,predefined-variables,write-host,Powershell,Variables,Echo,Predefined Variables,Write Host,我可以从PowerShell命令行编写主机自定义变量,但预定义变量的工作方式不同 响应预定义变量的正确方法是什么 写入主机$path有效。 写入主机$PSScriptRoot没有 这是我的密码 powershell.exe -ExecutionPolicy ByPass -Command "& { $path = """test"""; Write-Host $path; Write-Host $PSScriptRoot; }" 我希望将当前脚本的父目录作为变量 类似这样的东西$Roo

我可以从PowerShell命令行
编写主机
自定义变量,但预定义变量的工作方式不同

响应预定义变量的正确方法是什么

写入主机$path
有效。
写入主机$PSScriptRoot
没有

这是我的密码

powershell.exe -ExecutionPolicy ByPass -Command "& { $path = """test"""; Write-Host $path; Write-Host $PSScriptRoot; }"
我希望将当前脚本的父目录作为变量


类似这样的东西
$RootPath=Split Path(Split Path$PSScriptRoot-Parent)-Parent

如果当前位置是您想要的,这对我来说一直都很有效

(Get-Location).path
如果您还想知道其他预定义变量的名称,那么Write host应该可以工作


Get Variable
或干脆
Variable
将显示所有预定义变量。如果查看
$PSScriptroot
,您会发现它是空的。这就是为什么它似乎不适合您的原因。

$PSScriptRoot
是为文件中的脚本定义的。您没有运行基于文件的脚本,因此
$PSScriptRoot
为空。如果需要当前目录,请使用
pwd
[Environment]::CurrentDirectory
(取决于您是想要PowerShell的意见还是操作系统的意见)。@Jeroenmoster请提供这两个目录的示例?
写入主机(拆分路径(pwd)-父级)
?或者先设置一个变量:
$path=pwd
。这是使用cmdlet的输出作为另一个cmdlet的输入的基本情况。@Jeroenmoster我搜索此解决方案的时间超过了3个小时。我真的很感激。