zoukankan      html  css  js  c++  java
  • powershell 日常操作服务器脚本(1)

    由于日常接触服务器和APP运维工作较多,估整理一些日常使用powershell 脚本来操作服务器的一些方法:

    脚本如下:

    # 重启IIS
    function Action-IIS-Restart
    {
    param(
    #输入参数
    [Paramter(Mandatory=$true)]
    [string]
    $password,
    [evntype]
    $evn 
    )
    #password 转换
    $password_conv =ConvertTo-SecureString -String
    #需要重启IIS的服务器列表
    $servers= 'server1','server2'
    #登陆凭证对应用户信息
    $username='username'
    #创建登陆凭证
    $credential =New-Object System.Management.Automation.PSCredential -argumentlist $username ,$password_conv
    write-output '正在重启 IIS 服务 ................ '
    #循环遍历需要重启IIS服务器
    foreach($server in $servers){
    $message= '正在重启 IIS 服务,服务器 : '+$server
    write-output $message
    $sessions=New-PSSession -ComputerName $server -credential $credential
    Invoke-Command –Session $sessions -scriptBlock {restart-Service w3svc}
    $message= 'IIS 服务已重启,服务器 : '+$server
    write-output $message
    
    }
    write-output 'IIS 服务已全部重启 ...................'
    }
    
    
    
    #重启服务器
    function Action-Restart-Server
    {
    param(
    [Paramter(Mandatory=$true)]
    [string]
    $password
    )
    $username='username' #推荐使用G-account ,且该账号需要有admin权限
    $servers='server1','server2'#需要重启的服务器列表
    $password_conv =ConvertTo-SecureString -String $password -AsPlainText -Force 
    #创建凭证
    $credential =New-Object System.Management.Automation.PSCredential -argumentlist $username ,$password_conv
    #重启命令
    write-output '开始重启服务器......' 
    Restart-Computer -ComputerName $servers -Credential $credential -Wait -For PowerShell -InformationAction 
    write-output '服务器重启完成......' 
    }
    #获取服务上IIS pool的状态
    function IIS_Status_Check{
    #获取应用程序池状态
    Import-Module WebAdministration; Get-WebAppPoolState -Name 'DefaultAppPool' 
    #开启应用程序池
    Import-Module WebAdministration; Start-WebAppPool -Name 'DefaultAppPool' 
    #关闭应用程序池
    Import-Module WebAdministration; Stop-WebAppPool -Name 'DefaultAppPool'
    
    #获取站点状态
    Import-Module WebAdministration; Get-WebsiteState -Name "Default Web Site"
    }
    View Code

    --待续--

  • 相关阅读:
    第 15 章 标签页和工具提示插件
    第 14 章 下拉菜单和滚动监听插件
    第 13 章 模态框插件
    第 12 章 列表组面板和嵌入组件
    第 11 章 进度条媒体对象和 Well 组件
    第 10 章 巨幕页头缩略图和警告框组件
    第 9 章 路径分页标签和徽章组件
    lock()与lockInterruptibly()的区别
    MySQL中Innodb的聚簇索引和非聚簇索引
    MySQL使用可重复读作为默认隔离级别的原因
  • 原文地址:https://www.cnblogs.com/daniel-niu/p/13273279.html
Copyright © 2011-2022 走看看