[PowerShell][Proxy]用PowerShell設定Proxy – 模擬鍵盤

參考網址:
福別問的部落格 一鍵開啟proxy代理伺服器、顯示隱藏檔案副檔名、新增刪除輸入法

網路上大部分的教學是修改登錄檔, 但是實際上是行不通的, 修改登錄檔只能"表面上"把開啟代理伺服器的選項打勾, 並沒有真正地開啟代理伺服器。 這就是為什麼你可能按照網路上的方法寫了一個修改登錄檔的批次檔, 執行之後仍然不能夠從校外連線, 但是進到設定代理伺服器的頁面時, 卻又可以看到其代理伺服器已經被打勾啟動, 這時候又突然可以校外連線了。 (當你"檢視"啟動代理伺服器到底有沒有打勾時, 這時候windows後台又會做一些動作, 來"真正地"啟動代理伺服器。)

本腳本的原理就只是很簡單地模擬鍵盤的動作。
 

# 註解
# 開啟[網際網路 - 內容]
control inetcpl.cpl

# 如果是要開啟[資料夾選項],顯示隱藏檔案的副檔名,可以用RunDll32.exe shell32.dll,Options_RunDLL 7
# 如果是要開啟[地區及語言],新增或刪除輸入法,可以用control intl.cpl
# 更多進入控制台下的項目的指令,參考https://support.microsoft.com/en-us/help/192806/how-to-run-control-panel-tools-by-typing-a-command

# 讓系統取得title叫做[開啟網際網路 - 內容]的視窗
# 並讓該視窗變成當前視窗
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('開啟網際網路 - 內容')

#停頓1秒,避免delay造成下一步無法順利進行
Sleep 1

# 按下鍵盤上的各鍵
# ' '表示按下space鍵, +表示搭配Shift, %表示搭配Alt
# 各鍵的對應表參考https://msdn.microsoft.com/en-us/library/office/aa202943(v=office.10).aspx
# 注意,每個電腦的按鍵組合可能不相同,
# 請自行做相應的修改

$wshell.SendKeys('+{TAB}')

For ($i=1; $i -le 4; $i++)
{$wshell.SendKeys('{RIGHT}')}

$wshell.SendKeys('%l')

$wshell.SendKeys('{TAB}')
$wshell.SendKeys('{TAB}')

$wshell.SendKeys(' ')

$wshell.SendKeys('{ENTER}')
$wshell.SendKeys('{ENTER}')

 

發表於 Windows系統 | 發表迴響

[PowerShell]ps1 to exe Generator

參考網址:https://gallery.technet.microsoft.com/PS1-To-EXE-Generator-d39e8be2

發表於 Windows系統 | 發表迴響

[PowerShell]用PowerShell設定proxy

參考網址:https://github.com/majkinetor/posh/blob/master/MM_Network/Update-Proxy.ps1

# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 14-Aug-2015.

#requires -version 2.0

<#
.SYNOPSIS
    Get or set system proxy properties.
.DESCRIPTION
    This function implements unified method to set proxy system wide settings.
    It sets both WinINET ("Internet Options" proxy) and WinHTTP proxy.
    Without any arguments function will return the current proxy properties.
    To change a proxy property pass adequate argument to the function.
.EXAMPLE
    Update-Proxy -Server "myproxy.mydomain.com:8080" -Override "" -ShowGUI
    Set proxy server, clear overrides and show IE GUI.
.EXAMPLE
    Update-Proxy | Export-CSV proxy;  Import-CSV proxy | Update-Proxy -Verbose
    Save and restore proxy properties
.EXAMPLE
    $p = Update-Proxy; $p.Override += $p.Override += "*.domain.com" ; $p | proxy
    Add "*.domain.com" to the proxy override list
.NOTES
    The format of the parameters is the same as seen in Internet Options GUI.
    To bypass proxy for a local network specify keyword ";<local>" at the end
    of the Overide values.
    Setting the winhttp proxy requires administrative prvilegies.
.OUTPUTS
    [HashTable]
