顯示具有 Windows 標籤的文章。 顯示所有文章
顯示具有 Windows 標籤的文章。 顯示所有文章

2011年9月12日 星期一

「sudo」 for Windows PowerShell

相信有許多 Unix-like 的愛用者,
對於 sudo 帶來的便利性及安全性,
都有很高的依賴性及倚靠性,
在 Windows 的世界,雖然有個「以系統管理員身分執行」的玩意兒,
但是在習慣使用文字介面如 cmd, vim, PowerShell 的人們來說,
倘若遇到需要系統管理員權限的設定檔或文字檔,
往往就是一句「XXX」之後,還要到開始功能表尋找到 cmd 或 PowerShell,
然後再用滑鼠點右鍵選擇「以系統管理員身分執行」,然後再 cd 到剛剛的資料夾,再用編輯器開啟、編輯、blah blah....

一來一往,往往費神又費時,
今天介紹在 PowerShell 使用時,
可以輸入 Unix-like 中的 sudo (或使用者自己取)
後面再帶 vim 或 notepad 指令,接著被修改的設定檔或文字檔,
便可以系統管理員權限開啟該設定檔或文字檔,以便進行編修!

一、首先「以系統管理員身分執行」開啟 PowerShell,
預設沒錯的話,您會在您的家目錄,如:C:\Users\XXXXXXX

二、輸入指令以便新增 scripts 資料夾:

New-Item -ItemType directory $HOME\Documents\Scripts


三、使用編輯器如 vim 或 notepad 開啟 PowerShell 之 Profile 檔:
(預設為 WindowsPowerShell\Microsoft.PowerShell_profile.ps1)

notepad.exe $PROFILE

四、新增如下文字於該檔中:

New-PSDrive -name script -PSProvider FileSystem -Root $HOME\Documents\Scripts


五、存檔並離開。

六、重啟你的 PowerShell ,你將發現上面所新增的資料夾被 mount 起來,可以繼續進行:

cd script:


七、進入 script 槽後,用慣用的編輯器開啟新增 sudo.ps1 script檔:

notepad.exe sudo.ps1


八、加入 sudo script 如下:

param(
        [switch]$ps,            # Switch for running args as powershell script
        [string]$file,             # Script/Program to run
        [string]$arguments = $args # Arguments to program/script
     )

# Find our powershell full path
$powershell = (get-command powershell).definition

# Get current directory
$dir = get-location

#If we're running this as a elevated powershell script
if ($ps){

        # Script verification
        if([System.IO.File]::Exists("$(get-location)\$file")) {

                # Set the $script to full path of the ps script
                $script = (get-childitem $file).fullname
        }

        # Create a powershell process
        $psi = new-object System.Diagnostics.ProcessStartInfo $powershell

        $psi.WorkingDirectory = Get-Location

        # Combine the script and its arguments
        $sArgs = $script + " " + $arguments

        # Set the arguments to be the ps script and it's arguments
        $psi.Arguments = "-noexit -command set-location $dir; $sArgs"

        # Magic to run as elevated
        $psi.Verb = "runas";
}

# We're running something other than a powershells script
else {

        # File verification
        if([System.IO.File]::Exists("$(get-location)\$file")) {

                # Get full path
                $file = (get-childitem $file).fullname
        }

        # Same as above, create proccess/working directory/arguments/runas
        $psi = new-object System.Diagnostics.ProcessStartInfo $file
        $psi.Arguments = $arguments
        $psi.Verb = "runas"
}

# Start the process
[System.Diagnostics.Process]::Start($psi)


九、存檔並離開!

十、再次開啟 PowerShell 的 Profile 設定檔:

notepad.exe $PROFILE


十一、新增關聯指令:

New-Alias -Name sudo 'script:\sudo.ps1'

注意這裡的紅色字 sudo 可以改成任何你想要的字,只要不跟內建指令撞名即可!

十二、重啟 PowerShell,你可以使用:

sudo notepad C:\Windows\System32\drivers\etc\hosts



sudo vim $PROFILE

