PowerShell Solutions - Working with files

  • Comments posted to this topic are about the item PowerShell Solutions - Working with files

  • Hi Daniel

    This article is clearly written and very use full.

    Rajen

  • By "merge", do you mean "append"?

  • Good basics, thanks.

  • For the convert to html if you include the [ -notype ] at the end of your line of code the 2 PowerShell related columns will not show up in your report. See the example line below that I use to fetch the vlf's from a server using powershell except this is a conversion to a .csv file. I use the same technique for my html backup report.

    \output\VirtualLogFiles_$SQLServerName_$(get-date -f MM_dd_yyyy_HHmm)_$ServerStamp.csv -notype

  • Rather than:

    $date=(Get-Date -Format d).toString()

    $date=$date.replace("/","-")

    I prefer using the format operator -f with a 4-digit year and fully padded days and months in YYYYMMDD order to make sorting easier. Modifying the formatting is very simple:

    $date = "{0:yyyyMMddHHmm}" -f (Get-Date)

    201610041504

    $date = "{0:yyyyMMddHHmmss}" -f (Get-Date)

    20161004150441

    $date = "{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date)

    2016-10-04-15-04-41

    $date = "{0:yyyy-MM-dd HH:mm:ss}" -f (Get-Date)

    2016-10-04 15:04:41

    Or in your example:

    $date = "{0:M-d-yyyy}" -f (Get-Date)

    10-4-2016

    With padded day and month:

    $date = "{0:MM-dd-yyyy}" -f (Get-Date)

    10-04-2016

    I typically use this mechanism to output a time-stamped file so I know when the file was created and on which computer. Note that grabbing seconds will most likely result in a unique filename unless the process producing this output can run in under 1000 ms.

    Out-File -FilePath ('c:\temp\{0}-{1:yyyyMMddHHmm}-a_meaningful_name.txt' -f $env:COMPUTERNAME,(Get-Date))

    -Eric

  • I don't see the "-notype" as an option for ContertTo-Html, and can't get it to work as suggested in another comment. But it would be nice to be rid of the tacked-on columns! Does anyone know of a way?

  • Took me a while to dig this one up use: -ExcludeProperty

    Notice I tucked it in prior to the convert to html, works well for my reports using html style output.

    | Select * -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray | ConvertTo-HTML -Head $Header | Out-File

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply