PSScriptRoot is null: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Error == '''There is no error message, but $PSScriptRoot is empty/null.''' == Cause == ''This occurs because you are using ISE and when ISE is launched, $…“)
 
 
Zeile 10: Zeile 10:
 
*If you want to get the path in either shell or ISE you could use something like this:
 
*If you want to get the path in either shell or ISE you could use something like this:
 
<source lang="powershell">
 
<source lang="powershell">
If ($GLOBAL:psISE) {
+
If ($SCRIPT:psISE) {
     Split-Path -Path $GLOBAL:psISE.CurrentFile.FullPath         
+
     Split-Path -Path $SCRIPT:psISE.CurrentFile.FullPath         
 
} else {
 
} else {
     $GLOBAL:PSScriptRoot
+
     $SCRIPT:PSScriptRoot
 
}
 
}
 
</source>
 
</source>

Aktuelle Version vom 16. August 2021, 09:29 Uhr

Error

There is no error message, but $PSScriptRoot is empty/null.


Cause

This occurs because you are using ISE and 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.


Solution

  • If you want to get the path in either shell or ISE you could use something like this:
If ($SCRIPT:psISE) {
     Split-Path -Path $SCRIPT:psISE.CurrentFile.FullPath        
} else {
     $SCRIPT:PSScriptRoot
}