Remote Script Execution

Source: Internet
Author: User
1 ####################################### define variables ###################################### ################## 2 $ currentpath = $ myinvocation. mycommand. path. substring (0, $ myinvocation. mycommand. path. lastindexof ('\') + 1) 3 # define Server LIST 4 $ server_list = "server_list.txt" 5 $ server_list_path = join-path $ currentpath $ server_list 6 # define task plan output result save file 7 $ task_resultfile = "task_result.txt" 8 $ task_resultfile_path = join-path $ currentpath $ task_resultfile 9 # define the name of the script to be executed 10 $ scriptname = "dnsconfig. PS1 "11 $ scriptpath = join-path $ currentpath $ scriptname12 # define the username and password used 13 $ username =" Administrator "14 15 16 ####### start the script ######################################## ####### 17 # delete an existing IPC session connection 18 $ null = net use */del/y19 $ servers = GC $ server_list_path20 foreach ($ server in $ servers) 21 {22 if (test-connection $ server-count 1-Quiet) 23 {24 write-host $ server-foregroundcolor green25 # obtain the remote computer password 26 27 $ userpass = $ serverpass 28 $ Password = convertid-securestring $ serverpass-asplaintext-force29 $ 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 # remote execution script 34 $ tresult = invoke-command-computername $ server-credential $ cred-filepath $ scriptpath35 36} 37 Else 38 {39 write-host "$ server connection failed "-foregroundcolor red40 $ SERVER +" connection failed "| out-File $ task_resultfile_path-append 41} 42 43} 44 else 45 {46 write-host" cannot be pinged "-foregroundcolor red47 $ SERVER + "cannot ping" | out-File $ task_resultfile_path-append 48} 49} 50 51 52 53 $ null = net use */del/y

 

Remote Script Execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.