码迷,mamicode.com
首页 > 其他好文 > 详细

远程推送脚本,创建任务计划

时间:2014-10-21 17:10:48      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   使用   for   sp   

  1 #############################脚本功能及说明#######################################
  2 #该脚本用来在各台服务器上运行创建任务计划,可以将位于本地的脚本拷贝到远程服务器
  3 #通过查看Task_Result.txt文件可以查看任务计划的创建结果,如果任务计划创建失败,控制台会有输出
  4 #创建时间:2014-10-21
  5 
  6 #################################################定义函数##############################################################
  7 #定义函数CCopy,比较远程目录下的文件是否与本地一致(根据文件名称和文件修改时间进行比较),如果不一致则进行拷贝(远程服务器已存在文件不会影响)
  8 Function CCopy($CLocalPath,$CRemotePath) 
  9    {
 10     $ERPScripts_Local = Gci -Path $CLocalPath -ErrorAction SilentlyContinue
 11     If ($? -eq $true)
 12        {
 13         If (!(Test-Path -Path $CRemotePath))
 14            {
 15              $Null = New-Item -Path $CRemotePath -ItemType Directory
 16              Copy-Item $CLocalPath\* $CRemotePath -Force
 17             }       
 18         Else
 19            {
 20             Foreach ($ERPScript_Local in $ERPScripts_Local)
 21                {
 22                 $ERPScript_RemotePath = Join-Path $ScriptFolder_Remote_Path $ERPScript_Local.Name
 23                 If (!(Test-Path -Path $ERPScript_RemotePath))
 24                    {Copy-Item $ERPScript_Local.FullName $ScriptFolder_Remote_Path -Force} 
 25                 Else 
 26                    {
 27                     $ERPScript_Remote = Gci -Path $ERPScript_RemotePath -ErrorAction SilentlyContinue
 28                     If ($? -eq $true)
 29                        {
 30                         If ($ERPScript_Local.LastWriteTime -ne $ERPScript_Remote.LastWriteTime)
 31                             {Copy-Item $ERPScript_Local.FullName $ScriptFolder_Remote_Path -Force}
 32                         }
 33                     Else {"Failed. Can not find Remote directory."}         
 34                    }
 35                }
 36            } 
 37        }
 38     Else {"Failed. Can not find local directory."}
 39     
 40       } 
 41 
 42 
 43 #######################################定义变量#########################################################
 44 $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf(‘\‘)+1)
 45 #定义服务器列表
 46 $server_list = "server_list.txt"
 47 #定义任务计划输出结果保存文件
 48 $task_resultfile = "Task_Result.txt"
 49 #定义本地服务器脚本存放文件夹
 50 $Scriptfolder_Local =  "DNSCheck"
 51 #定义脚本名称
 52 $scriptName = "DNSCheck.ps1"
 53 #定义远程服务器脚本存放路径
 54 $ScriptFolder_Remote = "D:\Scripts_Production"
 55 #定义使用到的用户名和密码
 56 $UserName = "administrator"
 57 
 58 #定义任务计划的名称、启动时间、启动日期和启动脚本路径
 59 $TaskWatchName = "DNSCheck"
 60 $TaskWatchtime = "00:00"
 61 $TaskWatchDate = "2014/10/30"
 62 $TaskWatchScriptPath0 = Join-Path $ScriptFolder_Remote $scriptName
 63 $TaskWatchScriptPath = "%windir%\system32\WindowsPowerShell\v1.0\powershell.exe $TaskWatchScriptPath0"
 64 
 65 #######脚本开始###############################################
 66 #删除已有的IPC会话连接
 67 $Null = NET USE * /del /y
 68 $servers = gc $server_list
 69 foreach ($server in $servers)
 70     {
 71      If ( Test-Connection $server  -Count 1 -Quiet )
 72         {
 73          Write-Host $server -ForegroundColor green
 74          #获取远程计算机的密码 
 75          $UserPass = $serverpass 
 76          $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
 77          $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
 78          cmd /c  "NET USE \\$Server $UserPass /user:$UserName >nul 2>nul"
 79          If ($Lastexitcode -eq 0)
 80             {
 81              #将脚本拷贝到远程计算机
 82              $Script_Path_Local = $CurrentPath + $Scriptfolder_Local
 83              $ScriptFolder_Remote_Path = "\\" + $Server  + "\" + $ScriptFolder_Remote.Split(":")[0] + "$" + $ScriptFolder_Remote.Split(":")[1]
 84              CCopy $Script_Path_Local $ScriptFolder_Remote_Path
 85              #创建任务计划
 86              $Tresult =  invoke-command -ComputerName $server -Credential $cred -ScriptBlock {param($TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server)
 87              $Taskq = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName  -Encoding default -quiet
 88              If ($Taskq -eq "true")
 89                   {$Null = schtasks /change /tn $TaskWatchName /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath}
 90              Else {$Null = schtasks /create /tn $TaskWatchName /sc minute /mo 1 /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath}
 91              $result = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName  -Encoding default
 92              If ($result -ne $null)
 93                   {$server + " " + $result.Line}
 94              Else {"$server $TaskWatchName Task Created Failed"}      
 95                                                                   } -ArgumentList $TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server #|Tee -Variable TmpVarLog
 96              $Tresult |Out-File $task_resultfile -append
 97              #03包含逗号,08包含Ready
 98              If (!($Tresult.contains(",") -or $Tresult.contains("Ready")))
 99                 { Write-Host $Tresult -ForegroundColor Red}               
100             
101             }
102          Else 
103             {
104              Write-Host "$server 不符合密码规则"  -ForegroundColor Red
105              $server + "不符合密码规则" |Out-File $task_resultfile -Append 
106             }           
107         
108         }
109      Else 
110         { 
111          Write-Host "无法Ping通"  -ForegroundColor Red
112          $server + "无法Ping通" |Out-File $task_resultfile -Append 
113         }
114     }
115      
116     
117                   
118 $Null = NET USE * /del /y
119 
120 "任务计划创建完成!"
121 #cmd /c pause

 

远程推送脚本,创建任务计划

标签:style   blog   color   io   os   ar   使用   for   sp   

原文地址:http://www.cnblogs.com/dreamer-fish/p/4040703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!