Загрузите все функции в PowerShell из определенного каталога

В основном это проблема сканера TLD от Jettys, проблема не в том, что встроенный кот

не уверен, что последняя версия Jetty исправила эту проблему.

15
задан Peter Mortensen 27 May 2016 в 22:21
поделиться

3 ответа

Включите их в свой профиль PowerShell, чтобы они загружались автоматически при каждом запуске PS.

См. Профили Windows PowerShell для получения дополнительной информации о том, где найти сценарий вашего профиля.

PS по умолчанию использует ваш профиль в папке «Мои документы». Мой находится на сетевом диске, поэтому при входе в систему PowerShell указывает на ту же папку профиля.

15
ответ дан 1 December 2019 в 01:53
поделиться

Вы можете сделать это более простым способом. У меня есть это в моем профиле:

##-------------------------------------------
## Load Script Libraries
##-------------------------------------------
Get-ChildItem ($lib_home + "*.ps1") | ForEach-Object {& (Join-Path $lib_home $_.Name)} | Out-Null

Где $ lib_home - это папка, в которой хранятся скрипты, которые я хочу автоматически включить. В этом случае он выполняет их. Поэтому у меня есть сценарии определения глобальных функций. Вы также можете указать их источник (замените «&» на «.»).

11
ответ дан 1 December 2019 в 01:53
поделиться

Functionality wise I think there are a couple of ways you could improve your script.

The first is that your script is dependent upon the name of the script not changing. While I don't think it's likely you'll change the name of this script, you never know what mood you'll be in a few years from now. Instead why not just calculate the name of the script dynamically.

$scriptName = split-path -leaf $MyInvocation.MyCommand.Definition

The next problem is that I believe you're split function will fail if you ever place the directory in a path which contains a space. It will cause a path like "c:\foo bar\baz.ps1" to appear as "c:\foo", "bar\baz.ps1". Much better to remove the split and just use the enumeration by the get-childitem command.

Also you are taking a dependency on the current path being the path containing the scripts. You should either make that an explicit parameter or use the path containing the allFunctions.ps1 file (i prefer the latter)

Here is the updated version I would use.

$scriptName = split-path -leaf $MyInvocation.MyCommand.Definition
$rootPath = split-path -parent $MyInvocation.MyCommand.Definition
$scripts = gci -re $rootPath -in *.ps1 | ?{ $_.Name -ne $scriptName }
foreach ( $item in $scripts ) {
  . $item.FullName
}

From a security standpoint you have to consider the possibility that a malicious user adds a bad script into the target directory. If they did so it would be executed with your allFunctions.ps1 file and could do damage to the computer. But at the point the malicious user has access to your file system, it's likely they could do the damage without the help of your script so it's probably a minor concern.

6
ответ дан 1 December 2019 в 01:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: