前三篇文章中創建了PSNet程序集,其中包含了對指定IP進行端口掃描,收發TCP消息包和收發UDP消息包的相關功能,作為這是最基本的對網絡情況的最基本檢測,后續的文章將會對此程序集進行不斷的擴充使其包含更全面的功能。但是光有這些簡單網絡探測的功能還遠遠不夠,為了能更全面的使用PowerShell針對網絡安全進行檢測,在本文中將會創建PSSecurity程序集用于存放相關通過PowerShell的腳本。參照前幾篇文章中創建PSNet程序集的方法和目錄結構創建PSSecurity程序集目錄,便于后續對程序集的擴展。
具體詳細的步驟請參見前幾篇文章,創建PSSecurity程序集之后的目錄結構和文件如下所示:
代碼如下:
+D:/MY DOCUMENTS/WINDOWSPOWERSHELL/MODULES
└─PSSecurity
│ PSSecurity.psm1
│
└─SQLServer
Get-SqlSysLogin.ps1
在$Profile中添加:
代碼如下:
Import-Module $env:PSSpace/PSSecurity #用于在PowerShell啟動時自動加載PSSecurity程序集
其中PSSecurity.psm1中的內容如下:
代碼如下:
. $env:PSSpace/PSSecurity/SQLServer/Get-SqlSysLogin.ps1 #導入Get-SqlSysLogin函數
Write-Host "PSSecurity Module Added" -BackgroundColor green -ForegroundColor blue #用于提示此模塊已加載
Export-ModuleMember -Function * #用于將函數導出為模塊成員
接下來就是Get-SqlSysLogin.ps1的內容了
代碼如下:
=====文件名:Get-SqlSysLogin.ps1=====
function Get-SqlSysLogin {
Param(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeLine= $true)]
[Alias("PSComputerName","CN","MachineName","IP","IPAddress")]
[string]$ComputerName,
[parameter(Position = 1)]
[string]$UserName,
[parameter(Position = 2)]
[string]$Password
)
Process {
$Connection = New-Object System.Data.SQLClient.SQLConnection
if($userName) {
$Connection.ConnectionString = "Data Source=$ComputerName;Initial Catalog=Master;User Id=$userName;Password=$password;"
} else {
$Connection.ConnectionString = "server=$computerName;Initial Catalog=Master;trusted_connection=true;"
}
Try {
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand #創建SQLClient對象
$Command.Connection = $Connection
新聞熱點
疑難解答