2017-05-31 123 views
1

我有幾個網站,每個網站都託管在自己的虛擬機上。我正在製作一份「活文件」,該文件將通過每個網站的某些信息更新CSV文件。如何格式化PSObject的輸出?

我已經編譯了每個元素,並且獲取了我想要的信息。但是,導出時我的對象在CSV文件中顯示的並不是我想要的。

我得到System.Object[]$module財產,然後我得到@{property1=value1; property2=value2}$siteversion。我嘗試過使用一些不同的方法來改變這些結果,但我沒有看到任何改變。

$scriptBlock = [scriptblock]::Create({  
Import-Module WebAdministration 

###Getting Client Name### 
$licensePath = 'D:\websites\website_Prod\website\license.xml' 
$license = (Get-Content $licensePath) -as [XML] 
$license = $license.software_License 
$license = $license.License 
$clientName = $license.Name  

###Getting Local Path### 
$localPath = Get-WebApplication website | Select -ExpandProperty PhysicalPath  

###Getting the URL### 
$URL = Get-ChildItem -path IIS:SSLBindings | Where-Object {$_.Sites -eq "website"} 
$URL = $URL.Host 

###Getting Security Model### 
$webConfigFilePath = 'D:\websites\website_Prod\website\web.config' 
$webConfigFile = (Get-Content $webConfigFilePath) -as [XML] 
$appSettings = $webConfigFile.configuration.appsettings 
$appSettings = $appSettings.add 
$securityModel = $appSettings | Where-Object {$_.Key -eq "AuthenMode"} | Select-Object -ExpandProperty Value 

###Getting Service Types###  
$licensePath = 'D:\websites\website_Prod\website\license.xml' 
$license = (Get-Content $licensePath) -as [xml] 
$license = $license.software_License 
$license = $license.License 
$module = $license.Module 
$module = $module | Select -ExpandProperty ModuleName   

###Getting Certificate Expiration### 
$sslBinding = Get-ChildItem -Path IIS:SSLBindings 
$cert = $sslBinding | ForEach-Object -Process {Get-ChildItem -path Cert:\localMachine\My | Where-Object -Property Thumbprint -eq -Value $_.Thumbprint} 
$expiration = $cert.NotAfter  

###Getting Site Name### 
$siteNames = Get-ChildItem -Path D:\Websites | Where-Object {$_.Name -notlike "SFTP" -and $_.Name -notlike "wwwroot"} | Select-Object Name 

###Getting Site Versions### 
$siteVersions = @()  
    Foreach($site in $siteNames){ 
     $app = Get-ChildItem d:\websites | Where-Object {$_.Name -eq $site.Name} 
     $app = $app.FullName 
     $app = $app + '\website\bin'   
     $dll = Get-ChildItem $app | Where-Object {$_.Name -eq "website.Core.DLL"} 
     $version = $dll.VersionInfo | Select-Object -ExpandProperty FileVersion 
     $version 
     $versionObject = New-Object -TypeName PSObject 
     $versionObject | Add-Member -MemberType NoteProperty -Name SiteName -Value $Site.Name 
     $versionObject | Add-Member -MemberType NoteProperty -Name Version -Value $version 
     $siteVersions += $versionObject   

    }#Foreach($site in $siteNames) 

$siteInfo = New-Object -TypeName PSObject 
$siteInfo | Add-Member -MemberType NoteProperty -Name ClientName -Value $clientName  
$siteInfo | Add-Member -MemberType NoteProperty -Name Site -Value $siteVersion  
$siteInfo | Add-Member -MemberType NoteProperty -Name ServiceTypes -Value $module 
$siteInfo | Add-Member -MemberType NoteProperty -Name URL -Value $URL 
$siteInfo | Add-Member -MemberType NoteProperty -Name LocalPath -Value $localPath 
$siteInfo | Add-Member -MemberType NoteProperty -Name SecurityModel -Value $securityModel 
$siteInfo | Add-Member -MemberType NoteProperty -Name SSLCertExperiation -Value $expiration 

$siteInfo | export-csv d:\tmp\test.csv -notypeinformation 
})#ScriptBlock 
$data = Invoke-Command -ComputerName server -ScriptBlock $scriptBlock 
+1

你在哪裏使用導出,CSV?我假設你正在出口'$ siteVersions'和'$ siteInfo'? – gms0ulman

+0

我不確定這是否有特定的答案,但總的來說,我會說你需要深入瞭解有關的子項目。我剛剛創建了一個['Write-Log'框架](https://stackoverflow.com/questions/41860911/powershell-with-exchange-how-do-i-append-all-verbose-output-to-a-file/44265303#44265303),這可能會幫助你。 如果它涉及多個屬性爲'@ {property1 = value1; property2 = value2}'您需要決定是將它們放入一個單元格還是將它們分散到多個單元格中。 – iRon

+0

是的,我正在測試輸出併爲單個服務器導出'$ siteInfo'對象。我將編輯原文以顯示。 – T4RH33L

回答

2

基本上,這種情況發生的原因是因爲變量不包含您認爲它們包含的內容。在調試這些類型的問題時,我通常使用控制檯和Get-Member.GetType()很多

如果您的代碼更易於閱讀,這樣會更容易。我使用了一種更簡單的方式來創建自定義psobject,並且使用了一個GetType()的示例。一旦找出哪些值是對象/散列表而不是簡單的字符串/整數,這應該很容易解決。否則,如果他們繼續給你帶來麻煩,請對具體變量進行評論。

#$siteVersions = @() you can cast $siteVersions as an array on the same line you use it and do not need this line. 
    Foreach($site in $siteNames){ 
     $app = Get-ChildItem d:\websites | Where-Object {$_.Name -eq $site.Name} 
     $app = $app.FullName 
     $app = $app + '\website\bin'   

     $dll = Get-ChildItem $app | Where-Object {$_.Name -eq "website.Core.DLL"} 

     $version = $dll.VersionInfo | Select-Object -ExpandProperty FileVersion 

     # Ensure these are what you expect (i.e.strings) 
     $($site.name).GetType() 
     $version.GetType() 

     # an easy way to create an array of objects 
     [array]$siteVersions += New-Object psobject -Property @{ 
      SiteName = $site.Name 
      Version = $version 
     } 


    }#Foreach($site in $siteNames) 

,簡化了第二個對象,以Select-Object用於訂購出口:

$siteInfo = New-Object -TypeName PSObject =Property @{ 
    ClientName   = $clientName  
    Site    = $siteVersion  
    ServiceTypes  = $module 
    URL     = $URL 
    LocalPath   = $localPath 
    SecurityModel  = $securityModel 
    SSLCertExperiation = $expiration 
} 

# use Select so that you can see what each column contains. 
$siteInfo | Select-Object ClientName, Site, ServiceTypes, URL, LocalPath, SecurityModel, SSLCertExperiation | export-csv d:\tmp\test.csv -notypeinformation 
+1

感謝您的信息。我知道我的東西沒有被簡化。之後我將重點關注並清理它們。我正在通過我的每個變量來檢查它們的類型,看看是否有任何東西出現作爲差異。 – T4RH33L