Fundamentals of real-time monitoring: WMI monitor-> Database Server (intranet)->powershell-> monitoring Server (internal and external network), send
Described earlier how to create a WMI monitor, this article describes how to execute a PowerShell remote script to upload the information that WMI obtains from the database server to the monitoring server, the database server (intranet)->powershell-> monitoring Server (internal and external network )
First, the account password information encryption
Set the key, encrypt the key, account number, password, and store the information in the text
1. Set Encryption key
function Set-key {
Param ([string] $string)
$length = $string. length
$pad = 32-$length
if ($length-lt)-or ($length-gt)) {Throw "String must be between and characters"}
$encoding = New-object System.Text.ASCIIEncoding
$bytes = $encoding. GetBytes ($string + "0" * $pad)
Return $bytes
}
2. Encryption method
# #set Encrypted Data
function Set-encrypteddata {
Param ($key, [string] $plainText)
$securestring = New-object System.Security.SecureString
$chars = $plainText. ToCharArray ()
foreach ($char in $chars) {$secureString. Appendchar ($char)}
$encryptedData = convertfrom-securestring-securestring $secureString-key $key
Return $encryptedData
}
3. Decryption method
# #get Encrypted Data
function Get-encrypteddata {
Param ($key, $data)
$data | Convertto-securestring-key $key |
foreach-object {[Runtime.InteropServices.Marshal]::P trtostringauto ([Runtime.InteropServices.Marshal]:: Securestringtobstr ($_))}
}
Second, create a remote session and execute the remote script
Get the key and decrypt the account password
$idkeystr =GC D:\xxx\IDkey.txt
$userid =GC D:\xxx\ID.txt
$IDkey =set-key $idkeystr
$appServerUser =get-encrypteddata $IDkey $userid
$pwkeystr =GC D:\xxx\PWkey.txt
$PASSWD =GC D:\xxx\PW.txt
$PWkey =set-key $pwkeystr
$APPSERVERPWD =get-encrypteddata $PWkey $passwd
$appServer = ' Monitorserver '
$password = convertto-securestring $appServerPwd-asplaintext-force
$appCred = New-object System.Management.Automation.PsCredential ($appServerUser, $password)
# #创建会话
$s = new-pssession-computername $appServer-credential $appCred-usessl-sessionoption (New-pssessionoption- Skipcacheck-skipcncheck)-authentication Negotiate
# #执行无参数脚本
Invoke-command-session $s-scriptblock {powershell-file E:\\xxx\\xxx.ps1}
# #执行带参数脚本
Invoke-command-session $s-scriptblock {powershell-file e:\\xxx\\xxx.ps1 $args [0] $args [1] $args [2]}-argumentlist $tex T1, $text 2, $text 3
# #删除会话
$s | Remove-pssession
The next article describes how to send information via PowerShell
This article is from the "Aimax" blog, make sure to keep this source http://aimax.blog.51cto.com/11610508/1967783
Mssql/wmi/powershell (iii) Implementing PowerShell remote Scripting