Windows PowerShell怎么配置公共方法、变量、常量

在使用windows时经常会因为一些重复的指令而耗费开发时间,比如我们在管理多台服务器时,再或者访问不同的路径或者其它场景下。那么我们就可以把它们全部写入windowsPowerShell的用户配置文件(profile.ps1)里面。

文件路径如下:

PS> $profile.AllUsersAllHosts
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1

 

 如果没有profile.ps1文件,那就自己创建一个。(请勿复制,缩进的原因会导致运行失败。请亲手码上测试

# 设置变量
$a = 1;

# 设置一个方法
function hello(){
    "hello WindowsPowerShell"
}

# 设置方法运行某些指令
function openHosts(){
    # 以notepad打开hosts文件
    notepad C:\Windows\System32\drivers\etc\hosts    
}

# 连接ssh
function ssh221(){
  ssh root@154.221.**.*
}

 

看一下运行结果:

 

posted @ 2022-10-19 19:55  黄树瑜  阅读(210)  评论(0)    收藏  举报