「PowerShell」修訂間的差異

出自ProgWiki
跳至導覽 跳至搜尋
→‎FAQ
(未顯示同一使用者於中間所作的 24 次修訂)
行 1: 行 1:
'''PowerShell'''
+
'''PowerShell'''{{wikipedia|PowerShell|lang=en}}
*{{wikipedia|PowerShell|lang=en}}
 
 
*{{wikiversity|PowerShell|lang=en}}
 
*{{wikiversity|PowerShell|lang=en}}
*[https://docs.microsoft.com/zh-tw/powershell/scripting/install/windows-powershell-system-requirements Windows PowerShell 系統需求]
+
*[https://github.com/PowerShell/PowerShell/releases Releases · PowerShell/PowerShell · GitHub]
*參照:
 
**[http://social.technet.microsoft.com/Forums/zh-TW/powershellzhcht/threads Windows PowerShell 論壇]
 
**[http://technet.microsoft.com/zh-tw/scriptcenter/default.aspx 指令碼中心]
 
  
;安裝
+
==技術文件==
*[http://support.microsoft.com/kb/968930/zh-tw KB968930 - Windows Management Framework Core package (Windows PowerShell 2.0 and WinRM 2.0)]
+
*[https://docs.microsoft.com/zh-tw/powershell/ PowerShell 文件 - PowerShell | Microsoft Docs]
*[http://support.microsoft.com/kb/968929/zh-tw KB968929 - Windows Management Framework (Windows PowerShell 2.0, WinRM 2.0, and BITS 4.0)]
 
  
;啟動方法
+
;其他
*【開始】→【執行】→【powershell】
+
*[https://ithelp.ithome.com.tw/users/20005121/ironman/54 強而有力的 Windows PowerShell :: 2009 iT 邦幫忙鐵人賽]
 
+
*[https://blog.darkthread.net/blog/ps-func-param-syntax/ PowerShell FAQ - 函式多參數寫法陷阱-黑暗執行緒]
==技術文件==
+
*[https://blog.darkthread.net/blog/ps-func-callback-param/ PowerShell FAQ - 傳入 Callback 函式參數-黑暗執行緒]
 +
*[https://blog.darkthread.net/blog/ps-dump-and-compare-dll-ver PowerShell 小工具 - 盤點及比較 DLL 版本差異-黑暗執行緒]
 +
*[https://blog.darkthread.net/blog/exe-output-stream-to-ps-pipeline/ 使用 PowerShell 串接 EXE 輸出串流結果-黑暗執行緒]
 +
*[http://www.pstips.net/ PowerShell 中文博客] - ''收集和分享PowerShell相关教程,技术和最新动态''
 
<!--
 
<!--
*[http://www.microsoft.com/downloads/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en Windows PowerShell Quick Reference] //-->
+
*[http://www.microsoft.com/downloads/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en Windows PowerShell Quick Reference]  
 
*[http://www.microsoft.com/taiwan/technet/columns/profwin/28-monad.mspx 易學易用的 Windows PowerShell]
 
*[http://www.microsoft.com/taiwan/technet/columns/profwin/28-monad.mspx 易學易用的 Windows PowerShell]
*[http://search.microsoft.com/results.aspx?mkt=zh-TW&setlang=zh-TW&q=Windows+PowerShell 搜尋微軟網站上的『Windows PowerShell』]
 
*[https://ithelp.ithome.com.tw/users/20005121/ironman/54 強而有力的 Windows PowerShell :: 2009 iT 邦幫忙鐵人賽]
 
<!--
 
 
;Technet 專欄
 
;Technet 專欄
 
#[http://www.microsoft.com/taiwan/technet/columns/profwin/56-Winpowerwshell.mspx Windows PowerShell 講座 (1)—指令、重導、別名]
 
#[http://www.microsoft.com/taiwan/technet/columns/profwin/56-Winpowerwshell.mspx Windows PowerShell 講座 (1)—指令、重導、別名]
行 28: 行 23:
 
#[http://www.microsoft.com/taiwan/technet/columns/profwin/67-Winpowerwshell5.mspx Windows PowerShell 講座 (5)—存資料的其他方式及編寫指令碼的前置準備]
 
#[http://www.microsoft.com/taiwan/technet/columns/profwin/67-Winpowerwshell5.mspx Windows PowerShell 講座 (5)—存資料的其他方式及編寫指令碼的前置準備]
 
//-->
 
//-->
==常用指令==
+
 
 +
==應用==
 +
*[https://docs.microsoft.com/zh-tw/powershell/scripting/samples/sample-scripts-for-administration 適用於系統管理的範例指令碼 - PowerShell | Microsoft Docs]
 +
*[https://www.powershellgallery.com/ PowerShell Gallery | Home]
 +
 
 +
;其他
 +
*[http://www.microsoft.com/downloads/details.aspx?FamilyID=50633A1F-A665-425D-923C-1A269F8AC084&displayLang=en Windows PowerShell 2.0 Software Development Kit (SDK)]
 +
*[http://www.codeplex.com/powershellremoting/ PowerShell Remoting](遠端操作相關)
 +
:*[http://forums.microsoft.com/Technet-CHT/ShowPost.aspx?PostID=3272993&SiteID=23 Technet討論區 - 請問 PowerShell 可否遠端登入]
 +
 
 +
*[http://www.microsoft.com/taiwan/technet/scriptcenter/resources/qanda/mar07/hey0326.mspx 如何使用 Windows PowerShell 以便在應用程式終止時收到通知?]
 +
*[http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct07/hey1031.mspx How Can I Use Windows PowerShell to Delete All the Files in a Folder Older Than 90 Days?]
 +
*[http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/11/23/my-first-powershell-script.aspx#3432 檢查檔案是否存在?如不存在則發email通知]
 +
*[http://gallery.technet.microsoft.com/scriptcenter/files-from-websites-4a181ff3 Download files from websites programatically via powershell]
 +
 
 +
*[http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C How to run PowerShell scripts from C#]
 +
 
 +
==FAQ==
 +
===安裝PowerShell===
 +
*[https://docs.microsoft.com/zh-tw/powershell/scripting/install/installing-powershell 在 Windows、Linux 和 macOS 上安裝 PowerShell - PowerShell | Microsoft Docs]
 +
===使用嚴格模式===
 +
*[https://docs.microsoft.com/zh-tw/powershell/module/microsoft.powershell.core/set-strictmode Set-StrictMode (Microsoft.PowerShell.Core) - PowerShell | Microsoft Docs]
 +
<source lang="powershell">
 +
# 設定嚴格模式 1.0
 +
Set-StrictMode -Version 1.0
 +
 
 +
# 設定嚴格模式 2.0
 +
Set-StrictMode -Version 2.0
 +
 
 +
# 設定嚴格模式 3.0
 +
Set-StrictMode -Version 3.0
 +
 
 +
# 設定嚴格模式 最後版本
 +
Set-StrictMode -Version Latest
 +
 
 +
# 關閉嚴格模式
 +
Set-StrictMode -Off
 +
</source>
 +
===常用指令===
 
*Get-Help
 
*Get-Help
 
:取得指令的使用說明
 
:取得指令的使用說明
行 39: 行 72:
 
*ps
 
*ps
 
:顯示執行中的程序狀態
 
:顯示執行中的程序狀態
 +
*Get-Module
 +
:顯示已安裝的模組
 +
*Get-Package
 +
:顯示已安裝的軟體套件
 +
*Find-Package -Name 套件名
 +
:尋找套件(需安裝Nuget)
 +
*Get-PackageSource
 +
:顯示能安裝的軟體套件的來源
  
==相關套件==
+
===軟體套件安裝===
*[http://www.microsoft.com/downloads/details.aspx?FamilyID=50633A1F-A665-425D-923C-1A269F8AC084&displayLang=en Windows PowerShell 2.0 Software Development Kit (SDK)]
+
====經由Chocolatey====
*[http://www.codeplex.com/powershellremoting/ PowerShell Remoting](遠端操作相關)
+
*[https://chocolatey.org/install Chocolatey Software | Installing Chocolatey]
:*[http://forums.microsoft.com/Technet-CHT/ShowPost.aspx?PostID=3272993&SiteID=23 Technet討論區 - 請問 PowerShell 可否遠端登入]
+
*執行[https://chocolatey.org/install.ps1 install.ps1]
 
+
$ Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
==應用==
+
*安裝軟體 (以notepadplusplus為例)
*[http://www.microsoft.com/taiwan/technet/scriptcenter/resources/qanda/mar07/hey0326.mspx 如何使用 Windows PowerShell 以便在應用程式終止時收到通知?]
+
$ choco install notepadplusplus
*[http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct07/hey1031.mspx How Can I Use Windows PowerShell to Delete All the Files in a Folder Older Than 90 Days?]
+
====從WindowsApps安裝應用軟體====
*[http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/11/23/my-first-powershell-script.aspx#3432 檢查檔案是否存在?如不存在則發email通知]
+
*以WindowsCalculator為例
*[http://gallery.technet.microsoft.com/scriptcenter/files-from-websites-4a181ff3 Download files from websites programatically via powershell]
+
$ManifestPath = (Get-AppxPackage -Name "*WindowsCalculator*").InstallLocation + "\Appxmanifest.xml"
===C#===
+
Add-AppxPackage -Path $ManifestPath -Register -DisableDevelopmentMode
*[http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C How to run PowerShell scripts from C#]
 
  
==FAQ==
 
 
===改善PowerShell的效能===
 
===改善PowerShell的效能===
 
*[https://blog.miniasp.com/post/2019/01/11/ProgressPreference-slow-down-powershell-execution 如何修正 PowerShell 執行龜速的元兇 (關閉進度列提示) ]
 
*[https://blog.miniasp.com/post/2019/01/11/ProgressPreference-slow-down-powershell-execution 如何修正 PowerShell 執行龜速的元兇 (關閉進度列提示) ]
行 99: 行 138:
 
[System.Diagnostics.Process]::Start("C:\Client\client.exe", "")
 
[System.Diagnostics.Process]::Start("C:\Client\client.exe", "")
 
</source>
 
</source>
===除超過30天的資料庫備份檔===
+
===刪除超過30天的資料庫備份檔===
 
*參考: [https://medium.com/@jamieleetw95/powershell-%E8%87%AA%E5%8B%95%E5%88%AA%E9%99%A4%E6%AA%94%E6%A1%88-%E6%8C%89%E7%85%A7%E6%AA%94%E6%A1%88%E4%BF%AE%E6%94%B9%E6%97%A5-%E4%BF%9D%E7%95%99%E8%BF%91n%E5%A4%A9%E5%85%A7%E7%9A%84%E6%AA%94%E6%A1%88-46eabad9f465 用PowerShell刪除N天前或指定日期(前後)建立(或修改)的檔案 | 程式前沿]
 
*參考: [https://medium.com/@jamieleetw95/powershell-%E8%87%AA%E5%8B%95%E5%88%AA%E9%99%A4%E6%AA%94%E6%A1%88-%E6%8C%89%E7%85%A7%E6%AA%94%E6%A1%88%E4%BF%AE%E6%94%B9%E6%97%A5-%E4%BF%9D%E7%95%99%E8%BF%91n%E5%A4%A9%E5%85%A7%E7%9A%84%E6%AA%94%E6%A1%88-46eabad9f465 用PowerShell刪除N天前或指定日期(前後)建立(或修改)的檔案 | 程式前沿]
 +
*檔案:ClearDbBackup.ps1
 
<source lang="powershell">
 
<source lang="powershell">
 +
# 清除超過30天的資料庫備份檔
 +
#
 +
# 用法:
 +
# 用管理員身分執行 PowerShell (怕資料夾或檔案鎖在管理員權限而無法刪除)
 +
# >D:
 +
# >CD D:\Buckup
 +
# >./ClearDbBackup.ps1
 +
 +
 +
#定義主目錄
 +
#透過Get-ChildItem抓該資料夾內的檔案列表
 +
$TargetFolder = "D:\Backup\DB"
 +
 +
#定義天數,要刪除多久以前的檔案
 +
#若是要刪除30天前的檔案,$Days就設定30
 +
$Days = 30
 +
 
#取今日時間
 
#取今日時間
 
$Currentlytime = (Get-Date).AddDays(-$Days)
 
$Currentlytime = (Get-Date).AddDays(-$Days)
行 114: 行 171:
 
if($item.LastWriteTime -le $Currentlytime)            #-le 小於或等於
 
if($item.LastWriteTime -le $Currentlytime)            #-le 小於或等於
 
{
 
{
        Remove-Item $item.FullName | out-null   
+
        Remove-Item $item.FullName | out-null   
        write-host "Finish Deleting Dir $item" -ForegroundColor "DarkRed"
+
        write-host "Finish Deleting Dir $item" -ForegroundColor "DarkRed"
    }     
+
}     
 
}
 
}
 
</source>
 
</source>
 +
===ASP.NET WebForm PostBack===
 +
*[https://blog.darkthread.net/blog/crawl-webform-postback-w-powershell/ ASP.NET WebForm PostBack 式查詢網頁之 PowerShell 爬蟲範例-黑暗執行緒]
 +
===JSON===
 +
*[https://stackoverflow.com/questions/13968004/how-do-i-use-json-net-to-parse-json-in-powershell How do I use Json.NET to parse json in PowerShell? - Stack Overflow]
 +
**[https://www.nuget.org/packages/Newtonsoft.Json/ NuGet Gallery | Newtonsoft.Json]
 +
===批次轉檔===
 +
*[https://blog.darkthread.net/blog/ps-batch-conv-xls-to-xlsx/ PowerShell 小工具 - XLS 轉 XLSX 批次轉換-黑暗執行緒]
 +
*[https://blog.darkthread.net/blog/ps-batch-conv-doc-to-docx/ PowerShell 小工具 - DOC 轉 DOCX 批次轉換-黑暗執行緒]
 +
===文字亂碼===
 +
*[https://blog.darkthread.net/blog/ps-pipeline-exe-encoding/ 【茶包射手日記】PowerShell 串接 EXE 輸出中文變亂碼-黑暗執行緒]
 +
===計算檔案的雜湊值===
 +
*[https://blog.darkthread.net/blog/compute-file-hash-in-win/ 計算檔案雜湊碼 - 使用 PowerShell 及 Windows 內建工具-黑暗執行緒]
 +
===圖檔處理===
 +
*[https://blog.darkthread.net/blog/id-card-watermark-ps-tool/ 為自己寫程式 - 身分證影本加浮水印-黑暗執行緒]
 +
 +
===用管理者身分執行的 PowerShell 腳本===
 +
*[https://blog.darkthread.net/blog/ps1-requireadministrator/ PowerShell 密技 - 自動改用管理者權限執行 .ps1-黑暗執行緒]
 +
===其他===
 +
*[https://docs.microsoft.com/zh-tw/powershell/scripting/install/windows-powershell-system-requirements Windows PowerShell 系統需求]
 +
*參照:
 +
**[http://social.technet.microsoft.com/Forums/zh-TW/powershellzhcht/threads Windows PowerShell 論壇]
 +
**[http://technet.microsoft.com/zh-tw/scriptcenter/default.aspx 指令碼中心]
 +
 +
;安裝
 +
*[http://support.microsoft.com/kb/968930/zh-tw KB968930 - Windows Management Framework Core package (Windows PowerShell 2.0 and WinRM 2.0)]
 +
*[http://support.microsoft.com/kb/968929/zh-tw KB968929 - Windows Management Framework (Windows PowerShell 2.0, WinRM 2.0, and BITS 4.0)]
 +
*[https://github.com/PowerShell/PowerShell/releases Releases · PowerShell/PowerShell]
 +
**[https://docs.microsoft.com/zh-tw/powershell/scripting/install/installing-powershell-core-on-windows 在 Windows 上安裝 PowerShell - PowerShell | Microsoft Docs]
 +
 +
;啟動方法
 +
*【開始】→【執行】→【powershell】
  
==相關==
 
*[http://www.pstips.net/ PowerShell 中文博客] - ''收集和分享PowerShell相关教程,技术和最新动态''
 
  
 
[[Category:軟體]]
 
[[Category:軟體]]
 
[[Category:Microsoft]]
 
[[Category:Microsoft]]
[[Category:.NetFramework]]
+
[[分類:.NET]]
 +
[[分類:跨平台]]

於 2022年8月25日 (四) 22:56 的修訂

PowerShell,參照:『維基百科~PowerShell

技術文件

其他

應用

其他

FAQ

安裝PowerShell

使用嚴格模式

# 設定嚴格模式 1.0
Set-StrictMode -Version 1.0
 
# 設定嚴格模式 2.0
Set-StrictMode -Version 2.0
 
# 設定嚴格模式 3.0
Set-StrictMode -Version 3.0
 
# 設定嚴格模式 最後版本
Set-StrictMode -Version Latest
 
# 關閉嚴格模式
Set-StrictMode -Off

常用指令

  • Get-Help
取得指令的使用說明
  • exit
離開PowerShell
  • HELP
線上說明
  • kill
停止執行中的程序
  • ps
顯示執行中的程序狀態
  • Get-Module
顯示已安裝的模組
  • Get-Package
顯示已安裝的軟體套件
  • Find-Package -Name 套件名
尋找套件(需安裝Nuget)
  • Get-PackageSource
顯示能安裝的軟體套件的來源

軟體套件安裝

經由Chocolatey

$ Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  • 安裝軟體 (以notepadplusplus為例)
$ choco install notepadplusplus

從WindowsApps安裝應用軟體

  • 以WindowsCalculator為例
$ManifestPath = (Get-AppxPackage -Name "*WindowsCalculator*").InstallLocation + "\Appxmanifest.xml"
Add-AppxPackage -Path $ManifestPath -Register -DisableDevelopmentMode

改善PowerShell的效能

取得網路卡資訊

Get-WmiObject win32_NetworkAdapter | %{ if ($_.PhysicalAdapter -and $_.Name -notmatch "Virtual") {$_ } }

使用7z

使用git

使用xml

取得資料夾內附檔名為zip的檔名

$Dir="D:/Folder"    
 
foreach($item in (dir $Dir "*.zip")){ 
    Echo $item.Name
}

關閉程式與執行程式

關閉程式
#取得授權
Set-ExecutionPolicy RemoteSigned
 
#判斷與停止Client
if(Get-Process client -ErrorAction SilentlyContinue)
{
    Stop-Process -Name client
}
 
#等待1s
Start-Sleep -s 1
執行程式
#取得授權
Set-ExecutionPolicy RemoteSigned
#執行項目
[System.Diagnostics.Process]::Start("C:\Client\client.exe", "")

刪除超過30天的資料庫備份檔

# 清除超過30天的資料庫備份檔
#
# 用法:
# 用管理員身分執行 PowerShell (怕資料夾或檔案鎖在管理員權限而無法刪除)
# >D:
# >CD D:\Buckup
# >./ClearDbBackup.ps1
 
 
#定義主目錄
#透過Get-ChildItem抓該資料夾內的檔案列表
$TargetFolder = "D:\Backup\DB"
 
#定義天數,要刪除多久以前的檔案
#若是要刪除30天前的檔案,$Days就設定30 
$Days = 30
 
#取今日時間
$Currentlytime = (Get-Date).AddDays(-$Days)
 
$Dirs = Get-Childitem -Path $TargetFolder -Recurse -Include *.bak
 
foreach ($item in $Dirs) 
{
	#判斷檔案最後修改時間
	#由LastWriteTime抓取檔案最後修改時間
	#利用AddDays對$Currentlytime做時間的運算 
	if($item.LastWriteTime -le $Currentlytime)            #-le 小於或等於
	{
	        Remove-Item $item.FullName | out-null  
        	write-host "Finish Deleting Dir $item" -ForegroundColor "DarkRed"
	}    
}

ASP.NET WebForm PostBack

JSON

批次轉檔

文字亂碼

計算檔案的雜湊值

圖檔處理

用管理者身分執行的 PowerShell 腳本

其他

安裝
啟動方法
  • 【開始】→【執行】→【powershell】