PowerShell 現在處於什麼地位?有沒有必要學?

理由是什麼呢?


我們之前在Windows Server裡面做的一個AD管理工具,雖然是個GUI的程序,但底下跑了個powershell engine然後所有操作都是通過AD的cmdlet來執行的。

我們現在內部的工程系統,包括開發測試集成部署,也都建築在powershell上的。

可見powershell已經不僅僅是個命令行架構,更可以成為一種廣泛通用的開發手段。

有時候某個組做一個東西給別人用,發布一個sdk庫還不如發布一個powershell包別人很容易接受。


PS 現在還很欠挖掘,比如類似 zsh 中 $RPROMPT 的功能是可以實現的,但就是沒人做……($RPROMPT 的事情現在 PSReadline 那邊在挖坑,這個庫現在是 WMF5 的官方模塊,你有興趣可以去關注)

源碼:

# Load posh-git module from current directory
Import-Module posh-git

# Get full name of user
$username = [Environment]::UserName
$hostname = $env:COMPUTERNAME

# Am I an admin?
$wid = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp = new-object System.Security.Principal.WindowsPrincipal($wid)
$adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin = $prp.IsInRole($adm)

# Function to write git repository status on prompt
function writegitprompt($status){
if ($status) {
write-host " (" -nonewline
write-host ($status.Branch) -nonewline
write-host " " -nonewline
if($status.HasWorking) {
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true="Green";$false="DarkGray"}[$status.Working.Added -and $status.Working.Added.Count -ge 0]
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true=(@{$true="Red";$false="Yellow"}[$status.Working.Unmerged -and $status.Working.Unmerged.Count -ge 0]);$false=(@{$true="Red";$false="DarkGray"}[$status.Working.Unmerged -and $status.Working.Unmerged.Count -ge 0])}[$status.Working.Modified -and $status.Working.Modified.Count -ge 0]
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true="Red";$false="DarkGray"}[$status.Working.Deleted -and $status.Working.Deleted.Count -ge 0]
} else {
write-host "$([char]0x25CF)$([char]0x25CF)$([char]0x25CF)" -nonewline -ForegroundColor DarkGray
}
if($status.HasIndex) {
write-host "|" -nonewline
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true="Green";$false="DarkGray"}[$status.Index.Added -and $status.Index.Added.Count -ge 0]
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true=(@{$true="Red";$false="Yellow"}[$status.Index.Unmerged -and $status.Index.Unmerged.Count -ge 0]);false=(@{$true="Red";$false="DarkGray"}[$status.Working.Unmerged -and $status.Working.Unmerged.Count -ge 0])}[$status.Index.Modified -and $status.Index.Modified.Count -ge 0]
write-host "$([char]0x25CF)" -nonewline -ForegroundColor @{$true="Red";$false="DarkGray"}[$status.Index.Deleted -and $status.Index.Deleted.Count -ge 0]
}
write-host ")" -nonewline
}
}

# Here we go
function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE
if($IsAdmin){
write-host $username -nonewline -ForegroundColor Red
} else {
write-host $username -nonewline -ForegroundColor Yellow
}
write-host " $([char]0x3C9) " -nonewline -ForegroundColor Gray
Write-Host($pwd.ProviderPath) -nonewline -ForegroundColor Green

writegitprompt (Get-GitStatus)

# Rightmost time display
# Save cursor position first
$saveY = [console]::CursorTop
$saveX = [console]::CursorLeft
$columns = (Get-Host).UI.RawUI.windowsize.width # Column quantity of console window
[console]::SetCursorPosition($columns - 8, $saveY)
write-host "[" -nonewline
write-host (Get-Date -format "HH:mm") -nonewline -ForegroundColor Cyan
write-host "]" -nonewline
[console]::setcursorposition($saveX,$saveY) # Move cursor back

$global:LASTEXITCODE = $realLASTEXITCODE
return " $([char]0x3BB) "
}


什麼地位?

處於一個被低估的位置。之後影響力會越來越大,因為實在太強大了。

要不要學?

看你的需求,如果

  1. 你要長期用Windows Server伺服器
  2. 經常處理Office的一些重複性工作

  3. 從事或學習Windows環境的網路安全

建議學習,PowerShell和.net平台,office的無縫結合非常強大,而且因為能bypass各種防護措施,之後肯定是Windows平台滲透神器。

轉載一段代碼,獲取當前最占內存的十個進程,並在Excel生成餅圖

# create new excel instance
$objExcel = New-Object -comobject Excel.Application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)

# write information to the excel file
$i = 0
$first10 = (ps | sort ws -Descending | select -first 10)
$first10 | foreach -Process {$i++; $objWorksheet.Cells.Item($i,1) = $_.name; $objWorksheet.Cells.Item($i,2) = $_.ws}
$otherMem = (ps | measure ws -s).Sum - ($first10 | measure ws -s).Sum
$objWorksheet.Cells.Item(11,1) = "Others"; $objWorksheet.Cells.Item(11,2) = $otherMem