#>
function Update-Proxy() {
    [CmdletBinding()]
    param(
        # Proxy:Port
        [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string] $Server,
        # Semicollon delimited list of exlusions
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [string] $Override,
        # 0 to disable, anything else to enable proxy
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [string] $Enable,
        # Show Internet Options GUI
        [switch] $ShowGUI
    )
    $key  = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    $r = gp $key
    Write-Verbose "Reading proxy data from the registry"
    $proxy=@{
            Server   = if ($PSBoundParameters.Keys -contains 'Server')   {$Server}   else { $r.ProxyServer }
            Override = if ($PSBoundParameters.Keys -contains 'Override') {$Override} else { $r.ProxyOverride }
            Enable   = if ($PSBoundParameters.Keys -contains 'Enable')   {$Enable}   else { $r.ProxyEnable }
    }

    $set  = "Server","Override","Enable" | ? {$PSBoundParameters.Keys -contains $_ }
    if ($set) {
        #if (!(test-admin)) { throw "Setting proxy requires admin privileges" }

        Write-Verbose "Saving proxy data to registry"

        sp $key ProxyServer   $proxy.Server
        sp $key ProxyOverride $proxy.Override
        sp $key ProxyEnable   $proxy.Enable
        if (!(refresh-system)) { Write-Warning "Can not force system refresh after proxy change" }

        Write-Verbose "Importing winhttp proxy from IE settings"
        $OFS = "`n"
        [string]$res = netsh.exe winhttp import proxy source=ie
        if ($res -match 'Access is denied') {Write-Warning $res}
        else { Write-Verbose $res.Trim()}
    }

    New-Object PSCustomObject -Property $proxy
    if ($ShowGUI) { start control "inetcpl.cpl,,4" }
}

# The registry changes aren't seen until system is notified about it.
# Without this function you need to open Internet Settings window for changes to take effect. See http://goo.gl/OIQ4W4
function refresh-system() {
    $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

    $INTERNET_OPTION_SETTINGS_CHANGED   = 39
    $INTERNET_OPTION_REFRESH            = 37
    $type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
    $a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
    $b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
    return $a -and $b
}

Set-Alias proxy Update-Proxy

 

發表於 Windows系統 | 發表迴響

[powershell]Windows PowerShell如何執行

參考網址:平凡的幸福

PowerShell Scripts 檔案,附檔名為 .ps1。(數字的1)
PowerShell與一般直譯式語言一樣,直接以文字檔編輯,也不用編譯。

如果想要執行.ps1 的檔案,必須在PowerShell的介面下或直接以powershell指令執行
1、在PowerShell介面下
   在CMD底下或「開始」–>「執行」,輸入powershell,會出現下列的提示(前面會有PS的識別字)
   PS C:\>
   再將Script檔案以./執行,如
   PS C:\> ./test.ps1
   或是以完整路徑+Script檔案來執行,如
   PS C:\> d:\test.ps1
 
2、以powershell指令來執行
   在CMD底下或「開始」–>「執行」,輸入powershell + script檔案,例如:
   C:\> powershell "D:\test.ps1"  或是 C:\> powershell -command "D:\test.ps1"
   註:如果資料夾名稱有「空白字元」,則必須以「單引號」來區別。例如:D:\'test ps'\test01.ps1
 
如何查詢所有指令:
PS C:\> get-command

如何查詢指令用法,例如要查詢get-process:
PS C:\> get-help get-process  或是 PS C:\> help get-process 或是 PS C:\> get-process -?
(說明的文件是中文的,而且也有範例可以查詢)

線上查詢所有的 Windows PowerShell 指令和說明文件,可使用「*」萬用字元或是「?」單一字元。例如:
get-help *:列出所有的主題,包括指令和概念。
get-help * | more:列出所有的主題,包括指令和概念,more表示當資料已顯示整個視窗就暫停。
get-help about*:列出所有的概念主題。
get-help about_???:列出所有about_後面最多三個字元的概念主題,如about_do、about_for
get-help get*:列出所有 get 開頭的主題。
get-help {<指令名稱或主題名稱>}:列出指定的指令或主題的說明,例如 get-help dir  

教學網址:
http://www.pstips.net/
http://technet.microsoft.com/zh-tw/scriptcenter/dd742419.aspx

發表於 Windows系統 | 發表迴響

[powershell]PowerShell 更改執行原則,解決無法執行 ps1 指令稿問題

參考網址:office指南

問題

在執行 Windows PowerShell 的 .ps1 指令稿時,出現「系統上已停用指令碼執行」的錯誤訊息,該如何解決?

