Powershell监控操作系统用户账号事件并预警

原文地址:

  1. <img alt="" src="https://img-blog.csdn.net/20130912111656265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXV4aW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast">  
  1.    
 
  1. #  
  2. # 操作系统账号事件(登录、注销、新增、删除、软件安装)  
  3. # 主函数 Main  
  4. # @param string $str not null  
  5. # @param string $code not null  
  6. #   
  7. # Description:  
  8. # 设置登录事件的任务计划时,必须传递这两个参数  
  9. #  
  10.   
  11. #region get-serverip 获取IP  
  12. function get-serverip  
  13. {  
  14.     $serverip=gwmi win32_networkadapterconfiguration | ?{$_.IPAddress -ne $null -and $_.dhcpenabled -eq $false -and {$_.IPEnabled}} | %{$_.IPAddress}  
  15.     if(($serverip.gettype()).isarray)  
  16.     {  
  17.         return $serverip[0]  
  18.     }  
  19.     else  
  20.     {  
  21.         return $serverip  
  22.     }  
  23. }  
  24. #endregion  
  25.   
  26. #region Send-Mail 发送邮件  
  27. function Send-Mail($Subject,$Body)  
  28. {  
  29.     $password = ConvertTo-SecureString 'password' -AsPlainText -Force  
  30.     $Credential = New-Object System.Management.Automation.PSCredential('account',$password)  
  31.     $SmptServer="<a href="http://bxing.net" target="_blank">mail.xx.com.cn</a>"  
  32.     <a href="mailto:$From='a@xx.com.cn'" target="_blank">$From='a@xx.com.cn'  
  33. </a>    $To="test@xx.com.cn"  
  34.       
  35.     #抄送  
  36.     #$Cc="cc@xx.com.cn"  
  37.     $encode=[System.Text.UTF8Encoding]::UTF8  
  38.   
  39.     Send-MailMessage -SmtpServer $SmptServer -Credential $Credential -From $From -to $To -Encoding $Encode -Body $Body -Subject $Subject -Priority High -BodyAsHtml  
  40. }  
  41. #endregion  
  42.   
  43. #region cut-string 裁剪字符串  
  44. function cut-string   
  45. {  
  46.     param(  
  47.         $str,  
  48.         $start,  
  49.         $end  
  50.     )  
  51.     return $str.substring($str.indexof($start),$str.indexof($end)-$str.indexof($start))  
  52. }  
  53. #endregion  
  54.   
  55. #region get_login_user 获取登录账户  
  56. #return string  
  57. function get_login_user  
  58. {  
  59.     $users=query user  
  60.   
  61.     $lists=New-Object system.Collections.ArrayList  
  62.   
  63.     for($i=1;$i -lt $users.Count;$i++)  
  64.     {  
  65.         $user = $users[$i] -replace('  ',' ')  
  66.           
  67.         while($user.indexof('  ') -gt 0)  
  68.         {  
  69.             $user = $user -replace('  ',' ')  
  70.         }  
  71.         if($user.indexof(' ') -eq 0 -or $user.indexof('>') -eq 0)  
  72.         {  
  73.             $user=$user.substring(1)  
  74.         }  
  75.         $user=$user -split(' ')  
  76.           
  77.         $list=New-Object psobject  
  78.         #$time=$user[5]+" "+$user[6]  
  79.   
  80.         Add-Member -Name name -Value $user[0] -MemberType NoteProperty -InputObject $list  
  81.         Add-Member -Name status -Value $user[3] -MemberType NoteProperty -InputObject $list  
  82.         #Add-Member -Name time -Value $time -MemberType NoteProperty -InputObject $list  
  83.         $lists +=@($list)  
  84.     }  
  85.   
  86.     $loginUser = $lists | ?{$_.status -eq '运行中'} | select name  
  87.     foreach($userName in $loginUser)  
  88.     {     
  89.         if($userNames -eq $null)  
  90.         {  
  91.             $userNames=$userName.name  
  92.         }  
  93.         else  
  94.         {  
  95.             $userNames=$userNames + ',' + $userName.name  
  96.         }  
  97.     }  
  98.     return $userNames  
  99. }  
  100. #endregion  
  101.   
  102. #region Login-Succ-Notice 成功登录事件  
  103. function Login-Succ-Notice  
  104. {  
  105.     $loginInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4624} | select timecreated,message  
  106.     if($loginInfo -eq $null)  
  107.     {  
  108.         break  
  109.     }  
  110.     if(($loginInfo.gettype()).isarray)  
  111.     {  
  112.         $time=$loginInfo[0].timecreated  
  113.         $message=$loginInfo[0].message  
  114.     }  
  115.     else  
  116.     {  
  117.         $time=$loginInfo.timecreated  
  118.         $message=$loginInfo.message  
  119.     }  
  120.       
  121.     if($code -eq 1)  
  122.     {  
  123.         $loginType=cut-string $message '登录类型:' '新登录:'  
  124.         $loginType=$loginType -replace('登录类型:','')  
  125.         $loginType=$loginType -replace('            ','')  
  126.         if($loginType -eq 4)  
  127.         {  
  128.             break  
  129.         }  
  130.     }  
  131.       
  132.     $processInfo=cut-string $message '进程名:' '网络信息:'  
  133.     $processInfo=$processInfo -replace('进程名:        ','')  
  134.       
  135.     $message=cut-string $message '新登录' '详细身份验证信息'  
  136.     $loginName=cut-string $message '帐户名:' '帐户域:'  
  137.     $loginName=$loginName -replace('帐户名:','')  
  138.       
  139.     $loginIp=cut-string $message '源网络地址:' '源端口:'  
  140.     $loginIp=$loginIp -replace('源网络地址:','')  
  141.       
  142.     $ip=get-serverip  
  143.     $loginedName=get_login_user  
  144.     $Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>  
  145.   <tr  style='background:#39F'>  
  146.     <td>服务器</td>  
  147.     <td>登录账号</td>  
  148.     <td>进程</td>  
  149.     <td>登录时间</td>  
  150.     <td>客户端IP</td>  
  151.     <td>已登录账号</td>  
  152.   </tr>  
  153.   <tr>  
  154.     <td>$ip</td>  
  155.     <td>$loginName</td>  
  156.     <td>$processInfo</td>  
  157.     <td>$time</td>  
  158.     <td>$loginIp</td>  
  159.     <td>$loginedName</td>  
  160.   </tr>  
  161. </table>"  
  162.     try  
  163.     {  
  164.         Send-Mail "Login on $ip" $Body  
  165.     }  
  166.     catch  
  167.     {  
  168.         ac -Path c:\UserNotice.log -Value "[ $time Login] $error[0]"  
  169.     }  
  170. }  
  171. #endregion  
  172.   
  173. #region Cancel-Succ-Notice 注销登录事件  
  174. function Cancel-Succ-Notice  
  175. {  
  176.     $cancelInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4634} | select timecreated,message  
  177.     if($cancelInfo -eq $null)  
  178.     {  
  179.         break  
  180.     }  
  181.     if(($cancelInfo.gettype()).isarray)  
  182.     {  
  183.         $time=$cancelInfo[0].timecreated  
  184.         $message=$cancelInfo[0].message  
  185.     }  
  186.     else  
  187.     {  
  188.         $time=$cancelInfo.timecreated  
  189.         $message=$cancelInfo.message  
  190.     }  
  191.     $cancelName=cut-string $message '帐户名:' '帐户域:'  
  192.     $cancelName=$cancelName -replace('帐户名:','')  
  193.       
  194.     $ip=get-serverip  
  195.     $loginedName=get_login_user  
  196.     $Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>  
  197.   <tr  style='background:#39F'>  
  198.     <td>服务器</td>  
  199.     <td>注销账号</td>  
  200.     <td>注销时间</td>  
  201.     <td>未注销账号</td>  
  202.   </tr>  
  203.   <tr>  
  204.     <td>$ip</td>  
  205.     <td>$cancelName</td>  
  206.     <td>$time</td>  
  207.     <td>$loginedName</td>  
  208.   </tr>  
  209. </table>"  
  210.     try  
  211.     {  
  212.         Send-Mail "Cancel on $ip" $Body  
  213.     }  
  214.     catch  
  215.     {  
  216.         ac -Path c:\UserNotice.log -Value "[ $time Cancel] $error[0]"  
  217.     }  
  218. }  
  219. #endregion  
  220.   
  221. #region Create-User-Notice 新增账号事件  
  222. function Create-User-Notice  
  223. {  
  224.     $userinfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4722} | select timecreated,message  
  225.     if($userinfo -eq $null)  
  226.     {  
  227.         break  
  228.     }  
  229.     if(($userinfo.gettype()).isarray)  
  230.     {  
  231.         $time=$userinfo[0].timecreated  
  232.         $message=$userinfo[0].message  
  233.     }  
  234.     else  
  235.     {  
  236.         $time=$userinfo.timecreated  
  237.         $message=$userinfo.message  
  238.     }  
  239.     $operateUser=cut-string $message '主题:' '目标帐户:'  
  240.     $operateUser=cut-string $operateUser '帐户名:' '帐户域:'  
  241.     $operateUser=$operateUser -replace('帐户名:','')  
  242.       
  243.     $addUser=$message.substring($message.indexof('目标帐户:'))  
  244.     $addUser=cut-string $addUser '帐户名:' '帐户域:'  
  245.     $addUser=$addUser -replace('帐户名:','')  
  246.       
  247.     $ip=get-serverip  
  248.     $loginedUser=get_login_user  
  249.       
  250.     $Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>  
  251.   <tr  style='background:#39F'>  
  252.     <td>服务器</td>  
  253.     <td>操作账号</td>  
  254.     <td>被添加账号</td>  
  255.     <td>操作时间</td>  
  256.     <td>已登录账号</td>  
  257.   </tr>  
  258.   <tr>  
  259.     <td>$ip</td>  
  260.     <td>$operateUser</td>  
  261.     <td>$addUser</td>  
  262.     <td>$time</td>  
  263.     <td>$loginedUser</td>  
  264.   </tr>  
  265. </table>"  
  266.     try  
  267.     {  
  268.         Send-Mail "AddUser on $ip" $Body  
  269.     }  
  270.     catch  
  271.     {  
  272.         ac -Path c:\UserNotice.log -Value "[ $time AddUser] $error[0]"  
  273.     }  
  274. }  
  275. #endregion  
  276.   
  277. #region Delete-User-Notice 删除账号事件  
  278. function Delete-User-Notice{  
  279.   
  280.     $userInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4726} | select timecreated,message  
  281.     if($userinfo -eq $null)  
  282.     {  
  283.         break  
  284.     }  
  285.     if(($userinfo.gettype()).isarray)  
  286.     {  
  287.         $time=$userinfo[0].timecreated  
  288.         $message=$userinfo[0].message  
  289.     }  
  290.     else  
  291.     {  
  292.         $time=$userinfo.timecreated  
  293.         $message=$userinfo.message  
  294.     }  
  295.     $ip=get-serverip  
  296.     $loginedUser=get_login_user  
  297.       
  298.     $operateUser=cut-string $message '主题:' '目标帐户:'  
  299.     $operateUser=cut-string $operateUser '帐户名:' '帐户域:'  
  300.     $operateUser=$operateUser -replace('帐户名:','')  
  301.     $delUser=$message.substring($message.indexof('目标帐户:'))  
  302.     $delUser=cut-string $delUser '帐户名:' '帐户域:'  
  303.     $delUser=$delUser -replace('帐户名:','')  
  304.       
  305.     $Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>  
  306.   <tr  style='background:#39F'>  
  307.     <td>服务器</td>  
  308.     <td>操作账号</td>  
  309.     <td>被删除账号</td>  
  310.     <td>操作时间</td>  
  311.     <td>已登录账号</td>  
  312.   </tr>  
  313.   <tr>  
  314.     <td>$ip</td>  
  315.     <td>$operateUser</td>  
  316.     <td>$delUser</td>  
  317.     <td>$time</td>  
  318.     <td>$loginedUser</td>  
  319.   </tr>  
  320. </table>"  
  321.     try  
  322.     {  
  323.         Send-Mail "Delete on $ip" $Body  
  324.     }  
  325.     catch  
  326.     {  
  327.         ac -Path c:\UserNotice.log -Value "[ $time Delete] $error[0]"  
  328.     }  
  329. }  
  330. #endregion  
  331.   
  332. #region Software-Setup-Notice 软件安装事件  
  333. function Software-Setup-Notice  
  334. {  
  335.     $softinfo=Get-WinEvent -logname setup -maxevents 10 | ? {$_.id -eq 1610} | select timecreated,message  
  336.     if($softinfo -eq $null)  
  337.     {  
  338.         break  
  339.     }  
  340.     if(($softinfo.gettype()).isarray)  
  341.     {  
  342.         $time=$softinfo[0].timecreated  
  343.         $time=$softinfo[0].tostring()  
  344.         $message=$softinfo[0].message  
  345.     }  
  346.     else  
  347.     {  
  348.         $time=$softinfo.timecreated  
  349.         $time=$time.tostring()  
  350.         $message=$softinfo.message  
  351.     }  
  352.     $ip=get-serverip  
  353.     $loginedUser=get_login_user  
  354.       
  355.     $Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>  
  356.   <tr  style='background:#39F'>  
  357.     <td>服务器</td>  
  358.     <td>已登录账号</td>  
  359.     <td>安装时间</td>  
  360.     <td>安装信息</td>  
  361.   </tr>  
  362.   <tr>  
  363.     <td>$ip</td>  
  364.     <td>$loginedUser</td>  
  365.     <td>$time</td>  
  366.     <td>$message</td>  
  367.   </tr>  
  368. </table>"  
  369.   
  370.     try  
  371.     {  
  372.         Send-Mail 'Setup on $ip' $Body  
  373.     }  
  374.     catch  
  375.     {  
  376.         ac -Path c:\UserNotice.log -Value "[ $time Setup] $error[0]"  
  377.     }  
  378. }  
  379. #endregion  
  380.   
  381. #region Main 入口函数  
  382. function Main{  
  383.   
  384.     param(  
  385.     $str,  
  386.     $script:code  
  387.     )  
  388.       
  389.     if($str -eq $null)  
  390.     {  
  391.         Write-Warning 参数丢失!  
  392.         sleep 2  
  393.         break  
  394.     }  
  395.     if($str -eq 'login')  
  396.     {  
  397.         Login-Succ-Notice  
  398.     }  
  399.     if($str -eq 'cancel')  
  400.     {  
  401.         Cancel-Succ-Notice  
  402.     }  
  403.     if($str -eq 'add')  
  404.     {  
  405.         Create-User-Notice  
  406.     }  
  407.     if($str -eq 'delete')  
  408.     {  
  409.         Delete-User-Notice  
  410.     }  
  411.     if($str -eq 'setup')  
  412.     {  
  413.         Software-Setup-Notice  
  414.     }  
  415. }  
  416. #endregion  
  417.   
  418. main $args[0] $args[1]  
