Mssql/wmi/powershell (iii) Implementing PowerShell remote Scripting

Source: Internet
Author: User
Tags mssql

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

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.