我想达到什么目标?
我有一个 ps1 文件,其中包含我的所有功能。在第一步中,我想将其转换为 ps 模块。然后我想要以下内容:
$Env:PSModulePath
到每个人都可以访问的网络驱动器上的路径%userprofile%\Documents\WindowsPowershell
中导入模块 我如何尝试解决它
我和一位同事过去设置它的方式是这样的:
(我们有一个
profile.ps1
文件,它执行以下操作):#set path for profile_loader.ps1
$path = "\\server\share\folderwithscripts";
#call profile_loader.ps1
. "$path"
那么这个
profile_loader.ps1
baiscally 只是加载大量的脚本(ps1 文件),如下所示:. "\\server\share\pathtoanotherscript.ps1
一行接一行。
我不喜欢它,而且对于我将来要设置的其他 25 位同事来说太复杂了。
问题
实现这一目标的最佳方法是什么?一个很好的旧 .bat 文件,将 ps1 文件复制并传递到他们的用户配置文件中?或者,还有更好的方法?
最佳答案
作为拥有$profile
的人删除并设置为“公司默认值”,看在上帝的份上,不要。
如果您必须这样做,那么我建议您只创建一个您希望每个人都拥有的配置文件,并将您的所有模块放在一个共享位置,例如您安全锁定的 Sysadmin 文件夹。
做psedit $proile.AllUsersAllHosts
在您的机器上,修改它,然后使用您自己的强制配置文件创建一个包含您想要销毁的所有主机名的文本文件。把它扔进去,让它默认导入你的模块。
# Checks your server share for any PSM1 files, could change to include PS1 as well I suppose. Long name because its in a $Profile so ¯\_(ツ)_/¯
$ModulePathWithLongNameBecauseSomeoneMayUseThisInAnActualScript = Get-ChildItem -file -Recurse "\\server\share\" -Include "*.psm1"
# Sets module path for other adhoc module calls if they dont want to restart their Powershell
$env:PSModulePath = $env:PSModulePath + ";\\server\share\"
# Imports all PSM1 files from the ModulePath*
Foreach($psm in $ModulePathWithLongNameBecauseSomeoneMayUseThisInAnActualScript){
Import-Module "$($ModulePath.FullName)"
}
在你的机器上运行这个程序,让你的灵魂 splinter
$profile
给可能有自己设置的同事。# Get a list of machines that your staff will use and throw them into a txt or csv etc.
$PCsForForcedProfile = Get-Content "\\server\share\PleaseNo.txt"
Foreach($Colleague in $PCsForForcedProfile){
Copy-Item "C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1" "\\$Colleague\C$\Windows\System32\WindowsPowerShell\v1.0\" -force
}
关于powershell - 如何尽可能简单地为工作中的所有同事设置默认的 powershell 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51472767/