#
# 操作系统账号事件(登录、注销、新增、删除、软件安装)
# 主函数 Main
# @param string $str not null
# @param string $code not null
# 
# Description:
# 设置登录事件的任务计划时,必须传递这两个参数
#

#region get-serverip 获取IP
function get-serverip
{
    $serverip=gwmi win32_networkadapterconfiguration | ?{$_.IPAddress -ne $null -and $_.dhcpenabled -eq $false -and {$_.IPEnabled}} | %{$_.IPAddress}
	if(($serverip.gettype()).isarray)
	{
	    return $serverip[0]
	}
	else
	{
	    return $serverip
	}
}
#endregion

#region Send-Mail 发送邮件
function Send-Mail($Subject,$Body)
{
    $password = ConvertTo-SecureString 'password' -AsPlainText -Force
    $Credential = New-Object System.Management.Automation.PSCredential('account',$password)
    $SmptServer="mail.xx.com.cn"
    $From='a@xx.com.cn'
    $To="test@xx.com.cn"
	
	#抄送
	#$Cc="cc@xx.com.cn"
    $encode=[System.Text.UTF8Encoding]::UTF8

    Send-MailMessage -SmtpServer $SmptServer -Credential $Credential -From $From -to $To -Encoding $Encode -Body $Body -Subject $Subject -Priority High -BodyAsHtml
}
#endregion

