简体   繁体   English

强制安装模块始终安装到用户文件夹

[英]Force Install-Module to always install to user folders

I want to force PowerShell to always install Modules into user space.我想强制 PowerShell 始终将模块安装到用户空间。 Weirdly, Microsoft have a facility to force installation to System folders requiring Admin with Install-Module <Name> -Scope AllUser but do not have a way to force installation to user folders.奇怪的是,Microsoft 有一种工具可以强制安装到需要 Admin 和Install-Module <Name> -Scope AllUser系统文件夹,但没有办法强制安装到用户文件夹。

$ModulePath = "$(Split-Path $Profile)\Modules"   # Want all modules here.
$ModulePathTest = foreach ($i in ($env:PSModulePath).split(";")) { if ($i -like $ModulePath) { $True } }   # $ModulePathTest will be $true if $ModulePath is on the $env:PSModulePath
if ($ModulePathTest -eq $null) { <do stuff> }

So I got this far and now stuck.所以我走了这么远,现在卡住了。 If I am logged on Admin, I think it installs by default to C:\\Program Files and if as a normal user, it will go to $ModulePath .如果我登录 Admin,我认为它默认安装到C:\\Program Files ,如果作为普通用户,它将转到$ModulePath

• Is there a quick way to add $ModulePath to $env:PSModulePath (and permanently, so I think this means it must be pushed into the registry right? I do not see a PowerShell command that can permanently update an environment variable - if I am wrong, would be good to know)? • 是否有一种快速的方法可以将 $ModulePath 添加到 $env:PSModulePath(并且是永久的,所以我认为这意味着它必须被推送到注册表中,对吗?我没有看到可以永久更新环境变量的 PowerShell 命令 - 如果我错了,很高兴知道)?

• Is there a way that someone can think of to make Install-Module always install into $ModulePath in the above (ie C:\\Users\\<Username>\\Documents\\WindowsPowerShell\\Modules )? • 有没有办法让Install-Module总是安装到上面的$ModulePath中(即C:\\Users\\<Username>\\Documents\\WindowsPowerShell\\Modules )?

Why not simply set the preference globally.为什么不简单地全局设置首选项。

About Parameters Default Values 关于参数默认值

Short description Describes how to set custom default values for cmdlet parameters and advanced functions.简短说明 描述如何为 cmdlet 参数和高级功能设置自定义默认值。

Long description The $PSDefaultParameterValues preference variable lets you specify custom default values for any cmdlet or advanced function.详细说明 $PSDefaultParameterValues 首选项变量允许您为任何 cmdlet 或高级函数指定自定义默认值。 Cmdlets and advanced functions use the custom default value unless you specify another value in the command.除非您在命令中指定另一个值,否则 Cmdlet 和高级函数使用自定义默认值。

Syntax The $PSDefaultParameterValues variable is a hash table that validates the format of keys as an object type of System.Management.Automation.DefaultParameterDictionary.语法 $PSDefaultParameterValues 变量是一个哈希表,用于将键的格式验证为 System.Management.Automation.DefaultParameterDictionary 的对象类型。 The hash table contains Key/Value pairs.哈希表包含键/值对。 A Key is in the format CmdletName:ParameterName.密钥的格式为 CmdletName:ParameterName。 A Value is the DefaultValue or ScriptBlock assigned to the key. Value 是分配给键的 DefaultValue 或 ScriptBlock。

The syntax of the $PSDefaultParameterValues preference variable is as follows: $PSDefaultParameterValues 首选项变量的语法如下:

$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}
$PSDefaultParameterValues=@{ "CmdletName:ParameterName"={{ScriptBlock}} }
$PSDefaultParameterValues["Disabled"]=$True | $False

Using $PSDefaultParameterValues in PowerShell 在 PowerShell 中使用 $PSDefaultParameterValues

Let's say we want to make sure that we do not accidently stop any processes using Stop-Process.假设我们想确保我们不会使用 Stop-Process 意外停止任何进程。 We can force WhatIf to always be used with Stop-Process to better protect ourselves:我们可以强制 WhatIf 始终与 Stop-Process 一起使用以更好地保护自己:

$PSDefaultParameterValues.Add("Stop-Process:WhatIf",$True)

So, for what you are after, ...所以,对于你所追求的,...

What's in your PowerShell $PSDefaultParameterValues Preference Variable? PowerShell $PSDefaultParameterValues 首选项变量中有什么?

$PSDefaultParameterValues = @{
    'Out-Default:OutVariable' = 'LastResult'
    'Out-File:Encoding' = 'utf8'
    'Export-Csv:NoTypeInformation' = $true
    'ConvertTo-Csv:NoTypeInformation' = $true
    'Receive-Job:Keep' = $true
    'Install-Module:Scope' = 'CurrentUser'
    'Install-Module:AllowClobber' = $true
    'Install-Module:Force' = $true
    'Install-Module:SkipPublisherCheck' = $true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM