我有几个网站,每个网站都托管在自己的虚拟机上。我正在制作一份“活文件”,该文件将通过每个网站的某些信息更新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
你在哪里使用导出,CSV?我假设你正在出口'$ siteVersions'和'$ siteInfo'? – gms0ulman
我不确定这是否有特定的答案,但总的来说,我会说你需要深入了解有关的子项目。我刚刚创建了一个['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
是的,我正在测试输出并为单个服务器导出'$ siteInfo'对象。我将编辑原文以显示。 – T4RH33L