Difference between revisions of "Variables (PowerShell)"

Jump to navigation Jump to search
Page deperciated
(Page deperciated)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Depreciated|category=Variables_(PowerShell)}}
  __TOC__
  __TOC__


Line 307: Line 309:
== Arrays ==
== Arrays ==
<source lang="powershell">
<source lang="powershell">
$array = @()           # Create blank array
$array = @()                 # Create blank array
$array += 34           # Add value to end of array
$array = @("one", "two", 3)  # Create an array with some values
$array += 34                 # Add value to end of array
</source>
</source>


Line 392: Line 395:
</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 406:
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 442:
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 452:
{
{
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
     [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($sincepoch))
}
</source>
=== IP Address ===
The object structure: http://msdn.microsoft.com/en-us/library/system.net.ipaddress
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($IP2test) ) {
    Write-Host "$IP2test is a valid address"
}
}
</source>
</source>

Navigation menu