PowerShell 錯誤訊息
. : 因為這個系統上已停用指令碼執行,所以無法載入 C:\Users\USERNAME\Documents\Windows
PowerShell\profile.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為
https:/go.microsoft.com/fwlink/?LinkID=135170。
位於 線路:1 字元:3
+ . 'C:\Users\USERNAME\Documents\WindowsPowerShell\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

解法

若執行 .ps1 指令稿卻出現這樣的錯誤訊息,表示目前 PowerShell 中的執行原則(Excution Policy)可能被設定為 Restricted,不允許執行指令稿。

若想要讓 PowerShell 允許執行自己撰寫的 .ps1 指令稿,可依照以下步驟更改執行原則的設定。
STEP 1
在 PowerShell 啟動捷徑上按下滑鼠右鍵,選擇以系統管理員身分執行 PowerShell。

以系統管理員身分執行 PowerShell

STEP 2
執行以下 PowerShell 指令,將執行原則設定為 RemoteSigned

Set-ExecutionPolicy RemoteSigned
變更執行原則

變更執行原則之後,就可以正常執行自己撰寫的 .ps1 指令稿了。

討論

如果想要查詢目前 PowerShell 的執行原則設定值,可以執行以下 PowerShell 指令:

Get-ExecutionPolicy
RemoteSigned

如果想要將 PowerShell 的執行原則回復成預設的 Restricted,可以執行:

Set-ExecutionPolicy Restricted
發表於 Windows系統 | 發表迴響

[proxy]PAC script教學

參考網址:A夢之IT可憐貓

https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file

Proxy Server 的分流程式( proxy.pac)

一、前言

如果你的對proxy server  log 檔做流量分析,結果排行榜的前幾名都是自己學校的網站,那網路的流量就有點浪費了;如果貴單位必須設上層的 Proxy Server ,那影響更大了,因為資料都由上層 Proxy Server 送來,即使瀏覽的是LOCAL 網站,快則由 local 的 proxy server 送來,慢則由上層 server 先到local 抓取,再送過來,等於是繞了一圈。

Squid 1.X 版時,有設定 local IP 和 local domain 的功能,2.X 版也有類似的設定,但一直試不出不抓網域內 網站的設定,所以、得靠分流程式;在一般較大的單位中,Proxy server 不只一台,這也得靠 分流程式的設定;總之,要發揮proxy server 的效能與有效控管,分流程式是最佳選擇。

二、環境設定

  1. 分流程式是由JavaScript Function 組成,預設的副檔名是 .pac ,一般都是用 proxy.pac 這個名字,它必需獨立,不能包含在 HTML 中,格式如下
    function FindProxyForURL(url, host)
    {
        ……..
        ……..
    }

  2. proxy.pac 要放在那裡呢? 你可以把它當成網頁,放在 web server 上,讓瀏覽器讀取。 例如我們把設定檔存成 proxy.pac,然後放在 一台執行 Apache 的 WEB SERVER (www.cses.tcc.edu.tw) 的首頁目錄,那麼我們在瀏覽器的設定就是:
       http://www.cses.tcc.edu.tw/proxy.pac
    也許你會產生疑問,proxy.pac 和 proxy server 不用在同一台電腦上嗎? 那誰又規定跑 squid 的 proxy server 也要執行 apache 。 ^_^
    那些 web server 支援呢? Netscape 、Apache、CERN、NCSA都可以,以Apache 為例,請檢查 httpd.conf 中是否有以下的設定,如果沒有就自己加上去吧! 
      Addtype application/x-ns-proxy-autoconfig .pac 

三、範例說明

一個一個function 來說明太麻煩了,就直接用例子來說明好了。

範例一:基本簡單型

function FindProxyForURL(url, host)

    {

        if (isPlainHostName(host) ||

            dnsDomainIs(host, ".cses.tcc.edu.tw"))

            return "DIRECT";

        else

            return "PROXY proxy.cses.tcc.edu.tw:3128; DIRECT";

    }

