代码之家  ›  专栏  ›  技术社区  ›  GeraltDieSocke

如何尽可能简单地为所有同事设置默认的powershell配置文件?

  •  2
  • GeraltDieSocke  · 技术社区  · 6 年前

    我想实现什么?

    我有一个ps1文件,里面有我所有的功能。在第一步中,我想把它转换成一个ps模块。那么我想要以下的:

    • 同事得到一个脚本或蝙蝠,他必须运行一次这将设置他的模块环境路径 $Env:PSModulePath 到每个人都可以访问的网络驱动器上的路径
    • 将自定义profile.ps1复制并粘贴到用户中 %userprofile%\Documents\WindowsPowershell 导入模块的
    • 现在每个用户都应该拥有我在其shell中提供的powershell脚本

    我是怎么解决的

    我和一位同事过去的做法是:

    (我们有一个 profile.ps1 执行以下操作的文件:

    #set path for profile_loader.ps1
    $path = "\\server\share\folderwithscripts";
    #call profile_loader.ps1
    . "$path"
    

    然后这个 profile_loader.ps1 baiscaly只需加载大量脚本(ps1文件),如下所示:

    . "\\server\share\pathtoanotherscript.ps1

    一行接一行。

    我不喜欢,这对我未来要建立的另外25个同事来说太复杂了。

    问题

    实现这一目标的最佳方法是什么一个很好的旧.bat文件,它将ps1文件复制并传递到用户配置文件中还是有更好的办法?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Drew    6 年前

    作为一个拥有 $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)"
    }
    

    把这个放在你的机器上让你的灵魂崩溃 $配置文件 你的同事可能有自己的设置。

    # 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
    }