1 #######################################定义变量#########################################################
2 $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
3 #定义服务器列表
4 $server_list = "server_list.txt"
5 $server_list_path = Join-Path $CurrentPath $server_list
6 #定义任务计划输出结果保存文件
7 $task_resultfile = "Task_Result.txt"
8 $task_resultfile_path = Join-Path $CurrentPath $task_resultfile
9 #定义需要执行的脚本名称
10 $scriptName = "DNSConfig.ps1"
11 $scriptPath = Join-Path $CurrentPath $scriptName
12 #定义使用到的用户名和密码
13 $UserName = "administrator"
14
15
16 #######脚本开始###############################################
17 #删除已有的IPC会话连接
18 $Null = NET USE * /del /y
19 $servers = gc $server_list_path
20 foreach ($server in $servers)
21 {
22 If ( Test-Connection $server -Count 1 -Quiet )
23 {
24 Write-Host $server -ForegroundColor green
25 #获取远程计算机的密码
26
27 $UserPass = $serverpass
28 $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
29 $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
30 cmd /c "NET USE \\$Server $UserPass /user:$UserName >nul 2>nul"
31 If ($Lastexitcode -eq 0)
32 {
33 #远程执行脚本
34 $Tresult = invoke-command -ComputerName $server -Credential $cred -FilePath $scriptPath
35
36 }
37 Else
38 {
39 Write-Host "$server 连接失败" -ForegroundColor Red
40 $server + "连接失败" |Out-File $task_resultfile_path -Append
41 }
42
43 }
44 Else
45 {
46 Write-Host "无法Ping通" -ForegroundColor Red
47 $server + "无法Ping通" |Out-File $task_resultfile_path -Append
48 }
49 }
50
51
52
53 $Null = NET USE * /del /y