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

    --待续--

  • 相关阅读:
    Python_函数
    Python中元组,列表,字典的区别
    Oracle 在字符串中输入单引号或特殊字符
    Visual Studio Code管理MySQL
    Python .pyc的编译和反编译
    Django 模板变量之 forloop
    浅谈Django基础(HttpResponse、render、redirect)
    Django 使用form表单提交数据报错: Forbidden (403)
    Django出错提示TemplateDoesNotExist at /
    使用 vs code 创建 Django 项目
  • 原文地址:https://www.cnblogs.com/daniel-niu/p/13273279.html
Copyright © 2011-2022 走看看