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