【发布时间】:2015-06-17 19:03:04
【问题描述】:
我有这个 PowerShell 脚本,它将收集我的远程服务器的系统信息。
我的问题:我不确定在运行时如何使用此脚本来填充我在 SQL Server Management Studio 中创建的新创建的数据库Hal0Test。
最终目标:我希望我的 powershell 脚本查看远程服务器中的列 QAUTILITYDB01 > ServerList(数据库)> 标记为“ServerName”取“QACGAPPSVR01, QACGAPPSVR03等"将这些值中的每一个推送到我的 powershell 脚本中,并为每台服务器生成其 FQDN 和操作系统。最后,将新值(FQDN 和 OS)推送回远程数据库。
新的 Powershell 脚本代码:
$connectionString = "Server=QAUTILITYDB01;Database=Hal0Test;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$ServerArray = [System.Collections.ArrayList]@()
$query = "SELECT ServerName FROM ServerList"
$command.CommandText = $query
$ServerNames = $command.ExecuteReader()
While ($ServerNames.read()){
$ServerArray.Add($ServerNames[0])
}
foreach($Server in $ServerArray){
# $Server returns each server name
$os = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
$disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $_ |
Where-Object {$_.DriveType -eq 3} |
ForEach-Object {
'{0} {1:D} MB Free/{2:D} MB Used' -f $_.DeviceID,
[int]($_.FreeSpace/1MB), [int]($_.Size/1MB)
}
New-Object -Type PSCustomObject -Property @{
'FQDN' = $_
'ServerName' = $os.PSComputerName
'OperatingSystem' = $os.Caption
'Disks' = $disks -join ' | '
}
$command.CommandText = "UPDATE ServerList SET FQDN = '$_', OS = '$os.Caption' WHERE ServerName = '$os.PSComputerName';"
$result = $command.ExecuteNonQuery()
} Export-Csv 'C:\Desktop\HalO\output.csv' -Delimiter '|' -NoType
Powershell CSV 文件:
ServerName FQDN Disks OperatingSystem
SVR01 Svr01.xxx.com C: 17899 MB Free/51097 MB Used | E: 22277 MB Free/25597 MB Used Microsoft Windows Server 2008 R2 Enterprise
SVR03 svr03.xxx.com C: 18280 MB Free/61337 MB Used | E: 50079 MB Free/56317 MB Used Microsoft Windows Server 2008 R2 Enterprise
SVR05 svr05.xxx.com C: 8751 MB Free/40857 MB Used | E: 4987 MB Free/10237 MB Used Microsoft Windows Server 2008 R2 Enterprise
SVR06 svr06.xxx.com C: 14188 MB Free/61337 MB Used | E: 34962 MB Free/56317 MB Used Microsoft Windows Server 2008 R2 Enterprise
SVR08 Svr08.xxx.com C: 6464 MB Free/40857 MB Used | E: 5921 MB Free/10237 MB Used Microsoft Windows Server 2008 R2 Enterprise
错误:
Exception calling "ExecuteNonQuery" with "0" argument(s): "There is already an open DataReader
associated with this Command which must be closed first."
At C:\Users\mdaraghmeh\Desktop\HalO\test2.ps1:33 char:5
+ $result = $command.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
SQL Server 列表:
【问题讨论】:
标签: sql sql-server database powershell database-design