Skip to content

Instantly share code, notes, and snippets.

@wgross
Last active August 24, 2017 11:45
Show Gist options
  • Save wgross/c298295f5477b88b340a to your computer and use it in GitHub Desktop.
Save wgross/c298295f5477b88b340a to your computer and use it in GitHub Desktop.
Get current weather from wttr.in in powershell. These scripts are based in the post at http://my-devnull.de/tag/wttr-in/.
# To enable ANSI sequences in a PowerShell console run the following commands.
# After that you can use wttr.in in you PowerShell just lake that:
# (curl http://wttr.in/ -UserAgent "curl" ).Content
#
# More on it:
# http://stknohg.hatenablog.jp/entry/2016/02/22/195644 (jp)
#
Add-Type -Namespace Win32 -Name NativeMethods -ErrorAction SilentlyContinue -MemberDefinition @"
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr handle, out int mode);
"@
$Handle = [Win32.NativeMethods]::GetStdHandle(-11) # stdout
$Mode = 0
$Result = [Win32.NativeMethods]::GetConsoleMode($Handle, [ref]$Mode)
$Mode = $Mode -bor 4 # undocumented flag to enable ansi/vt100
$Result = [Win32.NativeMethods]::SetConsoleMode($Handle, $Mode)
chcp 437
param(
[Parameter(Position=0,ValueFromPipelineByPropertyName,ValueFromPipeline)]
[string]$City
)
process {
Enable-Ansii | Out-Null
Invoke-WebRequest -UserAgent "curl" -Uri "http://wttr.in/$City"| Select-Object -ExpandProperty Content
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment