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

标签 powershell powershell-module

我想达到什么目标?

我有一个 ps1 文件,其中包含我的所有功能。在第一步中,我想将其转换为 ps 模块。然后我想要以下内容:

  • 同事得到一个脚本或 bat ,他必须运行一次。这将设置他的模块环境路径$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 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/

    相关文章:

    powershell - 无法在Powershell中使用try..catch捕获错误

    powershell - 如何保存已删除文件的日志?

    vb.net - 在导入模块期间添加类型

    powershell - 将 -Verbose 状态传递给模块 cmdlet

    powershell - 如何将模块安装到自定义目录中?

    powershell - 找出文件是否是 PowerShell 中的符号链接(symbolic link)

    powershell - Sky for Business的Foreach Powershell命令

    javascript - token '&&' 在此版本中不是有效的语句分隔符

    powershell - 来自自定义模块的命令行开关未被识别