#region cut-string 裁剪字符串
function cut-string 
{
	param(
		$str,
		$start,
		$end
	)
	return $str.substring($str.indexof($start),$str.indexof($end)-$str.indexof($start))
}
#endregion

#region get_login_user 获取登录账户
#return string
function get_login_user
{
	$users=query user

	$lists=New-Object system.Collections.ArrayList

	for($i=1;$i -lt $users.Count;$i++)
	{
		$user = $users[$i] -replace('  ',' ')
		
		while($user.indexof('  ') -gt 0)
		{
			$user = $user -replace('  ',' ')
		}
		if($user.indexof(' ') -eq 0 -or $user.indexof('>') -eq 0)
		{
			$user=$user.substring(1)
		}
		$user=$user -split(' ')
		
		$list=New-Object psobject
		#$time=$user[5]+" "+$user[6]

		Add-Member -Name name -Value $user[0] -MemberType NoteProperty -InputObject $list
		Add-Member -Name status -Value $user[3] -MemberType NoteProperty -InputObject $list
		#Add-Member -Name time -Value $time -MemberType NoteProperty -InputObject $list
		$lists +=@($list)
	}

	$loginUser = $lists | ?{$_.status -eq '运行中'} | select name
	foreach($userName in $loginUser)
	{	
		if($userNames -eq $null)
		{
			$userNames=$userName.name
		}
		else
		{
			$userNames=$userNames + ',' + $userName.name
		}
	}
	return $userNames
}
#endregion