就會以系統管理員權限開啟,當然 Windows 的 UAC 會開啟並問你是或否,此時按「是」,
便會開啟具有系統管理員權限的視窗了!

Reference To: http://techha.us/2010/05/sudo-for-powershell/
                      http://www.ainotenshi.org/710/%E2%80%98sudo%E2%80%99-for-powershell-sorta

2011年8月22日 星期一

模擬 TOP 在 Windows 的 Powershell

Unix-like 系列大家耳熟能詳的 TOP command,
到了 Windows Powershell 存在嗎?

當然是不存在,不然幹嘛模擬XD
跟馬蓋先一樣,就 Powershell 現有的相關指令:Get-Process,來作延伸。

Get-Process,顧名思義,就是要 Get 目前的 Processes,
為了滿足從 Unix-like 過來的捧油們,也可以打 ps 來呼叫之!

直接來一下模擬 TOP Command:







while (1) { ps | sort -desc cpu | select -first 30; sleep -seconds 2; cls }


首先,while 就不用提了,就是迴圈,
然後裡頭呼叫了 ps 再透過 sort 指令依照 CPU 用量排序,所以這邊除了 CPU,也可以任君修改成需要的排序;再來透過 select 指令挑選頭 30 個 Processes,好比 Unix-like 的 head 用法;而後就是 sleep 指令,讓其 2 秒更新一次,最後再把螢幕清掉。

收工

2011年8月10日 星期三

Power shell 執行 ps1檔時 出現檔案無法載入,因為這個系統上已停用指令碼執行。如需詳細資訊,請參閱 "get-help about_signing"


Power shell 的console模式 執行 ps1檔時出現: 檔案無法載入,因為這個系統上已停用指令碼執行。如需詳細資訊,請參閱 "get-help about_signing"。
在PowerShell console模式下執行 Set-ExecutionPolicy RemoteSigned
如 :
PS C:\> Set-ExecutionPolicy RemoteSigned
PowerShell console模式下執行.ps1程式:
PS C:\Powertools> .\Test.ps1
Dos command下執行Powershell的ps1程式:
powershell c:\Powertools\Test.ps1

下載:微軟Windows命令行PowerShell 2.0


微軟Windows命令行工具PowerShell 2.0是否可以下載了呢?這讓很多開發和管理人員感到困惑。微軟日前在官方博客中證實,Windows客戶端和服務器各版本都已經可以下載使用 PowerShell 2.0。

PowerShell 2.0預設包含在Windows 7和Windows Server 2008 R2中, 舊版Windows其實在很久之前也可以下載使用PowerShell了,包括簡體中文和繁體中文。之所以造成一些混淆是因為微軟不再單 獨提供PowerShell 2.0,而是將其整合到了Windows管理架構(Management Framework)中

微軟工程師Jeffrey Snover表示:「這一混淆是我們造成的,因為它的重新發佈不再稱為PowerShell 2.0,而是Windows管理架構。除了PowerShell 2.0之外,Windows管理架構還包括另外兩個技術,名為Windows遠程管理WinRM 2.0和後台智能傳輸服務(BITS)4.0。」

下載Windows管理架構核心(WinRM 2.0和Windows PowerShell 2.0):
Windows Server 2008:
http://www.microsoft.com/downloads/details.aspx?FamilyId=863e7d01-fb1b-4d3e-b07d-766a0a2def0b
64位Windows Server 2008:
http://www.microsoft.com/downloads/details.aspx?FamilyId=d37e25cf-db05-4b23-a852-cdf865d81b82
Windows Server 2003:
http://www.microsoft.com/downloads/details.aspx?FamilyId=909bbcf1-bd78-4e03-8c83-69434717e551
Vista:
http://www.microsoft.com/downloads/details.aspx?FamilyId=f2fa1227-9a34-4e29-aa03-62f5c00e16f2
64位Vista:
http://www.microsoft.com/downloads/details.aspx?FamilyId=0f73efa2-f8d6-45f3-a8f8-5cdc205b119a
XP和Windows Embedded:
http://www.microsoft.com/downloads/details.aspx?FamilyId=60cb5b6c-6532-45e0-ab0f-a94ae9ababf5
http://news.mydrivers.com/1/164/164503.htm

2011年7月24日 星期日

如何修復造成 Windows XP 無法啟動的損毀登錄

請先參考微軟的解法:
如何修復造成 Windows XP 無法啟動的損毀登錄

當您嘗試啟動或重新啟動 Windows XP 電腦時,可能會收到下列其中一個錯誤訊息:
Windows XP 無法啟動,因為下列檔案遺失或損毀:\WINDOWS\SYSTEM32\CONFIG\SYSTEM
Windows XP 無法啟動,因為下列檔案遺失或損毀:\WINDOWS\SYSTEM32\CONFIG\SOFTWARE
停止:c0000218 {登錄檔案失敗} 登錄無法載入 Hive 檔案:\SystemRoot\System32\Config\SOFTWARE 或它的記錄檔或替代資料。
系統錯誤:Lsass.exe
當試圖更新密碼時,這個傳回狀態表示所提供的目前密碼數值不正確。

雖然可能以現在 Windows 7 的設計已經遇不太到這種狀況,
不過我公司的老爺車電腦還是遇到了,然而微軟的作法懶得教人用工具片開機,
所以落落長的一大串 DOS 指令,讓人很不舒服XD

以下簡要敘述核心作法:

以 WINDOWS\SYSTEM32\CONFIG\SYSTEM 損毀為例
整個做法就是取出舊的檔加配合系統還原來做處理.

微軟網站上所用的batch
***************************************************************
md tmp
copy c:\windows\system32\config\system c:\windows\tmp\system.bak
copy c:\windows\system32\config\software c:\windows\tmp\software.bak
copy c:\windows\system32\config\sam c:\windows\tmp\sam.bak
copy c:\windows\system32\config\security c:\windows\tmp\security.bak
copy c:\windows\system32\config\default c:\windows\tmp\default.bak

delete c:\windows\system32\config\system
delete c:\windows\system32\config\software
delete c:\windows\system32\config\sam
delete c:\windows\system32\config\security
delete c:\windows\system32\config\default

copy c:\windows\repair\system c:\windows\system32\config\system
copy c:\windows\repair\software c:\windows\system32\config\software
copy c:\windows\repair\sam c:\windows\system32\config\sam
copy c:\windows\repair\security c:\windows\system32\config\security
copy c:\windows\repair\default c:\windows\system32\config\default

***************************************************************

記得在c:底下操作.

主要就是要取出 system software, sam, security, default 這五個檔案。
這五個檔案也很好辨識。
你到 c:\windows\system32\config 底下找,沒有副檔名的那五個就是了。

以下為重點:
整個微軟的解法因為不想用工具片,所以一整個麻煩,需要多次重開機。
建議直接使用 Win PE 這類的工具片,或 XPE 這類的 Live 光碟

1: 先將 c:\windows\system32\config\裡舊的 system, software, sam, security, default 備份
2: 尋找 c 槽底下 System Volume Information 裡面的各個備份資料夾裡的 Snapshot 中的該5檔
3: 將舊的備份蓋掉 c:\windows\system32\config\ 裡的五個檔。(記得修改成相對映的檔名)

用 Win PE 這類的工具片來做會十分簡單,記得要開啟可以看到系統資料夾的選項。
System Volume Information 預設是隱藏的。

理論上只要有系統還原備份就可以順利處理掉這問題。

萬一沒有系統還原的時候怎麼辦?
以下為推論, 有部份我沒有實際做過

1: 先將 c:\windows\system32\config\裡舊的 system, software, sam, security, default 備份
2: 使用 c:\windows\repair 裡的五個檔取代 c:\windows\system32\config\ 裡的舊檔
3: 利用 Win PE 或其它工具片,除了 system 之外的四個檔再蓋回去

理論上它已經變成一個新的設定了,有可能其它四個檔會有不合的情形,
最少software這個檔是可以蓋的,可以省去重裝軟體的困擾。