說明:以我學校為例,在Browser 的 URL 欄位打 www 就會連到  www.cses.tcc.edu.tw ,第一個 isPlainHostName(host) 就是處理這個部份,第二個部份 dnsDomainIs(host, ".cses.tcc.edu.tw") 就是處理 linux.cses.tcc.edu.tw 這種網址,這些網域都是 local 的網站,所以回應 DIRECT ,直接去抓就可以了。
如果不符以上情形,那就是透過 proxy.cses.tcc.edu.tw:3128 去抓資料,後面加上  DIRECT 是怕 PROXY 當了,也可以直接抓取。

註:也許會覺得奇怪,PROXY 當了,就讀不到這個proxy.pac 了,那設定有用嗎?  沒有人規定proxy.pac 要放在 proxy server 上,不同的 server 是可以的,只要 web server 支援。
 

範例二:多了 IP 的判斷

function FindProxyForURL(url, host)
    {
        if (isPlainHostName(host) ||
            dnsDomainIs(host, ".cses.tcc.edu.tw") ||
          isInNet(host, "163.17.208.0", "255.255.255.0"))
            return "DIRECT";
        else
            return "PROXY proxy.cses.tcc.edu.tw:3128; DIRECT";
    }

說明:這一個例子只是比上例多一個檢查 IP 的動作,只要是連往 163.17.208.x/24 也都直接存取。

範例三:LOCAL 、特殊網站、其他三種情形

function FindProxyForURL(url, host)
    {
        if (isPlainHostName(host) ||
            dnsDomainIs(host, ".cses.tcc.edu.tw") ||
          isInNet(host, "163.17.208.0", "255.255.255.0"))
        return "DIRECT";
        

        if (dnsDomainIs(host, ".edu.tw"))
      return "PROXY cache2.nchu.edu.tw:3128;" +  
                "PROXY cache1.nchu.edu.tw:3128";
        else
       return "PROXY proxy.cses.tcc.edu.tw:3128";
    }

說明:因為 CODERED 的關係,中興大學對 80 PORT 的流量做管制,必需經過 proxy server ,所以,所有的教育網站都經過 proxy server ,興大提供 cache1 cache2 兩台 server ,是 edu.tw 的網站都要透過特定的 巨server , 要注意的是兩台 PROXy SERVER 的寫法。

我的設定:

function FindProxyForURL(url, host)
    {

# 自己學校的網域、IP 、縣網、縣網 IP 還有電子公文
# 的專屬網站都直接存取,不經過 PROXY SERVER 

          if (isPlainHostName(host) ||
          dnsDomainIs(host, ".cses.tcc.edu.tw") ||
            dnsDomainIs(host, ".boe.tcc.edu.tw") ||
             localHostOrDomainIs(host, "eod.taichung.gov.tw") ||
          isInNet(host, "163.17.208.0", "255.255.255.0") ||
          isInNet(host, "163.17.40.0", "255.255.255.0"))
       return "DIRECT";
# 到 台中縣的中、小學都經過 PROXY,不然又繞到中興就太
# 慢了,也可以把上面的 boe 去掉,那就是直接存取,
# 但是我覺得,國中、小的頻寬都不高,還是經過 PROXY 
# 比較好。

         if (dnsDomainIs(host, ".tcc.edu.tw"))
                return "PROXY proxy.cses.tcc.edu.tw:3128";
# 經過上面的過濾, 其他的教育單位就經由中興大學了。
         if (dnsDomainIs(host, ".edu.tw"))
         return "PROXY cache2.nchu.edu.tw:3128;" + 
                "PROXY cache1.nchu.edu.tw:3128";
       else
# 如果都不是、那就由本校的 proxy 出去了。
             return "PROXY proxy.cses.tcc.edu.tw:3128";
     }

 

四、後記

小程式我會寫,但是JavaScript沒碰過,如果怕語法有錯,推薦一套編輯器 http://www.editplus.com/  管它什麼  HTML, ASP, PHP, Perl, VBScript, JavaScript, CSS files,  C/C++, Java, JSP, SQL, Pascal, Python 通通支援,只要下載它的 語法檔或控制檔,檔案都小的,不過它沒有 巨集的功能。

參考資料

發表於 未分類 | 發表迴響

[windows]Open Control Panel Applets Directly in Windows 10

參考網址:winaero

control.exe /name microsoft.mouse
control.exe /name microsoft.DateAndTime
@%SYStemRoot%\System32\timedate.cpl,-51

https://docs.microsoft.com/en-us/windows/win32/shell/controlpanel-canonical-names#control-panel-canonical-names