#region Login-Succ-Notice 成功登录事件
function Login-Succ-Notice
{
	$loginInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4624} | select timecreated,message
	if($loginInfo -eq $null)
	{
		break
	}
	if(($loginInfo.gettype()).isarray)
	{
		$time=$loginInfo[0].timecreated
		$message=$loginInfo[0].message
	}
	else
	{
		$time=$loginInfo.timecreated
		$message=$loginInfo.message
	}
	
	if($code -eq 1)
	{
		$loginType=cut-string $message '登录类型:' '新登录:'
		$loginType=$loginType -replace('登录类型:','')
		$loginType=$loginType -replace('			','')
		if($loginType -eq 4)
		{
			break
		}
	}
	
	$processInfo=cut-string $message '进程名:' '网络信息:'
	$processInfo=$processInfo -replace('进程名:		','')
	
	$message=cut-string $message '新登录' '详细身份验证信息'
	$loginName=cut-string $message '帐户名:' '帐户域:'
	$loginName=$loginName -replace('帐户名:','')
	
	$loginIp=cut-string $message '源网络地址:' '源端口:'
	$loginIp=$loginIp -replace('源网络地址:','')
	
	$ip=get-serverip
	$loginedName=get_login_user
	$Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>
  <tr  style='background:#39F'>
  	<td>服务器</td>
    <td>登录账号</td>
	<td>进程</td>
    <td>登录时间</td>
	<td>客户端IP</td>
    <td>已登录账号</td>
  </tr>
  <tr>
    <td>$ip</td>
    <td>$loginName</td>
	<td>$processInfo</td>
    <td>$time</td>
    <td>$loginIp</td>
	<td>$loginedName</td>
  </tr>
