使用PowerShell登陆多台Windows,测试DCAgent方法

目标:

需要1台PC用域账户远程登陆10台PC,每台登陆后的PC执行发送敏感数据的操作后,再logoff。

在DCAgent服务器上,查看这10个用户每次登陆时,DCAgent是否能获取到登陆信息(IP:User)

 

前提:

  1. 需要在AD域内添加测试账户,并将这些账户添加到一个group内。
  2. 需要将每个PC注册到AD域内,并设置group内用户都可以远程登陆。
  3. 需要设置指定用户登陆后,执行定时任务,定时任务为:发送敏感数据后,执行logoff操作。
  4. 需要定时读取DCAgent.bak文件的内容并记录。

测试步骤:

  1. 添加AD域账户,并将这些账户添加到一个group内。

    我创建了user21-30这10个用户,并将这些用户添加到dc_group1中。

     2.   需要将步骤1中的10个PC注册到AD域内,并设置group内用户都可以远程登陆。

 

  1. 1.   对指定的登陆用户设置定时任务:

PC IP地址

用户名

172.18.0.21

user21

172.18.0.22

user22

172.18.0.23

user23

172.18.0.24

user24

172.18.0.25

user25

172.18.0.26

user26

172.18.0.27

user27

172.18.0.28

user28

172.18.0.29

user29

172.18.0.30

user30

 

1)        下载curl.exe程序,用来发送http post请求。

2)        编写test.bat批处理程序,用来执行发送敏感数据和logoff操作

test.bat:

echo start
@echo off

\test_folder\curl.exe -X POST http://172.16.0.10/cgi-bin/save_file_linux.py  -F "filename=test.txt" -v
echo job is done
shutdown -L

3)        设置定时任务:

开始->所有程序->附件->系统工具->任务计划程序->

 

4.  在管理PC上执行powershell.ise:

 

Powershell脚本代码:

function Connect-RDP {
 
  param (
    [Parameter(Mandatory=$true)]
    $ComputerName,
 
    [System.Management.Automation.Credential()]
    $Credential
  )
 
  # take each computername and process it individually
  $ComputerName | ForEach-Object {
 
    # if the user has submitted a credential, store it
    # safely using cmdkey.exe for the given connection
    if ($PSBoundParameters.ContainsKey('Credential'))
    {
      # extract username and password from credential
      $User = $Credential.UserName
      $Password = $Credential.GetNetworkCredential().Password
 
      # save information using cmdkey.exe
      cmdkey.exe /generic:$_ /user:$User /pass $Password
    }
 
    # initiate the RDP connection
    # connection will automatically use cached credentials
    # if there are no cached credentials, you will have to log on
    # manually, so on first use, make sure you use -Credential to submit
    # logon credential
 
    mstsc.exe /v $_ 
  }
}


#Connect-RDP 172.18.0.101 -Credential zoo\user12
$a = 1

F:/press_cancel_btn.au3

DO
{
Connect-RDP 172.18.0.21
Connect-RDP 172.18.0.22
Connect-RDP 172.18.0.23
Connect-RDP 172.18.0.24
Connect-RDP 172.18.0.25
Connect-RDP 172.18.0.26
Connect-RDP 172.18.0.27
Connect-RDP 172.18.0.28
Connect-RDP 172.18.0.29
Connect-RDP 172.18.0.30

timeout /t 250
$a++
} While ($a -le 10)


If($a -eq 11)
{
Stop-Process -processname AutoIt3*
echo finished
}

press_cancel_btn.au3代码:

需要安装autoit3程序。

#include <AutoItConstants.au3>

Dim $count = 12


while $count < 243

;click Windows PowerShell Credential Request cancel button
ControlClick("Windows PowerShell Credential Request", "","Button3")


Wend

注意:在第一次执行powershell脚本时,需要让PC记录每个远程桌面的Credential,所以在第一次时需要这样执行:

 

Connect-RDP 172.18.0.21 -Credential zoo\user21

Connect-RDP 172.18.0.21 -Credential zoo\user22

Connect-RDP 172.18.0.21 -Credential zoo\user23

Connect-RDP 172.18.0.21 -Credential zoo\user30

 

在正常的测试时,只需要按脚本执行即可。

5.在DCAgent服务器上定时查看DCAgent.bak文件并记录:

Record.py

import time

while True:
    origin_file=open('DCAgent.bak','rb')
    target_file=open('record.txt','a')

    for line in origin_file:
        content = line.strip()
        target_file.write(content)

    target_file.write('\n')

    origin_file.close()
    target_file.close()
    time.sleep(10)

 

posted @ 2016-03-21 16:07  bonjov1  阅读(1351)  评论(0编辑  收藏  举报