gpt4 book ai didi

powershell - 模块内的 New-PSDrive 不起作用

转载 作者:行者123 更新时间:2023-12-04 00:44:00 26 4
gpt4 key购买 nike

我有一个自动加载的模块,用于放置我的日常函数和变量。

希望当我启动 PowerShell session 时 PSDrive 总是在这里,在 MyPSModule 的一开始我调用:

$script_directory = 'D:\scripts\'
New-PSDrive -Name Scripts -root $script_directory -PSProvider FileSystem

但是,在我打开一个新的 PowerShell 环境并加载我的模块后: Get-PSDrive没有列出我的驱动器。 (我确定我的模块已加载,我调用了它的一些函数,我什至用 -Force 重新导入, Import-Module -Verbose 没有显示任何错误)

我必须手动调用: New-PSDrive -Name Scripts -root 'D:\scripts\' -PSProvider FileSystem .
只有在这种情况下, Get-PSDrive列出我的驱动器。

怎么了 ? PSDrive 是在加载我的模块时创建的,我应该怎么做?

最佳答案

使用参数 Scope有值 global

-Scope

Specifies a scope for the drive. Valid values are "Global", "Local", or "Script", or a number relative to the current scope (0 through the number of scopes, where 0 is the current scope and 1 is its parent). "Local" is the default. For more information, see about_Scopes (http://go.microsoft.com/fwlink/?LinkID=113260).


$script_directory = 'D:\scripts\'
New-PSDrive -Name Scripts -root $script_directory -PSProvider FileSystem -Scope global

Scop参数说明:

Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles.

Local: The current scope. The local scope can be the global scope or any other scope.

Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.

Private: Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.

关于powershell - 模块内的 New-PSDrive 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26994265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com