</table>"
	try
	{
		Send-Mail "Login on $ip" $Body
	}
	catch
	{
		ac -Path c:\UserNotice.log -Value "[ $time Login] $error[0]"
	}
}
#endregion

#region Cancel-Succ-Notice 注销登录事件
function Cancel-Succ-Notice
{
	$cancelInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4634} | select timecreated,message
	if($cancelInfo -eq $null)
	{
		break
	}
	if(($cancelInfo.gettype()).isarray)
	{
		$time=$cancelInfo[0].timecreated
		$message=$cancelInfo[0].message
	}
	else
	{
		$time=$cancelInfo.timecreated
		$message=$cancelInfo.message
	}
	$cancelName=cut-string $message '帐户名:' '帐户域:'
	$cancelName=$cancelName -replace('帐户名:','')
	
	$ip=get-serverip
	$loginedName=get_login_user
	$Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>
  <tr  style='background:#39F'>
  	<td>服务器</td>
    <td>注销账号</td>
    <td>注销时间</td>
    <td>未注销账号</td>
  </tr>
  <tr>
    <td>$ip</td>
    <td>$cancelName</td>
    <td>$time</td>
	<td>$loginedName</td>
  </tr>
</table>"
	try
	{
		Send-Mail "Cancel on $ip" $Body
	}
	catch
	{
		ac -Path c:\UserNotice.log -Value "[ $time Cancel] $error[0]"
	}
}
#endregion

