Informacje o wymaganiachAbout Requires
Krótki opisShort description
Zapobiega uruchamianiu skryptu bez wymaganych elementów.Prevents a script from running without the required elements.
Długi opisLong description
#Requires
Instrukcja uniemożliwia uruchomienie skryptu, chyba że spełnione są wymagania wstępne dotyczące wersji programu PowerShell, modułów (i wersji) lub przystawek (oraz wersji) i wersji.The #Requires
statement prevents a script from running unless the PowerShell version, modules (and version), or snap-ins (and version), and edition prerequisites are met. Jeśli wymagania wstępne nie są spełnione, program PowerShell nie uruchamia skryptu.If the prerequisites aren't met, PowerShell doesn't run the script.
SkładniaSyntax
#Requires -Version <N>[.<n>]
#Requires -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
#Requires -Modules { <Module-Name> | <Hashtable> }
#Requires -PSEdition <PSEdition-Name>
#Requires -ShellId <ShellId> -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
#Requires -RunAsAdministrator
Aby uzyskać więcej informacji na temat składni, zobacz ScriptRequirements.For more information about the syntax, see ScriptRequirements.
Reguły do użyciaRules for use
Skrypt może zawierać więcej niż jedną #Requires
instrukcję.A script can include more than one #Requires
statement. #Requires
Instrukcje mogą pojawić się w dowolnym wierszu skryptu.The #Requires
statements can appear on any line in a script.
Umieszczenie #Requires
instrukcji wewnątrz funkcji nie ogranicza jej zakresu.Placing a #Requires
statement inside a function does NOT limit its scope. Wszystkie #Requires
instrukcje są zawsze stosowane globalnie i muszą zostać spełnione, aby można było wykonać skrypt.All #Requires
statements are always applied globally, and must be met, before the script can execute.
Ostrzeżenie
Mimo że #Requires
instrukcja może pojawić się w dowolnym wierszu skryptu, jego pozycja w skrypcie nie ma wpływu na sekwencję swojej aplikacji.Even though a #Requires
statement can appear on any line in a script, its position in a script does not affect the sequence of its application. Stan globalny #Requires
przedstawiający instrukcję musi zostać spełniony przed wykonaniem skryptu.The global state the #Requires
statement presents must be met before script execution.
Przykład:Example:
Get-Module AzureRM.Netcore | Remove-Module
#Requires -Modules AzureRM.Netcore
Może się zdarzyć, że powyższy kod nie powinien zostać uruchomiony, ponieważ wymagany moduł został usunięty przed #Requires
instrukcją.You might think that the above code shouldn't run because the required module was removed before the #Requires
statement. Jednak #Requires
przed wykonaniem skryptu można go spełnić.However, the #Requires
state had to be met before the script could even execute. Następnie pierwszy wiersz skryptu unieważnił wymagany stan.Then the first line of the script invalidated the required state.
ParametryParameters
-Zestaw <Assembly path> |<.NET assembly specification>-Assembly <Assembly path> | <.NET assembly specification>
Ważne
-Assembly
Składnia jest przestarzała.The -Assembly
syntax is deprecated. Nie pełni żadnej funkcji.It serves no function. Składnia została dodana w programie PowerShell 5,1, ale kod pomocniczy nigdy nie został zaimplementowany.The syntax was added in PowerShell 5.1 but the supporting code was never implemented. Składnia nadal jest akceptowana w celu zapewnienia zgodności z poprzednimi wersjami.The syntax is still accepted for backward compatibility.
Określa ścieżkę do pliku DLL zestawu lub nazwy zestawu platformy .NET.Specifies the path to the assembly DLL file or a .NET assembly name. Parametr Assembly został wprowadzony w programie PowerShell 5,0.The Assembly parameter was introduced in PowerShell 5.0. Aby uzyskać więcej informacji na temat zestawów .NET, zobacz nazwy zestawów.For more information about .NET assemblies, see Assembly names.
Na przykład:For example:
#Requires -Assembly path\to\foo.dll
#Requires -Assembly "System.Management.Automation, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
-Version <N> [. <n> ]-Version <N>[.<n>]
Określa minimalną wersję programu PowerShell wymaganą przez skrypt.Specifies the minimum version of PowerShell that the script requires. Wprowadź numer wersji głównej i opcjonalny numer wersji pomocniczej.Enter a major version number and optional minor version number.
Na przykład:For example:
#Requires -Version 6.0
-PSSnapin <PSSnapin-Name> [-Version <N> [. <n> ]]-PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
Określa przystawkę programu PowerShell wymaganą przez skrypt.Specifies a PowerShell snap-in that the script requires. Wprowadź nazwę przystawki i opcjonalny numer wersji.Enter the snap-in name and an optional version number.
Na przykład:For example:
#Requires -PSSnapin DiskSnapin -Version 1.2
-Moduły <Module-Name> |<Hashtable>-Modules <Module-Name> | <Hashtable>
Określa moduły programu PowerShell wymagane przez skrypt.Specifies PowerShell modules that the script requires. Wprowadź nazwę modułu i opcjonalny numer wersji.Enter the module name and an optional version number.
Jeśli wymagane moduły nie znajdują się w bieżącej sesji, program PowerShell importuje je.If the required modules aren't in the current session, PowerShell imports them. Jeśli nie można zaimportować modułów, program PowerShell zgłasza błąd powodujący przerwanie.If the modules can't be imported, PowerShell throws a terminating error.
Dla każdego modułu wpisz nazwę modułu ( <String> ) lub tablicę skrótów.For each module, type the module name (<String>) or a hash table. Wartość może być kombinacją ciągów i tablic skrótów.The value can be a combination of strings and hash tables. Tabela skrótów ma następujące klucze.The hash table has the following keys.
ModuleName
- Wymagane Określa nazwę modułu.ModuleName
- Required Specifies the module name.GUID
- Opcjonalne Określa identyfikator GUID modułu.GUID
- Optional Specifies the GUID of the module.- Jest również wymagane , aby określić jeden z trzech poniższych kluczy.It's also Required to specify one of the three below keys. Tych kluczy nie można używać razem.These keys can't be used together.
ModuleVersion
-Określa minimalną akceptowalną wersję modułu.ModuleVersion
- Specifies a minimum acceptable version of the module.RequiredVersion
-Określa dokładną, wymaganą wersję modułu.RequiredVersion
- Specifies an exact, required version of the module.MaximumVersion
-Określa maksymalną akceptowalną wersję modułu.MaximumVersion
- Specifies the maximum acceptable version of the module.
Uwaga
RequiredVersion
dodano w programie Windows PowerShell 5,0.RequiredVersion
was added in Windows PowerShell 5.0.
MaximumVersion
dodano w programie Windows PowerShell 5,1.MaximumVersion
was added in Windows PowerShell 5.1.
Na przykład:For example:
Wymagaj, aby AzureRM.Netcore
0.12.0
została zainstalowana (wersja lub nowsza).Require that AzureRM.Netcore
(version 0.12.0
or greater) is installed.
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; ModuleVersion="0.12.0" }
Wymagaj, aby AzureRM.Netcore
została zainstalowana (tylko wersja 0.12.0
).Require that AzureRM.Netcore
(only version 0.12.0
) is installed.
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; RequiredVersion="0.12.0" }
Wymaga AzureRM.Netcore
zainstalowania programu (wersji 0.12.0
lub mniejszej).Requires that AzureRM.Netcore
(version 0.12.0
or lesser) is installed.
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; MaximumVersion="0.12.0" }
Wymagaj, aby była zainstalowana jakakolwiek wersja programu AzureRM.Netcore
i PowerShellGet
.Require that any version of AzureRM.Netcore
and PowerShellGet
is installed.
#Requires -Modules AzureRM.Netcore, PowerShellGet
W przypadku korzystania z RequiredVersion
klucza upewnij się, że ciąg wersji dokładnie pasuje do wymaganego ciągu wersji.When using the RequiredVersion
key, ensure your version string exactly matches the version string you require.
Get-Module AzureRM.Netcore -ListAvailable
Directory: /home/azureuser/.local/share/powershell/Modules
ModuleType Version Name PSEdition ExportedCommands
---------- ------- ---- --------- ----------------
Script 0.12.0 AzureRM.Netcore Core
Poniższy przykład kończy się niepowodzeniem, ponieważ 0,12 nie pasuje dokładnie do 0.12.0.The following example fails because 0.12 doesn't exactly match 0.12.0.
#Requires -Modules @{ ModuleName="AzureRM.Netcore"; RequiredVersion="0.12" }
-PSEdition <PSEdition-Name>-PSEdition <PSEdition-Name>
Określa wersję programu PowerShell wymaganą przez skrypt.Specifies a PowerShell edition that the script requires. Prawidłowe wartości to rdzeń dla programu PowerShell Core i pulpitu dla środowiska Windows PowerShell.Valid values are Core for PowerShell Core and Desktop for Windows PowerShell.
Na przykład:For example:
#Requires -PSEdition Core
-ShellId-ShellId
Określa powłokę wymaganą przez skrypt.Specifies the shell that the script requires. Wprowadź identyfikator powłoki.Enter the shell ID. W przypadku użycia parametru ShellId należy również podać parametr PSSnapin .If you use the ShellId parameter, you must also include the PSSnapin parameter.
Bieżącą ShellId można znaleźć, wykonując zapytania dotyczące $ShellId
zmiennej automatycznej.You can find the current ShellId by querying the $ShellId
automatic variable.
Na przykład:For example:
#Requires -ShellId MyLocalShell -PSSnapin Microsoft.PowerShell.Core
Uwaga
Ten parametr jest przeznaczony do użycia w przypadku powłoce, które zostały wycofane.This parameter is intended for use in mini-shells, which have been deprecated.
-RunAsAdministrator-RunAsAdministrator
Gdy ten parametr przełącznika zostanie dodany do #Requires
instrukcji, określa, że sesja programu PowerShell, w której jest uruchamiany skrypt, musi być uruchomiona z podwyższonym poziomem uprawnień użytkownika.When this switch parameter is added to your #Requires
statement, it specifies that the PowerShell session in which you're running the script must be started with elevated user rights. Parametr RunAsAdministrator jest ignorowany w systemie operacyjnym innym niż Windows.The RunAsAdministrator parameter is ignored on a non-Windows operating system. Parametr RunAsAdministrator został wprowadzony w programie PowerShell 4,0.The RunAsAdministrator parameter was introduced in PowerShell 4.0.
Na przykład:For example:
#Requires -RunAsAdministrator
PrzykładyExamples
Poniższy skrypt ma dwie #Requires
instrukcje.The following script has two #Requires
statements. Jeśli wymagania określone w obu instrukcjach nie są spełnione, skrypt nie zostanie uruchomiony.If the requirements specified in both statements aren't met, the script doesn't run. Każda #Requires
instrukcja musi być pierwszym elementem w wierszu:Each #Requires
statement must be the first item on a line:
#Requires -Modules AzureRM.Netcore
#Requires -Version 6.0
Param
(
[parameter(Mandatory=$true)]
[String[]]
$Path
)
...