Split-Path -Path $ PSCommandPath -Parent - PullRequest
0 голосов
/ 12 июня 2018

Я использую PowerShell Вер.5.1.14393.2248 Мой сценарий находится по пути: C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker.

Я хочу получить родительский путь, например: C:\Anwendungen\PowershellAddOn\Modules.Когда я бегу:

$destination = Split-Path -Path $PSCommandPath -Parent
$destination 

Я все равно получу C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker вместо C:\Anwendungen\PowershellAddOn\Modules

Что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 12 июня 2018

Чтение about_Automatic_Variables :

$PSCommandPath
   Contains the full path and file name of the script that is being run. 
   This variable is valid in all scripts.
…

$PSScriptRoot
   Contains the directory from which a script is being run. 

   In Windows PowerShell 2.0, this variable is valid only in script modules
   (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

Использование:

$destination = Split-Path -Path (Split-Path -Path $PSCommandPath -Parent) -Parent

или в Windows PowerShell 3.0 и более поздних версиях:

$destination = Split-Path -Path $PSScriptRoot -Parent
0 голосов
/ 12 июня 2018

Вы должны проверить вывод $ PSCommandPath при запуске скрипта.Это, вероятно, не указывает на C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker, так как при вызове:

Split-Path C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker

Вы получите C:\Anwendungen\PowershellAddOn\Modules.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...