随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

远程执行脚本

复制代码
 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
复制代码

 

posted on   momingliu11  阅读(1283)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示