远程推送脚本,创建任务计划
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 #cd D:\folderall\刘岩\Scripts\DNS 45 $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1) 46 #定义服务器列表 47 $server_listfile = "server_list.txt" 48 $server_list =Joint-Path $CurrentPath $server_listfile 49 #定义任务计划输出结果保存文件 50 $task_result = "Task_Result.txt" 51 $task_resultfile =Joint-Path $CurrentPath $task_result 52 #定义本地服务器脚本存放文件夹 53 $Scriptfolder_Local = "DNSCheck" 54 #定义脚本名称 55 $scriptName = "DNSCheck.ps1" 56 #定义远程服务器脚本存放路径 57 $ScriptFolder_Remote = "D:\Scripts_Production" 58 #定义使用到的用户名和密码 59 $UserName = "administrator" 60 61 #定义任务计划的名称、启动时间、启动日期和启动脚本路径 62 $TaskWatchName = "DNSCheck" 63 $TaskWatchtime = "00:00" 64 $TaskWatchDate = "2014/10/30" 65 $TaskWatchScriptPath0 = Join-Path $ScriptFolder_Remote $scriptName 66 $TaskWatchScriptPath = "%windir%\system32\WindowsPowerShell\v1.0\powershell.exe $TaskWatchScriptPath0" 67 68 #######脚本开始############################################### 69 #删除已有的IPC会话连接 70 $Null = NET USE * /del /y 71 $servers = gc $server_list 72 foreach ($server in $servers) 73 { 74 If ( Test-Connection $server -Count 1 -Quiet ) 75 { 76 Write-Host $server -ForegroundColor green 77 #获取远程计算机的密码 78 79 $UserPass = $serverpass 80 $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force 81 $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 82 cmd /c "NET USE \\$Server $UserPass /user:$UserName >nul 2>nul" 83 If ($Lastexitcode -eq 0) 84 { 85 #将脚本拷贝到远程计算机 86 $Script_Path_Local = $CurrentPath + $Scriptfolder_Local 87 $ScriptFolder_Remote_Path = "\\" + $Server + "\" + $ScriptFolder_Remote.Split(":")[0] + "$" + $ScriptFolder_Remote.Split(":")[1] 88 CCopy $Script_Path_Local $ScriptFolder_Remote_Path 89 #创建任务计划 90 $Tresult = invoke-command -ComputerName $server -Credential $cred -ScriptBlock {param($TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server) 91 $Taskq = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName -Encoding default -quiet 92 If ($Taskq -eq "true") 93 {$Null = schtasks /change /tn $TaskWatchName /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath} 94 Else {$Null = schtasks /create /tn $TaskWatchName /sc minute /mo 1 /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath} 95 $result = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName -Encoding default 96 If ($result -ne $null) 97 {$server + " " + $result.Line} 98 Else {"$server $TaskWatchName Task Created Failed"} 99 } -ArgumentList $TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server #|Tee -Variable TmpVarLog 100 $Tresult |Out-File $task_resultfile -append 101 #03包含逗号,08包含Ready 102 If (!($Tresult.contains(",") -or $Tresult.contains("Ready"))) 103 { Write-Host $Tresult -ForegroundColor Red} 104 105 } 106 Else 107 { 108 Write-Host "$server 连接失败" -ForegroundColor Red 109 $server + "连接失败" |Out-File $task_resultfile -Append 110 } 111 112 } 113 Else 114 { 115 Write-Host "无法Ping通" -ForegroundColor Red 116 $server + "无法Ping通" |Out-File $task_resultfile -Append 117 } 118 } 119 120 121 122 $Null = NET USE * /del /y 123 124 "任务计划创建完成!" 125 #cmd /c pause