How to Create Shortcuts to Open Control Panel Items in Windows 10

發表於 Windows系統 | 發表迴響

Window系統命令行調用控制面板程序

參考網址:
Window系統命令行調用控制面板程序
https://www.itread01.com/content/1517999052.html

 

30 Run Commands Every Windows User Should Know
https://techviral.net/10-run-commands-every-windows-user-know/

 

小技巧!Windows 你不能不知的 20 個實用執行 命令指令
https://www.kocpc.com.tw/archives/194013

Executing Control Panel Items
https://docs.microsoft.com/en-us/windows/win32/shell/executing-control-panel-items

 

啟動 Windows 設定應用程式
https://docs.microsoft.com/zh-tw/windows/uwp/launch-resume/launch-settings-app

發表於 Windows系統 | 發表迴響

使用 netsh 命令手動設定 Proxy 伺服器

參考網址:microsoft

netsh winhttp set proxy 120.116.22.209:3128 bypass-list="59.127.219.48"
netsh winhttp reset proxy
netsh winhttp show proxy

CMD [以系統管理員身分執行]
 

[Windows Batch] 批次檔如何在執行時取得系統管理員權限
在欲執行的批次檔前面加上這段語法,就可以在執行時取得系統管理員權限(原理是產生一個要求執行權限的VBS來實現)

:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line

 

HamielKuo記事 Bat.執行檔.要求管理員權限
哈! 將要執行的command line 放到下段的【start】後面就好 ^^
 

@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin Shell
ECHO =============================

:init
setlocal DisableDelayedExpansion
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************

ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::

 

發表於 Windows系統 | 發表迴響

建立 Windows 10 App 的捷徑

參考網址:Heresy's Space

微軟的 Windows 10 目前有許多軟體,都已經是採用 UWP App 的形式(通用 Windows 平台 App、官網)在運作了。
像是 Facebook、Messenger、Line、Netflex 等等,基本上都是以需要從 Microsoft Store 來進行安裝的,某方面來說,也越來越接近 Google Play 的形式了。

但是老實說,個人覺得 Windows App 在使用上,還是有很多不方便的地方。

其中一個,就是除了「釘選到 [開始]」、「釘選到工作列」外,似乎很建立出捷徑來放在自己想要的地方?

 

同時,由於 Windows App 的安裝也都是由系統決定的,往往到最後程式裝在哪,都找不到…
(實際上是在「C:\Program Files\WindowsApps」這個使用者沒有權限直接存取的目錄)
(就算透過修改權限進去看也沒什麼用,因為執行方法不太一樣)

而到底要怎麼建立 Windows 10 App 的捷徑呢?在《How to Create a Shortcut for Any "Modern" Windows App》這篇文章,提供了一個方法。

操作步驟如下:

  1. 執行「explorer.exe shell:::{4234d49b-0245-4df3-b780-3893943456e1}」這個指令(Windows key + R, type in shell:AppsFolder 也可以)

     

     

    • 一個方法是開啟一個檔案總管(最簡單就是 Win + e),在位址列把上面那行貼進去,然後按 Enter。
    • 或者是開啟命令提示字元、或是 PowerShell 來執行。
  2. 執行成功的話,會跳出一個顯示「Applications」、介面不太完整的檔案總管,裡面會顯示系統中有安裝的 App 與應用程式

     
  3. 在想要建立捷徑的 App 上面按滑鼠右鍵,就會有「建立捷徑」的選項了~
    而由於這個資料夾無法被正確地寫入,所以他應該會詢問「是否要將捷徑改放在桌面上?」;選「是」後,在桌面上就會出現這個 App 的捷徑了~
     
  4. 之後有必要的話,也可以把這個捷徑放到自己想要的地方了。

有沒有更好的方法?不曉得,不過至少這是一個必要時的可行方案了。


附註:

  • 不知道為什麼,這邊顯示的 App 很多圖示都很怪。基本上都是白色加透明底,沒有選起來更本看不到。
  • 老實說,Heresy 還是不是很喜歡 Windows 10 的 App 系統…
    和內建的新注音相容性感覺始終不是很好,不但似乎沒有學習能力,還常常會罷工。

 

後記:Windows key + R, type in shell:AppsFolder 也可以

發表於 Windows系統 | 發表迴響