概要
某些模块发布者的法律部门要求客户在从 PowerShell 库安装其模块之前必须显式接受许可证。 如果用户使用 PowerShellGet 安装、更新或保存模块(无论是直接安装、更新或保存模块,还是作为另一个包的依赖项),并且该模块要求用户同意许可证,则用户必须指示他们接受许可证,否则作将失败。
发布模块的要求
想要要求用户接受许可证的模块应满足以下要求:
- 模块清单的 PSData 部分应包含 RequireLicenseAcceptance = $True。
- 模块应在根目录中包含 license.txt 文件。
- 模块清单应包含许可证 URI。
- 模块应使用 PowerShellGet 格式版本 2.0 及更高版本发布。
对安装/保存/Update-Module 的影响
- 安装/保存/更新 cmdlet 支持新参数 AcceptLicense ,其行为就像用户看到许可证一样。
- 如果 RequiredLicenseAcceptance 为 True 且未指定 AcceptLicense ,则会向用户显示
license.txt,并提示用户:Do you accept these license terms (Yes/No/YesToAll/NoToAll)。- 如果许可证被接受
- Save-Module: 将模块复制到用户的系统
- Install-Module: 将模块复制到用户系统的正确文件夹(基于范围)
- Update-Module: 模块已更新。
- 如果许可证被拒绝。
- 作被取消。
- 所有 cmdlet 都会检查元数据 (requireLicenseAcceptance 和 Format Version) ,其中指出需要接受许可证
- 如果客户端的格式版本早于 2.0,则作失败并要求用户更新客户端。
- 如果模块的格式版本早于 2.0,则忽略 requireLicenseAcceptance 标志。
- 如果许可证被接受
模块依赖项
- 在安装/保存/更新作期间,如果依赖模块(其他内容依赖于模块)需要许可证接受,则需要许可证接受行为(上述)。
- 如果模块版本已在本地目录中列为已安装在系统上,我们将绕过许可证检查。
- 在安装/保存/更新作期间,如果依赖模块需要许可证,并且未接受许可证,则该作将失败,并遵循无法安装/保存/更新的包的正常流程。
对 -Force 的影响
指定 –Force 不足以接受许可证。
–AcceptLicense 需要安装权限。 如果指定,则 RequiredLicenseAcceptance 为 True,并且–AcceptLicense未指定,则–Force作失败。
例子
示例 1:更新模块清单以要求接受许可证
Update-ModuleManifest -Path C:\modulemanifest.psd1 -RequireLicenseAcceptance -PrivateData @{
PSData = @{
# Flag to indicate whether the module requires explicit user acceptance
RequireLicenseAcceptance = $true
} # End of PSData hashtable
} # End of PrivateData hashtable
此命令更新清单文件并将 RequireLicenseAcceptance 标志设置为 true。
示例 2:安装需要接受许可证的模块
Install-Module -Name ModuleRequireLicenseAcceptance
License Acceptance
License 2.0
Copyright (c) 2016 PowerShell Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
Do you accept the license terms for module 'ModuleRequireLicenseAcceptance'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
此命令显示文件中的 license.txt 许可证,并提示用户接受许可证。
示例 3:安装需要使用 -AcceptLicense 接受许可证的模块
Install-Module -Name ModuleRequireLicenseAcceptance -AcceptLicense
安装模块时没有任何接受许可证的提示。
示例 4:安装需要使用 -Force 接受许可证的模块
Install-Module -Name ModuleRequireLicenseAcceptance -Force
PackageManagement\Install-Package : License Acceptance is required for module 'ModuleRequireLicenseAcceptance'. Please specify '-AcceptLicense' to perform this operation.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.1.3.3\PSModule.psm1:1837 char:21
+ ... $null = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], E
xception
+ FullyQualifiedErrorId : ForceAcceptLicense,Install-PackageUtility,Microsoft.PowerShell.PackageManagement.Cmdlets
.InstallPackage
示例 5:安装具有需要接受许可证的依赖项的模块
模块 ModuleWithDependency 依赖于模块 ModuleRequireLicenseAcceptance。 系统会提示用户接受许可证。
Install-Module -Name ModuleWithDependency
License Acceptance
MIT License 2.0
Copyright (c) 2016 PowerShell Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
Do you accept the license terms for module 'ModuleRequireLicenseAcceptance'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
示例 6:安装具有需要许可证接受和 -AcceptLicense 的依赖项的模块
模块 ModuleWithDependency 依赖于模块 ModuleRequireLicenseAcceptance。 由于指定了 AcceptLicense ,因此不会提示用户接受许可证。
Install-Module -Name ModuleWithDependency -AcceptLicense
示例 7:在早于 PSGetFormatVersion 2.0 的客户端上安装需要接受许可证的模块
Install-Module -Name ModuleRequireLicenseAcceptance
WARNING: The specified module 'ModuleRequireLicenseAcceptance' with PowerShellGetFormatVersion
'2.0' is not supported by the current version of PowerShellGet. Get the latest version of the
PowerShellGet module to install this module, 'ModuleRequireLicenseAcceptance'.
示例 8:需要接受许可证的保存模块
Save-Module -Name ModuleRequireLicenseAcceptance -Path C:\Saved
License Acceptance
License 2.0
Copyright (c) 2016 PowerShell Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
Do you accept the license terms for module 'ModuleRequireLicenseAcceptance'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
此命令显示文件中的 license.txt 许可证,并提示用户接受许可证。
示例 9:保存模块需要使用 -AcceptLicense 接受许可证
Save-Module -Name ModuleRequireLicenseAcceptance -AcceptLicense -Path C:\Saved
模块被保存,没有任何接受许可证的提示。
示例 10:需要接受许可证的更新模块
Update-Module -Name ModuleRequireLicenseAcceptance
License Acceptance
License 2.0
Copyright (c) 2016 PowerShell Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
Do you accept the license terms for module 'ModuleRequireLicenseAcceptance'.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
此命令显示文件中的 license.txt 许可证,并提示用户接受许可证。
示例 11:更新模块需要使用 -AcceptLicense 接受许可证
Update-Module -Name ModuleRequireLicenseAcceptance -AcceptLicense
模块已更新,但没有任何接受许可证的提示。