# here is the list of download URLs - get it and # convert the JSON format $url = 'http://www.mediathekdirekt.de/good.json' $web = Invoke-WebRequest-Uri$url-UseBasicParsing $videos = $web.Content | ConvertFrom-Json
# get all videos, create a nice title to display, # and attach the original data to each entry $videos | ForEach-Object { $title = '{0} - {1}'-f$_[2], $_[5] $title | Add-Member-MemberType NoteProperty -NameData-Value$_-PassThru } | Sort-Object | Out-GridView-Title'Video'-OutputMode Multiple | ForEach-Object { # get the actual download info from the selected videos # and do the download $url = $_.Data[6] $filename = Split-Path-Path$url-Leaf # videos are saved into your TEMP folder unless you # specify a different folder below $filepath = Join-Path-Path$env:temp-ChildPath$filename Invoke-WebRequest-Uri$url-OutFile$filepath-UseBasicParsing Invoke-Item-Path$filepath }
Whenever PowerShell records an error, it wraps it in an Error Record object. Here is a function that takes such an error record and extracts the useful information: 当 PowerShell 记录一个错误时,它将错误信息包装在一个 Error Record 对象中。以下是一个处理这种错误记录并解析有用信息的函数:
$hostname = 'powershellmagazine.com' # run the console-based application ASYNCHRONOUSLY in its own # window (PowerShell continues) and return the # process object (-PassThru) # Hide the new window (you can also show it if you want) $process = Start-Process-FilePath ping -ArgumentList"$hostname -n 4 -w 2000"-WindowStyleHidden-PassThru
# wait for the process to complete, and meanwhile # display some dots to indicate progress: do { Write-Host'.'-NoNewline Start-Sleep-Milliseconds300 } until ($process.HasExited) Write-Host
# the Error Level information is then found in ExitCode: $IsOnline = $process.ExitCode -eq0 $IsOnline
$hostname = 'powershellmagazine.com' # run the console-based application synchronously in the PowerShell window, # and return the process object (-PassThru) $process = Start-Process-FilePath ping -ArgumentList"$hostname -n 2 -w 2000"-Wait-NoNewWindow-PassThru
# the Error Level information is then found in ExitCode: $IsOnline = $process.ExitCode -eq0 $IsOnline
$hostname = 'powershellmagazine.com' # run console-based executable directly # and disregard text results $null = ping.exe $hostname-n2-w2000 # instead look at the exit code delivered in # $LASTEXITCODE. Ping.exe returns 0 if a # response was received: $IsOnline = $LASTEXITCODE-eq0 $IsOnline