#region Create-User-Notice 新增账号事件
function Create-User-Notice
{
    $userinfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4722} | select timecreated,message
	if($userinfo -eq $null)
	{
		break
	}
	if(($userinfo.gettype()).isarray)
	{
		$time=$userinfo[0].timecreated
		$message=$userinfo[0].message
	}
	else
	{
		$time=$userinfo.timecreated
		$message=$userinfo.message
	}
	$operateUser=cut-string $message '主题:' '目标帐户:'
	$operateUser=cut-string $operateUser '帐户名:' '帐户域:'
	$operateUser=$operateUser -replace('帐户名:','')
	
	$addUser=$message.substring($message.indexof('目标帐户:'))
	$addUser=cut-string $addUser '帐户名:' '帐户域:'
	$addUser=$addUser -replace('帐户名:','')
	
	$ip=get-serverip
	$loginedUser=get_login_user
	
	$Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>
  <tr  style='background:#39F'>
  	<td>服务器</td>
    <td>操作账号</td>
    <td>被添加账号</td>
    <td>操作时间</td>
	<td>已登录账号</td>
  </tr>
  <tr>
    <td>$ip</td>
    <td>$operateUser</td>
    <td>$addUser</td>
	<td>$time</td>
	<td>$loginedUser</td>
  </tr>
</table>"
	try
	{
		Send-Mail "AddUser on $ip" $Body
	}
	catch
	{
		ac -Path c:\UserNotice.log -Value "[ $time AddUser] $error[0]"
	}
}
#endregion

#region Delete-User-Notice 删除账号事件
function Delete-User-Notice{

	$userInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4726} | select timecreated,message
	if($userinfo -eq $null)
	{
		break
	}
	if(($userinfo.gettype()).isarray)
	{
		$time=$userinfo[0].timecreated
		$message=$userinfo[0].message
	}
	else
	{
		$time=$userinfo.timecreated
		$message=$userinfo.message
	}
	$ip=get-serverip
	$loginedUser=get_login_user
	
	$operateUser=cut-string $message '主题:' '目标帐户:'
	$operateUser=cut-string $operateUser '帐户名:' '帐户域:'
	$operateUser=$operateUser -replace('帐户名:','')
	$delUser=$message.substring($message.indexof('目标帐户:'))
	$delUser=cut-string $delUser '帐户名:' '帐户域:'
	$delUser=$delUser -replace('帐户名:','')
	
	$Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>
  <tr  style='background:#39F'>
  	<td>服务器</td>
    <td>操作账号</td>
    <td>被删除账号</td>
    <td>操作时间</td>
	<td>已登录账号</td>
  </tr>
  <tr>
    <td>$ip</td>
    <td>$operateUser</td>
    <td>$delUser</td>
	<td>$time</td>
	<td>$loginedUser</td>
  </tr>
</table>"
	try
	{
		Send-Mail "Delete on $ip" $Body
	}
	catch
	{
		ac -Path c:\UserNotice.log -Value "[ $time Delete] $error[0]"
	}
}
#endregion

#region Software-Setup-Notice 软件安装事件
function Software-Setup-Notice
{
	$softinfo=Get-WinEvent -logname setup -maxevents 10 | ? {$_.id -eq 1610} | select timecreated,message
	if($softinfo -eq $null)
	{
		break
	}
	if(($softinfo.gettype()).isarray)
	{
		$time=$softinfo[0].timecreated
		$time=$softinfo[0].tostring()
		$message=$softinfo[0].message
	}
	else
	{
		$time=$softinfo.timecreated
		$time=$time.tostring()
		$message=$softinfo.message
	}
	$ip=get-serverip
	$loginedUser=get_login_user
	
	$Body="<table width='700' border='1' cellpadding='0' cellspacing='0' style='font-size:13px;'>
  <tr  style='background:#39F'>
  	<td>服务器</td>
    <td>已登录账号</td>
    <td>安装时间</td>
    <td>安装信息</td>
  </tr>
  <tr>
    <td>$ip</td>
    <td>$loginedUser</td>
    <td>$time</td>
	<td>$message</td>
  </tr>
</table>"

	try
	{
		Send-Mail 'Setup on $ip' $Body
	}
	catch
	{
		ac -Path c:\UserNotice.log -Value "[ $time Setup] $error[0]"
	}
}
#endregion

