Powershell 开机自动加载Snapin 和module

简介:

以前只知道在Powershell的默认路径下面添加第三方的module就可以开机自动加载了

1
2
3
4
5
PS C:\WINDOWS\system32>  $env:PSModulePath  -split  ";"
C:\Users\yli\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files (x86)\NetApp\NetApp PowerShell Toolkit\Modules\.


但是其他路径的Module怎么处理?Snapin怎么处理,今天终于知道可以配置profile script来配置了。


方法很简单。


PowerShell的用户档案的WindowsPowerShell文件夹里面添加一个新的 profile.ps1 文件,然后在这个文件里面执行import-module和add-pssnapin等命令就可以了。开机的时候会自动执行该文件。


wKiom1bBaY2zCwejAAAwtf2Xj5k072.png

例如 开机自动加载对应的一些模块和插件等等


1
2
3
4
5
6
7
8
9
10
import-module  dataontap
Add-PSSnapin  VMware.VimAutomation.Core
$cred  Get-Credential 
Import-Module  MSOnline
Set-ExecutionPolicy  remotesigned
Connect-MsolService  -Credential  $cred
  
#连接到Office365
$Session  New-PSSession  -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell/ -Credential  $Cred  -Authentication Basic -AllowRedirection
Import-PSSession  $session


下面是引自《Learn Powershell 3 in a Month of Lunches》不同的路径的profle有不同效果。


$pshome/profile.ps1—This will execute for all users of the computer, no matter

which host they’re using (remember that $pshome is predefined within

PowerShell and contains the path of the PowerShell installation folder).

2 $pshome/Microsoft.PowerShell_profile.ps1—This will execute for all users of

the computer if they’re using the console host. If they’re using the PowerShell

ISE, the $pshome/Microsoft.PowerShellISE_profile.ps1 script will be executed

instead.

3 $home/Documents/WindowsPowerShell/profile.ps1—This will execute only

for the current user (because it lives under the user’s home directory), no matter

which host they’re using.

4 $home/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1—

This will execute for the current user if they’re using the console host. If they’re

using the PowerShell ISE, the $home/Documents/WindowsPowerShell/

Microsoft.PowerShellISE_profile.ps1 script will be executed instead.










本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1742094,如需转载请自行联系原作者

目录
相关文章
|
Java Linux 开发者
Powershell教程(2)——自定义模块Module语法
Powershell教程(2)——自定义模块Module语法
1099 0
Powershell教程(2)——自定义模块Module语法
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
726 10
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
919 0
|
XML 监控 数据格式
利用powershell进行windows日志分析
0x00 前言   Windows 中提供了 2 个分析事件日志的 PowerShell cmdlet:一个是Get-WinEvent,超级强大,但使用起来比较麻烦;另一个是Get-EventLog,使得起来相当简单,可以实时筛选,接下来,我们利用PowerShell 来自动筛选 Windows 事件日志。
3062 0
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
986 0
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的