您的位置:首页 > 其它

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

2014-10-21 16:33 281 查看
#############################脚本功能及说明#######################################
#该脚本用来在各台服务器上运行创建任务计划,可以将位于本地的脚本拷贝到远程服务器
#通过查看Task_Result.txt文件可以查看任务计划的创建结果,如果任务计划创建失败,控制台会有输出
#创建时间:2014-10-21

#################################################定义函数##############################################################
#定义函数CCopy,比较远程目录下的文件是否与本地一致(根据文件名称和文件修改时间进行比较),如果不一致则进行拷贝(远程服务器已存在文件不会影响)
Function CCopy($CLocalPath,$CRemotePath)
{
$ERPScripts_Local = Gci -Path $CLocalPath -ErrorAction SilentlyContinue
If ($? -eq $true)
{
If (!(Test-Path -Path $CRemotePath))
{
$Null = New-Item -Path $CRemotePath -ItemType Directory
Copy-Item $CLocalPath\* $CRemotePath -Force
}
Else
{
Foreach ($ERPScript_Local in $ERPScripts_Local)
{
$ERPScript_RemotePath = Join-Path $ScriptFolder_Remote_Path $ERPScript_Local.Name
If (!(Test-Path -Path $ERPScript_RemotePath))
{Copy-Item $ERPScript_Local.FullName $ScriptFolder_Remote_Path -Force}
Else
{
$ERPScript_Remote = Gci -Path $ERPScript_RemotePath -ErrorAction SilentlyContinue
If ($? -eq $true)
{
If ($ERPScript_Local.LastWriteTime -ne $ERPScript_Remote.LastWriteTime)
{Copy-Item $ERPScript_Local.FullName $ScriptFolder_Remote_Path -Force}
}
Else {"Failed. Can not find Remote directory."}
}
}
}
}
Else {"Failed. Can not find local directory."}

}

#######################################定义变量#########################################################
#cd D:\folderall\刘岩\Scripts\DNS
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
#定义服务器列表
$server_listfile = "server_list.txt"
$server_list =Joint-Path $CurrentPath $server_listfile
#定义任务计划输出结果保存文件
$task_result = "Task_Result.txt"
$task_resultfile  =Joint-Path $CurrentPath $task_result
#定义本地服务器脚本存放文件夹
$Scriptfolder_Local =  "DNSCheck"
#定义脚本名称
$scriptName = "DNSCheck.ps1"
#定义远程服务器脚本存放路径
$ScriptFolder_Remote = "D:\Scripts_Production"
#定义使用到的用户名和密码
$UserName = "administrator"

#定义任务计划的名称、启动时间、启动日期和启动脚本路径
$TaskWatchName = "DNSCheck"
$TaskWatchtime = "00:00"
$TaskWatchDate = "2014/10/30"
$TaskWatchScriptPath0 = Join-Path $ScriptFolder_Remote $scriptName
$TaskWatchScriptPath = "%windir%\system32\WindowsPowerShell\v1.0\powershell.exe $TaskWatchScriptPath0"

#######脚本开始###############################################
#删除已有的IPC会话连接
$Null = NET USE * /del /y
$servers = gc $server_list
foreach ($server in $servers)
{
If ( Test-Connection $server  -Count 1 -Quiet )
{
Write-Host $server -ForegroundColor green
#获取远程计算机的密码

$UserPass = $serverpass
$Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
cmd /c  "NET USE \\$Server $UserPass /user:$UserName >nul 2>nul"
If ($Lastexitcode -eq 0)
{
#将脚本拷贝到远程计算机
$Script_Path_Local = $CurrentPath + $Scriptfolder_Local
$ScriptFolder_Remote_Path = "\\" + $Server  + "\" + $ScriptFolder_Remote.Split(":")[0] + "$" + $ScriptFolder_Remote.Split(":")[1]
CCopy $Script_Path_Local $ScriptFolder_Remote_Path
#创建任务计划
$Tresult =  invoke-command -ComputerName $server -Credential $cred -ScriptBlock {param($TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server)
$Taskq = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName  -Encoding default -quiet
If ($Taskq -eq "true")
{$Null = schtasks /change /tn $TaskWatchName /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath}
Else {$Null = schtasks /create /tn $TaskWatchName /sc minute /mo 1 /ru $UserName /rp $UserPass /st $TaskWatchtime /sd $TaskWatchDate /tr $TaskWatchScriptPath}
$result = cmd /c "chcp 437 >null && schtasks /query" |select-string $TaskWatchName  -Encoding default
If ($result -ne $null)
{$server + " " + $result.Line}
Else {"$server $TaskWatchName Task Created Failed"}
} -ArgumentList $TaskWatchName,$UserName,$UserPass,$TaskWatchtime,$TaskWatchDate,$TaskWatchScriptPath,$server #|Tee -Variable TmpVarLog
$Tresult |Out-File $task_resultfile -append
#03包含逗号,08包含Ready
If (!($Tresult.contains(",") -or $Tresult.contains("Ready")))
{ Write-Host $Tresult -ForegroundColor Red}

}
Else
{
Write-Host "$server 连接失败"  -ForegroundColor Red
$server + "连接失败" |Out-File $task_resultfile -Append
}

}
Else
{
Write-Host "无法Ping通"  -ForegroundColor Red
$server + "无法Ping通" |Out-File $task_resultfile -Append
}
}

$Null = NET USE * /del /y

"任务计划创建完成!"
#cmd /c pause
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: