Powershell caps lock state change 引起名稱 密碼錯誤


http://wp.me/ph3BR-1ig

有人回報說, 登入 VPN 的連線常常不成功, 看了一下, 原來 VPN 的登入帳號密碼是必須區分大小寫的, 在 script  裡面已經輸入成為大小寫混合的文字, 但是執行過程裡面, 顯示卻是全部大寫字母, 難怪登入名稱密碼都報錯.

源碼的片段,


#IP address or host name of cisco vpn
[string]$CiscoVPNHost = "1.2.3.4"
[string]$Login = "userNAME"
[string]$Password = "password_1234ABCD"

....

#Write login and password
[System.Windows.Forms.SendKeys]::SendWait(“$Login{Enter}”)
[System.Windows.Forms.SendKeys]::SendWait(“$Password{Enter}”)

. .

明明要求大小寫, 顯示卻是全部大寫字母輸入, 問題在哪裡呢 !? 原來某些人喜歡把鍵盤設定成輸入全為大寫字母, 意思就是按一下鍵盤上面 [Caps lock] 的那個鍵, 鎖定在大寫輸入 (再按一次, 就回到小寫輸入, 老外稱為 toggle key). 另外, 如果發現大小寫的指示燈是壞的, 那麼使用者除了敲鍵盤鍵檢視以外, 是沒有辦法確認輸入大小寫的狀態. 這兩種情形, 都會讓鍵盤可在輸入英文大寫字母的狀態而使用者不以為意 (中文輸入狀態還沒研究有否其他影響), 但是 SendKeys 這個方法, 只是用程序自動輸入, 模擬有人在敲鍵盤輸入的動作, 所以, 一旦大寫輸入狀態設定, 就會全部變成大寫字母, 密碼和帳號當然被判斷為錯誤, 這是問題. 那解決方案呢 ?! 可要求使用者先檢查鍵盤狀態再啟動, 但保證有更多的問題和埋怨, 這只能是應變方法. 所以要用真正解決問題的方案, 找一下網路, 就抄回來試驗, 不過, 原作的 powershell script 只能在 Powershell 裡面執行, 想要點 BAT 讓她自動化, 還是需要自己改一下. 以下實驗, 目的是檢查鍵盤的大小寫輸入狀態, 有了這樣的基礎後, 再去修改 VPN 登入的 script, 應該就可以避免使用者預設鍵盤大小寫習慣的影響.

.

.

檔案名稱 hello.bat, 內容如下, 存檔後點兩下就會執行, 但是一定要存到 D:\hello.bat


@echo off

@echo Powershell script, run at command line level, use batch file to invoke
@echo Xiaolaba, 2014-07-17
@echo.
@echo 1) To display hello world message
@echo 2) To show keyboard status with caplock / scrolllock / numlock

powershell.exe "D:\hello.ps1"

@echo.
pause

.

.

檔案名稱 hello.ps1, 內容如下,

但是一定要存到 D:\hello.ps1 (檔俺名稱後綴是 P,S 和 數字1, 不是 L,  不是 i, 清楚了嗎)


# reference : http://technet.microsoft.com/en-us/library/bb963733.aspx

#modified, xiaolaba, 2014-07-17
# Filename: Hello.ps1
Write-Host
Write-Host 'Hello Xiaolaba, keyboard status key checking !'
Write-Host "Good-bye Xiaolaba ! `n"
# end of hello script

#2014-07-17, xiaolaba, debug purpose, time stamp display
#REF: http://technet.microsoft.com/en-us/library/ff730960.aspx
#REF: http://technet.microsoft.com/en-us/library/ee692801.aspx
$a = Get-Date -Format "yyyy-MM-d HH:MM:s"
"Time is: " + $a

# 2014-07-17, xiaolaba, include this section,
# ensure this script can be invoked at dosbox or double click to file of hello.bat

########################################################
#To check keyboard, caps lock, input small letter

Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop

#Set foreground window function
#This function is called every time in [System.Windows.Forms.Control]
#with this, thsi script can be called under command line,
# for example, "powershell.exe hello.ps1"

Add-Type @'
using System;
using System.Runtime.InteropServices;
public class Win {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
'@ -ErrorAction Stop
########################################################

#2014-07-17, xiaolaba modified
#REF: http://gallery.technet.microsoft.com/scriptcenter/ON-OFF-Keyboad-Lock-keys-6ba9885c
#REF: http://newdelhipowershellusergroup.blogspot.in/2013/10/on-off-keyboard-lock-keys-capsscrollnum.html

#the script following is only able to run within powershell IDE, command line invoke will be error

# Creating a WScript.Shell onject
 $keyBoardObject = New-Object -ComObject WScript.Shell
 
# CAPSLOCK / SCROL LOCK /  Num Lock key Status  
# Getting the status of Keys using .Net Object , this result of the ISkeyLock is in Boolean
# Capsock
 [string]$capsLockKeySatus = [System.Windows.Forms.Control]::IsKeyLocked('CapsLock') 

#Numlock  
 [string]$numLockKeySatus = [System.Windows.Forms.Control]::IsKeyLocked('NumLock') 

#scrollLock
 [string]$ScrollLockKeySatus =  [System.Windows.Forms.Control]::IsKeyLocked('Scroll')
 
#---- process starts form here  
# checking caps lock  
 if ( $capsLockKeySatus -eq $false )  
    {
        Write-Host "Capslock key is off"  
         
        # if you want to ON the CapsLock {IF CAPSLOCK IS ALREADY OFF }, then please unComment the below Command.
        #  $keyBoardObject.SendKeys("{CAPSLOCK}")
    }  
 
else {          
        Write-Host "Capslock key is On"  
         
        # if you want to OFF the CapsLock Key {IF CAPSLOCK IS ALREADY on }, then please unComment the below Command.
        #-->   $keyBoardObject.SendKeys("{CAPSLOCK}")
         
    }
 
# Checking ScrollOck
 
 if ( $ScrollLockKeySatus -eq $false )  
    {
        Write-Host "Scroll Lock key is off"  
         
        # if you want to ON the ScrollLoc {IF ScrollLock IS ALREADY OFF }, then please unComment the below Command.
        #$keyBoardObject.SendKeys("{SCROLLLOCK}")
    }  
 
else {          
        Write-Host "Scroll Lock key is On"  
         
        # if you want to OFF the CapsLock Key {IF CAPSLOCK IS ALREADY on }, then please unComment the below Command.
        #-->  $keyBoardObject.SendKeys("{SCROLLLOCK}")
         
    }
 
 
# Checking NumLock
 
 if ( $numLockKeySatus -eq $false )  
    {
        Write-Host "NumLock Lock key is off"  
         
        # if you want to ON the ScrollLoc {IF ScrollLock IS ALREADY OFF }, then please unComment the below Command.
        #$keyBoardObject.SendKeys("{NUMLOCK}")
    }  
 
else {          
        Write-Host "NumLock Lock key is On"  
         
        # if you want to OFF the CapsLock Key {IF CAPSLOCK IS ALREADY on }, then please unComment the below Command.
        #-->   $keyBoardObject.SendKeys("{NUMLOCK}")
         
    }
#-----------end of the script ----------------------#

.

.

執行的結果, 成功演示鍵盤3 個 toggle keys 的狀態.
powershell_caps_lock_check

END of this journey.

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s