2017-02-12 56 views
-1

我正在使用系統的DNS名稱從數據庫中提取信息。一旦我獲得了這些信息,我就會設置它來替換我不再需要的計算機名稱的部分。無法展開PSObject

然而,問題是,一旦執行替換,我不能再選擇對象 - 如果您選擇DnsName或只查看變量@{Dnsname=computer123},它將返回空白。我試過-ExpandProperty,但那也行不通。

function ExecuteSqlQuery($SQLQuery) { 
    $Server = "localhost" 
    $Database = "DB123" 
    $Array = @() 

    #Create and open a database connection 
    $Connection = New-Object System.Data.SQLClient.SQLConnection 
    $Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;" 
    $Connection.Open() 

    #Create a command object 
    $Command = New-Object System.Data.SQLClient.SQLCommand 
    $Command.Connection = $Connection 
    $Command.CommandText = $SQLQuery 

    #Execute the Command 
    $Reader = $Command.ExecuteReader() 
    $Counter = $Reader.FieldCount 

    #Parse the records 
    while ($Reader.Read()) { 
    for ($i =0; $i -lt $Counter; $i++) { 
     $Array += $Reader.GetValue($i) 
    } 
    } 

    # Close the database connection 
    $Connection.Close() 
    return $Array 
} 

$DNSsys = "SELECT DnsName FROM Systems" 
$DNSName = ExecuteSqlQuery ($DNSsys) 

$DNSName = $DNSName -replace '.123.com', '' -replace '.abc.com', '' 

我也遇到同樣的問題,當我拉從SCCM與下面的代碼,其中,直到-replace運行一切正常信息:

# Create the PSSession 
$Session = New-PSSession -ComputerName sccmserver 

# Load the CM Module using Implicit Remoting 
Import-Module -Name "C:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" -PSSession $Session 

#Set the CMSite as our current location to run the CM cmdlets 
Invoke-Command -Session $Session {Set-Location -Path LOC:} 

$SCCMdevices = Get-CMDevice -CollectionName "All Systems" | Where {$_.IsActive -like "True"} | Select-Object -Property Name, DeviceOS 

$SCCMdevices = $SCCMdevices -replace 'Microsoft Windows NT Workstation 5.1', 'Windows XP' ` 
         -replace 'Microsoft Windows NT Workstation 5.1 (Tablet Edition)', 'Windows XP' ` 
         -replace 'Microsoft Windows NT Workstation 6.1', 'Windows 7' ` 
         -replace 'Microsoft Windows NT Workstation 6.1 (Tablet Edition)', 'Windows 7' ` 
         -replace 'Microsoft Windows NT Workstation 6.1 (Embedded)', 'Windows 7' ` 
         -replace 'Microsoft Windows NT Workstation 6.2', 'Windows 8' ` 
         -replace 'Microsoft Windows NT Workstation 6.2 (Tablet Edition)', 'Windows 8' ` 
         -replace 'Microsoft Windows NT Workstation 6.3', 'Windows 8.1' ` 
         -replace 'Microsoft Windows NT Workstation 6.3 (Tablet Edition)', 'Windows 8.1' ` 
         -replace 'Microsoft Windows NT Workstation 10.0', 'Windows 10' ` 
         -replace 'Microsoft Windows NT Workstation 10.0 (Tablet Edition)', 'Windows 10' ` 
         -replace 'Microsoft Windows NT Server 5.2', 'Windows Server 2003' ` 
         -replace 'Microsoft Windows NT Advanced Server 5.2', 'Windows Server 2003' ` 
         -replace 'Microsoft Windows NT Server 6.0', 'Windows Server 2008' ` 
         -replace 'Microsoft Windows NT Advanced Server 6.0', 'Windows Server 2008' ` 
         -replace 'Microsoft Windows NT Server 6.1', 'Windows Server 2008 R2' ` 
         -replace 'Microsoft Windows NT Advanced Server 6.1', 'Windows Server 2008 R2' ` 
         -replace 'Microsoft Windows NT Server 6.2', 'Windows Server 2012' ` 
         -replace 'Microsoft Windows NT Advanced Server 6.2', 'Windows Server 2012' ` 
         -replace 'Microsoft Windows NT Server 6.3', 'Windows Server 2012 R2' ` 
         -replace 'Microsoft Windows NT Advanced Server 6.3', 'Windows Server 2012 R2' ` 
         -replace 'Microsoft Windows NT Server 10.0', 'Windows Server 2016' ` 
         -replace 'Microsoft Windows NT Advanced Server 10.0', 'Windows Server 2016' ` 
         -replace '6.1', 'Windows 7' ` 
         -replace '10.0', 'Windows 10' 
+1

您發佈的函數將返回給定查詢的字符串數組,而不是對象數組。 –

+1

請考慮提供[MCVE(最小,完整和可驗證示例)](http://stackoverflow.com/help/mcve)。 – mklement0

+0

添加另一個完全不同的代碼片段不會幫助澄清你的問題。此外,我不強烈建議使用「-replace」操作,而是強烈建議使用第二個腳本中的散列表將操作系統名稱映射到替換值。 –

回答

0

這裏的方式來處理它,所以你」再沒有運行對整個對象的字符串修改器 - 更換,而是僅僅是屬性:

$Array += $Reader.GetValue($i) | Select-Object *,@{N="HostName";E={$_.DNSName -replace '.123.com', '' -replace '.abc.com', ''}}