1

I have several websites with each being hosted on their own virtual machine. I am putting together a "living document" that will keep a CSV file updated with certain information for each site.

I have each element compiled and I am getting the information that I want. However, my objects when exported are displaying not as I would like within the CSV file.

I am getting the System.Object[] for the $module property and then I am getting the @{property1=value1; property2=value2} for $siteversion. I've tried using a few different approaches to change these results but I am seeing no change.

$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
T4RH33L
  • 159
  • 1
  • 4
  • 14
  • 1
    Where are you using Export-CSV? I assume you are exporting `$siteVersions` and `$siteInfo`? – G42 May 31 '17 at 14:30
  • I am not sure whether there is a specific answer for this but In general, I would say that you need to look deeper in the concerned child items. I just created a [`Write-Log` framework](https://stackoverflow.com/questions/41860911/powershell-with-exchange-how-do-i-append-all-verbose-output-to-a-file/44265303#44265303) that might help you with this. If it concerns a multiple properties as `@{property1=value1; property2=value2}` you need to decide if you want to put them in one cell or spread them over multiple cells. – iRon May 31 '17 at 14:37
  • Yes, I was testing the output and exported the `$siteInfo` object for a single server. I will edit the original post to show that. – T4RH33L May 31 '17 at 14:37

1 Answers1

2

Basically, the reason this is happening is because the variables do not contain what you think they contain. When debugging these kinds of issues, I normally use the console and Get-Member or .GetType()alot.

This will be easier if your code is easier to read. I used a simpler way to create custom psobjects, and an example of GetType() in use. Once you find out which values are objects/hashtables instead of simple string/integers, this should be simple to fix. Otherwise please comment with specific variables if they continue to give you trouble.

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

And the second object simplified, with Select-Object used for ordering on export:

$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
G42
  • 9,791
  • 2
  • 19
  • 34
  • 1
    Thanks for the information. I knew that my objects were not simplified. I was going to focus on that later and clean them up. I am going through each of my variables now to check their types and see if anything comes up as a discrepancy. – T4RH33L May 31 '17 at 15:37