2012年2月1日 星期三

Power Shell 線上教學網址

微軟不是不好!

而是微軟設法轉變出太多「花俏」 的方式,使得微軟?
一直在改變「新使用者、舊使用者」在各種運用上的習慣!

所以有人說?別跟微軟太親近!

http://social.technet.microsoft.com/Forums/zh-TW/powershellzhcht/thread/6b7d2758-f5c5-4590-9113-ca6f69c48472/
http://technet.microsoft.com/zh-tw/library/dd125460.aspx


-----------------------------------------------------------------------------------
範例 

http://zh.wikipedia.org/wiki/Windows_PowerShell#.E7.AF.84.E4.BE.8B

  • 停止所有目前執行中的以"p"字元開頭命名的程式:
PS> get-process p* | stop-process
  • 停止所有目前執行中的所有使用大於1000MB記憶體的程式:
PS> get-process | where { $_.WS -gt 1000MB } | stop-process
  • 計算一個目錄下檔案內的位元組大小:
PS> get-childitem | measure-object -property length -sum
  • 等待一個叫做"notepad"的程式執行結束:
PS> $processToWatch = get-process notepad
PS> $processToWatch.WaitForExit()
  • 將"hello, world!"字串轉為英文大寫字元,成為"HELLO, WORLD!":
PS> "hello, world!".ToUpper()
  • 在字串"string"的第1個字元後插入字串"ABC",成為"sABCtring":
PS> "string".Insert(1, "ABC")
  • 訂閱一個指定的RSS Feed並顯示它最近8個主題:
PS> $rssUrl = "http://blogs.msdn.com/powershell/rss.aspx"
PS> $blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
PS> $blog.rss.channel.item | select title -first 8
  • 把"$UserProfile"設定成數值"UserProfile"的環境變數:
PS> $UserProfile = $env:UserProfile
-----------------------------------------------------------------------------------


(下面的 Power Shell 若是 VBS 上寫一個同樣的功能?可以宣告一次,不用撰寫 10 行就能結束,還包含置入性文件!若在完整的 DOS Batch 環境下?至多 6 行,就已經足夠。)

以下轉載自網址  「http://blog.darkthread.net/post-2007-11-23-my-first-powershell-script.aspx(我不認識他,也未告知,就轉載了。)




###################################
#Check-File.ps1 by Jeffrey Lee (www.darkthread.net) #
###################################
$AppName = "Check-File.ps1"
$AppVer = "v1.0 [2007-11-23]"
$today = Get-Date -format "yyyyMMdd"
$file = "\\remote_server\share_folder\prefix" + $today + ".txt"
if (Test-Path $file)
{ Write-Host "Pass" -foreground "green" }
else
{
  Write-Host "Fail" -foreground "red"
  $smtp = New-Object System.Net.Mail.SmtpClient
  $smtp.Host = "relay.mail.darkthread.com"
  $smtp.Send("sender@darkthread.com", "recipant@darkthread.com", "File Check Warning", $file + " not found!!")
}
不 到20列寫完,簡單到出人意料。Get-Date取得今天日期,Test-Path檢查檔案是否存在,而搬出 System.Net.Mail.SmtpClient讓原本以為要大興土木的寄信動作三行打死。以後要遇到要寫Script或小型管理工具的場合,又多 了一件順手的新兵器,哈!!
底下是一些相關資源:

1.下載PowerShell 1.0 (PowerShell 2.0 CTP也出來了,只可惜不支援Windows 2003)
http://www.microsoft.com/downloads/details.aspx?FamilyId=10EE29AF-7C3A-4057-8367-C9C1DAB6E2BF&displaylang=en

2.PowerShell CheatSheet,撰寫PowerShell時用的小抄,不過內容略嫌簡單、涵蓋範圍不大,有點不夠用的感覺。

http://www.microsoft.com/downloads/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en


3.PowerShell HelpFile。CHM格式的說明文件,寫得很詳細也很淺顯易讀,是佛心級的好文件,其中VBScript與PowerScript的指令對照表更是功德無量!!!
http://www.microsoft.com/downloads/details.aspx?FamilyId=3B3F7CE4-43EA-4A21-90CC-966A7FC6C6E8&displaylang=en


另外,跑ps1 Script檔時,會先遇到此錯誤:
File C:\Program Files\Console2\check-file.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:15
+ ./check-file.ps1 <<<<

基於安全的理由,ps1 Script預設是不給跑的,記得用以下的指令調整安全設定:
Set-ExecutionPolicy RemoteSigned


Power Shell 呼叫其他「命令」

$a = "ipconfig.exe /all > c:\\ipinfo.txt"
Invoke-Expression $a
.

沒有留言:

張貼留言

歡迎討論

HR , NO!Human Resources,NO!

※※ 這樣內容,上手會困難嗎?※※ 我想到什麼?就寫什麼!※※

※對於資訊!我想到什麼?就寫什麼!

如果困難的話?
歡迎來信討論或發表意見,我會儘快回覆。

也歡迎來 YAHOO 知識家,集思廣益!

我的YAHOO 知識家 首頁:
Yahoo 知識+ 2013年改版前 (網域似乎已作廢)
YAHOO 知識家+ 2013年改版後

有需要技術文件 DarkMan 蒐集了不少!
存在FTP共享。想下載?請洽DarkMan信箱取得下載帳號。※

人性的理由
上句:不修一切法,如如是己身。傳其法,授其使,說其名,淪為其用。
下句:你寫得出來其意就傳你【大神威、大魔法,無上魔道。】