Powershell 如何格式化PSObject的输出?
我有几个网站,每个都在自己的虚拟机上托管。我把一个“活文档”放在一起,它将保持一个CSV文件的更新,其中包含每个站点的特定信息 我对每个元素进行了编译,并得到了我想要的信息。但是,导出时,我的对象在CSV文件中的显示与我希望的不同 我为Powershell 如何格式化PSObject的输出?,powershell,Powershell,我有几个网站,每个都在自己的虚拟机上托管。我把一个“活文档”放在一起,它将保持一个CSV文件的更新,其中包含每个站点的特定信息 我对每个元素进行了编译,并得到了我想要的信息。但是,导出时,我的对象在CSV文件中的显示与我希望的不同 我为$module属性获取系统.Object[],然后为$siteversion获取@{property1=value1;property2=value2}。我尝试过使用一些不同的方法来改变这些结果,但我没有看到任何改变 $scriptBlock = [scriptb
$module
属性获取系统.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
基本上,发生这种情况的原因是因为变量不包含您认为它们包含的内容。在调试此类问题时,我通常使用控制台和或
.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
您在哪里使用导出CSV?我假设您正在导出
$siteVersions
和$siteInfo
?我不确定是否有具体的答案,但一般来说,我认为您需要深入查看相关的子项。我刚刚创建了一个可以帮你解决这个问题的。如果它涉及多个属性,如@{property1=value1;property2=value2}
,则需要决定是将它们放在一个单元格中,还是将它们分散在多个单元格中。是的,我正在测试输出并为单个服务器导出$siteInfo
对象。我会编辑原来的帖子来展示这一点。谢谢你提供的信息。我知道我的对象并没有简化。我打算稍后集中精力,把它们清理干净。我现在要检查每个变量的类型,看看是否有不一致的地方。