#region Main 入口函数
function Main{

	param(
	$str,
	$script:code
	)
	
	if($str -eq $null)
	{
		Write-Warning 参数丢失!
		sleep 2
		break
	}
	if($str -eq 'login')
	{
		Login-Succ-Notice
	}
	if($str -eq 'cancel')
	{
		Cancel-Succ-Notice
	}
	if($str -eq 'add')
	{
		Create-User-Notice
	}
	if($str -eq 'delete')
	{
		Delete-User-Notice
	}
	if($str -eq 'setup')
	{
		Software-Setup-Notice
	}
}
#endregion

main $args[0] $args[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用以下命令获取操作系统信息: ```powershell Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture, BuildNumber, RegisteredUser ``` 该命令使用了 `Get-CimInstance` 命令来获取 `Win32_OperatingSystem` 类的实例,然后使用 `Select-Object` 命令选择需要的属性,包括操作系统名称、版本、架构、构建号和注册用户。 ### 回答2: PowerShell是Windows系统中的一种脚本语言,它可以很方便地获取操作系统(OS)信息。下面,我们将通过以下四个步骤来学习如何获取操作系统(OS)信息: 1. 打开PowerShell 在Windows系统中,用户可以通过两种方式打开PowerShell。第一种方式是通过Windows搜索功能,在搜索框中输入PowerShell,并点击搜索结果中的PowerShell应用程序。第二种方式是通过Windows开始菜单,点击启动菜单中的Windows PowerShell程序。 2. 获取操作系统(OS)的信息 在PowerShell中,用户可以使用系统内置的命令获取操作系统的信息。下面列举了几个用于获取操作系统信息的命令: - Get-CimInstance Win32_OperatingSystem: 获取操作系统的信息,包括操作系统的名称、版本、安装日期和语言等。 - Get-ComputerInfo: 获取计算机的信息,包括计算机名称、操作系统名称和版本、处理器、内存和硬盘等情况。 - [System.Environment]::OSVersion: 获取当前系统的操作系统版本。 - [System.Environment]::MachineName: 获取当前计算机名称。 3. 显示操作系统(OS)的信息 在获取操作系统(OS)信息之后,需要将这些信息显示在屏幕上。可以使用如下命令将信息输出到屏幕: - Write-Host “操作系统名称: $($os.Name)” - Write-Host “操作系统版本: $($os.Version)” - Write-Host “安装日期: $($os.InstallDate)” - Write-Host “语言: $($os.Language)” 4. 保存操作系统(OS)的信息 如果需要将操作系统(OS)信息保存到文件中,可以使用如下命令: - Get-CimInstance Win32_OperatingSystem | Export-Csv -Path “C:\OS_Info.csv” 上述命令将获取操作系统(OS)信息并保存到C盘根目录下的OS_Info.csv文件中。 以上就是获取操作系统(OS)信息的四个步骤。掌握这些命令和技巧将让你在开发过程中更为高效和便捷。 ### 回答3: Powershell是一个强大的基于命令行的脚本编程语言,可以用来获取操作系统OS信息。在Powershell中,我们可以使用系统内置的命令和函数来获取OS信息。 首先,我们可以使用Get-WmiObject命令来获取操作系统的基本信息。这个命令返回一个WMI对象,该对象包含有关操作系统的许多信息,例如操作系统版本,安装日期,硬件配置和系统启动时间等等。使用以下命令来获取操作系统信息: Get-WmiObject -Class Win32_OperatingSystem 此命令将返回一个Win32_OperatingSystem对象,该对象包含有关当前操作系统的信息。 输出结果中包括: 1. Caption:本地操作系统的名称。 2. Version:本地操作系统的版本号。 3. CSDVersion:安装的Service Pack版本号。 4. BuildNumber:本地操作系统的构建号。 5. SerialNumber:本地操作系统的序列号。 6. InstallDate:操作系统的安装日期。 7. Manufacturer:操作系统的制造商。 8. OSArchitecture:操作系统的体系结构(32位或64位)。 除了使用Get-WmiObject命令外,我们还可以使用其他一些命令和函数来获取操作系统信息。例如,可以使用以下命令来获取操作系统版本: [System.Environment]::OSVersion.VersionString 这个命令将返回当前操作系统的版本号,如 “Microsoft Windows 10 Enterprise 10.0.18363 版本 1909”。 总之,Powershell是一个功能强大的脚本编程语言,可以用来获取操作系统OS信息。无论是使用系统内置的命令和函数,还是使用自己编写的脚本,Powershell都可以帮助我们轻松地获取系统信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值