【问题标题】:Where to put PowerShell profile scripts if standard locations are forbidden?如果标准位置被禁止,那么在哪里放置 PowerShell 配置文件脚本?
【发布时间】:2016-10-06 15:58:31
【问题描述】:

在我们的公司环境中,我在创建 PowerShell 配置文件脚本时遇到了困难。

为了防止用户在本地磁盘上写入文档,“文档”目录被强制位于网络驱动器上。通常是“H:”(家庭)驱动器。

同样禁止用户在C:\Windows\System32下写入。

如果这两个不可用,我可以将 ISE 配置文件脚本放在哪里?

PSVersion 5.0.10586.117

PS C:\Windows\System32\WindowsPowerShell\v1.0> $HOME, $PSHOME
C:\Users\pwatson
C:\Windows\System32\WindowsPowerShell\v1.0

另请参阅:Help-About about_Profiles

当我没有连接到网络时,这些是 $profile 设置。我仍然无法在 C:\Windows\System32 下写入,CurrentUser 值无效。

PS C:\Windows\System32\WindowsPowerShell\v1.0> $profile | Get-Member -Type NoteProperty | ForEach-Object {$_.ToString
()}
string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
string CurrentUserAllHosts=WindowsPowerShell\profile.ps1
string CurrentUserCurrentHost=WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS C:\Windows\System32\WindowsPowerShell\v1.0>

【问题讨论】:

  • 据我所知,您是此环境中的用户还是管理员?为什么用户需要对脚本的写入权限?
  • @wOxxOm - 这不是微软文档所说的可以放置脚本的地方。你知道这行得通吗?
  • @Kage - 我是用户。这是我的开发机器。我有“管理员”权限,但不能在 System32 下写。
  • @Liturgist,不,它没有用,我已经删除了评论。
  • 我发现我可以在$profile.CurrentUserAllHosts 放置一个profile.ps1 文件,shell 会使用它。该值采用 UNC 格式,\\SERVER\SHARE\user\My Documents...。当我没有连接到网络时,我将没有可用的配置文件。

标签: powershell


【解决方案1】:

一种选择是创建一个具有如下目标的快捷方式:

%systemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -file C:\somewhere\myprofile.ps1

然后始终使用此快捷方式启动 PowerShell。

这实际上并没有使用内置的 PowerShell 配置文件概念,而是点源一个 ps1 文件,其行为与配置文件非常相似。

如果您想从 cmd.exe 启动 PowerShell,请创建一个与上述内容相同的批处理文件并将其放在路径中的某个位置(如果您有权限这样做)

【讨论】:

  • 我将选择这个作为答案,因为它可能会帮助某人。我可以试试这个,但我认为不使用设计的配置文件概念可能会丢失一些东西。还有$PSCommandPath$PSScriptRooot$env:PSModulePath要处理。
猜你喜欢
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 2011-04-08
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
相关资源
最近更新 更多