Difference between revisions of "Variables (PowerShell)"

Jump to navigation Jump to search
→‎Datetime: Turned into subsection of "Object Types" (new heading) and added "IP Address"
(→‎Datetime: Turned into subsection of "Object Types" (new heading) and added "IP Address")
Line 392: Line 392:
</source>
</source>


== Datetime ==
== Object Types ==
=== Datetime ===
The object structure: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
The object structure: http://msdn.microsoft.com/en-us/library/system.datetime.aspx


=== Improper (US) Formatting ===
==== Improper (US) Formatting ====
Be aware that if you live somewhere dates are normally formatted properly (eg not the USA), then PowerShell (or probably the underlying DateTime object type as this sort of problem seems to rear its head at unexpected moments when working on Windows) has a nasty habit of returning a string formatted with day and month swapped around when coming from a script.
Be aware that if you live somewhere dates are normally formatted properly (eg not the USA), then PowerShell (or probably the underlying DateTime object type as this sort of problem seems to rear its head at unexpected moments when working on Windows) has a nasty habit of returning a string formatted with day and month swapped around when coming from a script.


Line 402: Line 403:
If your dates are getting mixed up, it may not be your mistake, and it may be that you've fallen fowl of the problem as well.
If your dates are getting mixed up, it may not be your mistake, and it may be that you've fallen fowl of the problem as well.


=== Formatting ===
==== Formatting ====
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the '''<code> -uFormat </code>''' option...
To control how a DateTime is displayed you can pass it through <code> Get-Date </code> with the '''<code> -uFormat </code>''' option...
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>
<source lang="powershell">Get-Date $datetime -uFormat "%R hrs, %a %d %b %Y"</source>
Line 438: Line 439:
For the full list of formatting options see http://technet.microsoft.com/en-us/library/ee692801.aspx, and even more detail at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx
For the full list of formatting options see http://technet.microsoft.com/en-us/library/ee692801.aspx, and even more detail at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx


=== Converters ===
==== Converters ====
<source lang="powershell">
<source lang="powershell">
function ConvertLocalToUnix([datetime]$datetime)
function ConvertLocalToUnix([datetime]$datetime)
Line 448: Line 449:
{
{
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
}
</source>
=== IP Address ===
To create an IP address object...
<source lang="powershell">
$ip = [System.Net.IPAddress]::Parse("192.168.1.123")
</source>
To confirm a supplied address is valid...
<source lang="powershell">
if ([System.Net.IPAddress]::TryParse($IP-to-test) {
    Write-Host "$IP-to-test is a valid address"
}
}
</source>
</source>

Navigation menu