What better place to solve a nagging Powershell issue than good ol' SQL Server Central :)I'm trying to retrieve disk usage data from servers on my domain. Server list is pulled from a simple text file on the local computer, each server is then queried for disk information, data is saved in a table, data is written to a database table.This was taken from somewhere on the internet and tweaked to work in my environment, and it has worked splendidly until I was asked to incorporate additional servers that had hyphenated names.[code="other"]#define servers to be monitored$server = get-content C:\<path>\serverList.txt#data table to hold resultsFunction out-DataTable { $dt = new-object Data.datatable $First = $true foreach ($item in $input){ $DR = $DT.NewRow() $Item.PsObject.get_properties() | foreach { if ($first) { $Col = new-object Data.DataColumn $Col.ColumnName = $_.Name.ToString() $DT.Columns.Add($Col) } if ($_.value -eq $null) { $DR.Item($_.Name) = "[empty]" } elseif ($_.IsArray) { $DR.Item($_.Name) =[string]::Join($_.value ,";") } else { $DR.Item($_.Name) = $_.value } } $DT.Rows.Add($DR) $First = $false } return @(,($dt))}#function to retrieve disk informationFunction Get-DisksSpace ([string]$Servername, $unit= "GB"){$measure = "1$unit"Get-WmiObject -computername $serverName -query "select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label from Win32_Volume where DriveType = 2 or DriveType = 3" `| select SystemName ` , Name ` , @{Label="SizeIn$unit";Expression={"{0:n2}" -f($_.Capacity/$measure)}} ` , @{Label="FreeIn$unit";Expression={"{0:n2}" -f($_.freespace/$measure)}} ` , Label}#execute the functionsforeach ($s in $server){#Write what is being retrievedGet-DisksSpace $s#Load into table and database$dataTable = Get-DisksSpace $s | where {$_.name -like "E:\*" -or $_.name -like "C:\*"} | out-DataTable$connectionString = "Data Source=<Server\Instance>; Integrated Security=True;Initial Catalog=<DestinationDB>;"$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString$bulkCopy.DestinationTableName = "<Schema.Table>"$bulkCopy.WriteToServer($dataTable)}[/code]This returns all the information requested except for a system name for hyphenated servers. If you run just the Get-WmiObject cmdlet, two different results are returned.[code="other"]Get-WmiObject -computername computername -query "select SystemName from Win32_Volume where DriveType = 2 or DriveType = 3" [/code]Returns[quote]__GENUS : 2__CLASS : Win32_Volume__SUPERCLASS : __DYNASTY : __RELPATH : __PROPERTY_COUNT : 7__DERIVATION : {}__SERVER : __NAMESPACE : __PATH : SystemName : [b]COMPUTERNAME[/b][/quote][code="other"]Get-WmiObject -computername computer-name -query "select SystemName from Win32_Volume where DriveType = 2 or DriveType = 3" [/code]Returns[quote]__GENUS : 2__CLASS : Win32_Volume__SUPERCLASS : __DYNASTY : __RELPATH : __PROPERTY_COUNT : 1__DERIVATION : {}__SERVER : __NAMESPACE : __PATH : SystemName : [/quote]I've tried surrounding the computer-name with single and double quotes. I've tried escaping the hyphens with backquotes " ` ". Has anyone else ran into this issue before?
↧