# draw the pie chart
$objCharts = $objWorksheet.ChartObjects()
$objChart = $objCharts.Add(0, 0, 500, 300)
$objChart.Chart.SetSourceData($objWorksheet.range("A1:B11"), 2)
$objChart.Chart.ChartType = 70
$objChart.Chart.ApplyDataLabels(5)

關於PowerShell和安全的一些腳本:

hack/scripts/powershell at master · pwnaus/hack · GitHub

關於PowerShell和安全的文章:

使用powershell Client進行有效釣魚 PowerShell與Office宏結合系列

WMI Attacks | WooYun知識庫 WMI系列

powershell各種反彈姿勢以及取證(一) PowerShell製作反彈腳本系列


Powershell已經變成了Windows 平台上的主流腳本語言。

可以參考:powershell - 熱門問答


我覺得大家在學習powershell之前,應該先接受【微軟反正就是不改cmd了】的這個事實,不要總是想為什麼非要打開powershell而不好好做cmd。在powershell出來之後,cmd的使命已經更加明確了——兼容那些開發商已經死掉但是你們卻無法拋棄的那一小撮msdos程序。


之前一直在用win7加cygwin以及Linux,最近換上win8之後嘗試了一下powerShell,泥馬被震驚了有木有。簡直神器啊。


個人認為,通過powershell控制windows才是正確使用微軟產品的正確方式。

圖形化操作系統讓操作簡單易行,然而卻讓很多人,包括程序猿放棄了其底層的基本結構。

除卻專業的程序猿,日常辦公也可以用它來代替vba和vbs。一切需要重複性勞動的工作,比如統計。每月必然需要把這個表的東西複製到那個表,然後統計好了再新做個表,畫幾個餅圖之類的提交下道工序。然而整套流程基本都是機械化的重複勞動。

目前的解決方式一般是erp和oa協同工作。erp處理數據,oa處理流程。如果把這兩個有機的結合起來,融合成工作流的機制,很多崗位其實是可以精簡的,可以提高效率的。

自動化辦公喊了這麼多年,然並卵,說有長進相對於其他行業來說真的是太緩慢了。手機從功能機都變大屏智能機,iphone都出到6S了,然而辦公自動化產品依舊是那副樣子。

powershell如果可以更精簡,能夠讓非計算機專業人士也可以隨心所欲的使用。那麼對於全社會的生產力提升都是很有幫助的。

畢竟就算linux玩兒的再好,自己單位的財務也不用它發工資。


Powershell是除了黑歷史API以外第一個讓我感到我在Windows上竟然如此無知的東西_(:3」∠)_


Windows上運行強大無比,無所不能。多寫一些腳本配合日常工作,幸福感上升一個層次。


而且 最近PowerShell的主要設計者兼架構師Jeffrey Snover被提升為Technical Fellow. Jeffrey Snover Promoted to Microsoft Technical Fellow -- Redmondmag.com


之前都是用shell,前一段時間,發現win10非常好用,然後要發掘一下win10的功能,結果驚呆了。powershell這樣的大殺器都有。跟windows組件的結合,真正的讓辦公效率提高很多。最最關鍵的,尼瑪他支持面向對象,面向對象呀,一個命令行工具支持面向對象,這簡直能趕上更大粒度的python了。


主要是管道和重定向太坑爹,否則我就用了。。。


要是以後你司的伺服器用powershell, 還是有必要學一下的


處於一個"受眾僅為Windows Server管理員"的境況。總得來說不如bash易用,當然,這是個人意見。如果你是Windows Server管理員,想要進階的話,幾乎是必學的。


但我怎麼覺得每個版本的的用法都不同?


Powershell在Windows平台所有產品里已經越來越重要了(不僅僅伺服器管理),比如Exchange2013,Lync2013幾乎都可以全部PowerShell命令行管理了,而且在批量處理的情況下尤其方便,例如批量增加1000個郵箱,調整它們的大小...


簡單補充一下,如果是想在Azure上從事雲的Dev or Opps 的工作,有機會的話最好學習一下powershell,Azure的很多fabricdeploy的工具都和powershell很類似


相當於Linux裡面的shell,不管你喜歡也好,不喜歡也好,只要你做Windows開發,基本上很難避開它。如樓上答案所說,微軟是不打算改cmd了,所以PowerShell就是唯一可用的命令行了


推薦閱讀:

Windows 的文件夾的許可權管理為什麼亂七八糟的?
為什麼 Windows 下的快捷鍵邏輯如此混亂?
使用 Mac 的人會鄙視 PC 用戶嗎,為什麼?
為什麼到現在還說 Windows 的穩定性不好?
微軟為什麼將 Windows 回收站設計為清空後無法找回?有改進方式么?

TAG:MicrosoftWindows | 編程 | 伺服器 | Shell編程開發 | PowerShell |