2017-05-31 9 views
1

私は、それぞれがそれぞれの仮想マシンでホストされているいくつかのWebサイトを持っています。私はCSVファイルを各サイトの特定の情報で更新する「生きた文書」をまとめています。PSObjectの出力をどのように書式設定しますか?

私は各要素をコンパイルして、私が必要とする情報を得ています。ただし、エクスポートされたオブジェクトは、CSVファイル内に表示されないように表示されます。

$moduleプロパティの場合はSystem.Object[]が表示されており、$siteversionの場合は@{property1=value1; property2=value2}が表示されています。私はこれらの結果を変更するためにいくつかの異なるアプローチを試みましたが、私は変化が見られません。

$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

Select-Object

#$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) 

と簡体第二の目的

は、輸出に注文するために使用しますか?あなたは '$ 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} 'それらを1つのセルに配置するのか、複数のセルに分散させるのかを決定する必要があります。 – iRon

+0

はい、出力をテストしていて、単一のサーバーの '$ siteInfo'オブジェクトをエクスポートしました。元の投稿を編集して表示します。 – T4RH33L

答えて

2

基本的には、変数が含まれていると思われる内容が変数に含まれていないためです。これらの種類の問題をデバッグするときは、通常、コンソールとGet-Memberまたは.GetType()のように、を使用します。

これは、コードを読みやすくすると簡単になります。私は、より簡単な方法でカスタムオブジェクトを作成し、使用例としてGetType()を使用しました。単純な文字列/整数の代わりにどの値がオブジェクト/ハッシュテーブルであるかを見つけたら、これは簡単に修正することができます。それ以外の場合は、特定の変数についてコメントしてください。 [エクスポート-CSVを使用している

$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

関連する問題