捐血一袋救人一命

2021年3月15日 星期一

PowerShell 的 Internet http(s)/Web 存取功能

$ie = New-Object -com InternetExplorer.Application$Downloader = New-Object -TypeName System.Net.WebClient[Net.HttpWebRequest]$WebRequest = [Net.WebRequest]::Create($Uri)Invoke-WebRequestInvoke-RestMethod / Invoke-Method透過 Selenium 操控瀏覽器,存取網站第一種,只能操作 Internet Explorer,而且經常會莫名發生例外狀況,而無法使用 getElement(s)等相關方法;此時,建議改用IHTMLDocument3_getElement(s)等相關方法第二種,通常用在下載檔案,當你有確定的檔案網址時,就使用它來下載檔案/圖片/影片第三種讀取網站會是字串型態第四種、第五種則是 HtmlWebResponse物件,通常用在存取網頁或呼叫API,如果遠端網站的資料是透過 Javascript AJAX 取得,那很可能就會抓不到資料至於第五種,需要額外安裝Selenium WebDriver,不同瀏覽器需要安裝不同的版本;此外,當瀏覽器版本升級時,WebD...

2021年3月2日 星期二

使用 Powershell 設定電腦環境

因為經常需要設定電腦,所以寫了個程式來設定電腦的環境,免得經常要手動設定,還容易忘記漏設定。 Param ([switch]$Verbose)$VerboseStatus = $VerbosePreferenceIf($Verbose){    $VerbosePreference = "Continue"}If(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){    $ScriptFullPath = $MyInvocation.MyCommand.Definition $arguments = "& '" + $ScriptFullPath + "'"    # 另外以管理者身分執行 PowerShell,並載入 Script 來執行 Start-Process powershell -Verb runAs -ArgumentList $arguments ...

使用 PowerShell 清除暫存檔,並清除垃圾桶

先設定一個陣列 $tempfolders ,用來存放所有暫存檔路徑然後使用 Remove-Item 指令來強制刪除檔案(force),包含子目錄(recurse)因為有些暫存檔,有可能正在使用,是無法刪除的,會造成指令發出錯誤訊息,所以最後加上參數 -ErrorAction SilentlyContinue,當發生錯誤時,安靜地繼續執行指令最後,清除垃圾桶,也是一樣的$tempfolders = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*")Remove-Item $tempfolders -force -recurse -ErrorAction SilentlyContinueClear-RecycleBin -Force -ErrorAction SilentlyContinue如果要清除特別命名方式的檔案,或是不確定檔案位置的話,歡迎留...

使用 PowerShell 停用不安全的 SSL 2.0 3.0 & TLS 1.0 1.1 通訊協定

$Protocols = @("SSL 2.0", "SSL 3.0", "TLS 1.0", "TLS 1.1")$EndPoints = @("Client", "Server")Write-Verbose "停用 SSL 2.0 & 3.0 以及 TLS 1.0 & 1.1"$Protocols | ForEach{    $Protocol = $_    If(!(Test-Path -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\Protocols\$Protocol")){        New-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\Protocols\$Protocol" | Out-Null    }    $EndPoints | ForEach{        $EndPoint...