0

I have a very odd thing happening in Powershell.

Here is the code:

add-pssnapin windows.serverbackup
$wbs = Get-Wbsummary
$lastbackuptime = $wbs.lastBackupTime
$solution = "Windows Server Backup"

$surl = "https://REDACTED:1338/REDACTED.asp?querysolution=$solution&querylastbackuptime=$lastbackuptime"
write-host $surl
write-host $lastbackuptime

Here is what is output when I run this

https://REDACTED:1338/REDACTED.asp?querysolution=Windows Server Backup&querylastbackuptime=05/07/2013 05:04:12
07/05/2013 05:04:12

Why is powershell swapping the date around when made as part of another variable but not when I output the variable on its own?!

2 Answers 2

3

This is a special case with casting a datetime object. When you simply print the date as a string using write-host, that will be equal to running $lastbackuptime.toString(). This method uses the culture of you're computer. In my case, the culture in Region settings for Windows is Norway, so I get the "european" dateformat: dd/mm/yyyy.

However, when you include $lastbackuptime inside a string, it performs a cast to a string-object. In PowerShell(or .Net) it was decided that when casting a datetime-object, it should use a standard format to convert it to string so that the code would run the same no matter what culture the computer was configured with. That's why casting gives you the US format, while toString() and Write-Host gives the "european" format

Ex:

[16:07:43] PS-ADMIN C:\> $d.tostring()
07.05.2013 16:04:17
[16:13:05] PS-ADMIN C:\> write-host $d
07.05.2013 16:04:17
[16:13:12] PS-ADMIN C:\> [string]$d
05/07/2013 16:04:17

To specify the format your datetime should be displayed, you can do something like this:

[16:14:28] PS-ADMIN C:\> $d.tostring("dd/MM/yyyy")
07.05.2013
[16:14:34] PS-ADMIN C:\> "examplestring $($d.tostring("dd/MM/yyyy"))"
examplestring 07.05.2013

Read more here

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and this seems to be the problem but it is converting my may into an april. It seems to have done it with yours too if I do $lastbackuptimestring = $lastbackuptime.tostring("dd/mm/yyyy hh:mm:ss") that then makes the variable 07/04/2013 05:04:12 when it should be 07/05/2013 05:04:12
OK I fixed this by doing the following: $ukCulture = [Globalization.cultureinfo]::GetCultureInfo("en-GB") $lastbackuptime = [datetime]::Parse($lastbackuptime, $ukculture)
the reason it returned april is because I used mm (minutes) instead of MM (month). I fixed that a few mins after I posted the answer. I just forgot to fix the output.
0

Check your regional settings, specifically the Short Date & Long Date formats.

On my system, Short Date is MM/dd/yyyy and and Long Date is dddd, MMMM dd,yyyy. Then, running a simplified version of your example:

>$lastbackuptime = get-date;
>$lastbackuptime

Tuesday, May 07, 2013 10:07:42


>$url="http://whatever/redacted.asp?time=$lastbackuptime";
>$url
http://whatever/redacted.asp?time=05/07/2013 10:07:42

When used on its own, the Long Date format is used in returning the date, but when concatenated with (or expanded inside) another string the Short Date format is used.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.