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

Download a File from an FTP–Use PowerShell

使用過 FTP 的人,一定知道在 FTP 環境中,需要有 帳號/密碼 的確認過程,才能處理檔案。
當這個處理過程要改用 PowerShell 來控制時,該如何下指令?
假設,有一 FTP ,IP = 10.1.2.71
該網站上有一個檔案,檔名:RFQC100485.zip
希望下載這個檔案,改存放成 D:\Test\PowerShell\backup.zip
依據上述情況,處理方法可以有兩種:

# 方法 1
$source = "ftp://10.1.2.71/RFQC100485.zip"
$destination = "D:\Test\PowerShell\backup.zip"
# 需要在輸入 密碼 訊息!
Invoke-WebRequest $source -OutFile $destination -Credential myFtp


















# 方法 2
$source = "ftp://10.1.2.71/RFQC100485.zip"
$destination = "D:\Test\PowerShell\backup.zip"
$username = "myFtp"
$password = "myPassword" | ConvertTo-SecureString -asPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)Invoke-WebRequest $source -OutFile $destination -Credential $cred

在 PowerShell 中,探討 Excel 的架構

藉由 http://www.lazywinadmin.com/2014/03/powershell-read-excel-file-using-com.html 網路資源一文,來探究 Excel 架構該如何在 PowerShell 中表現出來!

首先來看個 Excel 範例:VIDEOSERVER01-BuildSpecs.xlsx

接下來的問題,就是該如何使用 PowerShell 來讀取該檔案的內容?及如何隨心所欲的控制 Excel 的數據!

  • 使用 COM 來建置 Excel.Application 的物件
    $objExcel = New-Object -ComObject Excel.Application
  • 顯示 Excel.Application 的物件
    $objExcel.Visible = $true
  • 使用 quit() 來移除 Excel.Application 的物件
    $objExcel.quit()
  • 使用 Open() 來開啟 Excel 檔案
    $FilePath = "D:\Test\PowerShell\VIDEOSERVER01-BuildSpecs.xlsx"
    $WorkBook = $objExcel.Workbooks.Open( $FilePath )
  • 得知 WordBook 中,含有幾個 WorkSheet
    $WorkBook.sheets
  • 存取特定的 Sheet
    $SheetName = "BuildSpecs"
    $WorkSheet = $WorkBook.sheets.item( $SheetName )
  • 其他相關指令
    $objExcel.WorkBooks | Select-Object -Property name, path, author
    $objExcel.WorkBooks
    $objExcel.WorkBooks | Get-Member
    $WorkBook
    $WorkBook | Get-Member -Name *sheet*
    $WorkBook.sheets | Select-Object -Property Name


對於儲存格的存取,可以有很多種方式,擇一使用即可:

$worksheet.Range("C3").Text
$worksheet.Range("C3:C3").Text
$worksheet.Range("C3","C3").Text
$worksheet.cells.Item(3, 3).text
$worksheet.cells.Item(3, 3).value2
$worksheet.Columns.Item(3).Rows.Item(3).Text
$worksheet.Rows.Item(3).Columns.Item(3).Text
$worksheet.UsedRange.Range("c3").Text

網路資源:Microsoft.Office.Interop.Excel.ApplicationClass

Win 7 環境下,如何更新 PowerShell 的版本?

在 Win 7 環境下,作業系統會內定安裝 PowerShell 的功能,只不過它的版本只有 2.0,若是需要更新地版本(目前最新版本是 4.0),就要下載 Windows Management Framework 4.0 來安裝!(記得要重新開機!)

在還未更新版本之前,先來了解目前系統中 PowerShell 是什麼版次?

1. 進入 Command Line

2. 切換目錄到 C:\Windows\System32\WindowsPowerShell\v1.0

3. 執行指令 $PSVersionTable

4. 查看版本 PSVersion ---> 2.0

未命名

更新後,可以看到是 4.0 版本。

image


若是你使用 Win 8,那麼它就內定安裝 PowerShell 4.0 !

擷取

 

接下來,讓我們了解 PowerShell 4.0 有哪些功能?

Windows PowerShell 4.0 is part of the Windows Management Framework 4.0, which includes the following:

  • Windows PowerShell
  • Windows PowerShell Integrated Scripting Environment (ISE)
  • Windows PowerShell Web Services (Management OData IIS Extension)
  • Windows Remote Management (WinRM)
  • Windows Management Infrastructure (WMI)
  • Server Manager WMI provider
  • Windows PowerShell Desired State Configuration (DSC)


網路資源:

PowerShell Snapin (嵌入式管理單元)

PowerShell Snapin (嵌入式管理單元),內建有七種。若有需要可再以外掛方式來增加。

內建的七種單元:

  • Microsoft.PowerShell.Diagnostics
    這個 Windows PowerShell 嵌入式管理單元包含 Windows Eventing 及 效能計數器 Cmdlet。
  • Microsoft.WSMan.Management
    這個 PS 嵌入式管理單元包含 Cmdlet (例如 WSManInstance 和 Set-WSManInstance),Windows PowerShell 主機會使用這些 Cmdlet 管理 WsMan 作業。
  • Microsoft.PowerShell.Core
    這個 Windows PowerShell 嵌入式管理單元包含用於管理 Windows PowerShell 元件的 Cmdlet。
  • Microsoft.PowerShell.Utility
    這個 Windows PowerShell 嵌入式管理單元包含用於操控資料的公用程式 Cmdlet。
  • Microsoft.PowerShell.Host
    這個 Windows PowerShell 嵌入式管理單元包含供 Windows PowerShell 主控台主機使用的 Cmdlet (例如 Start-Transcript 和 Stop-Transcript )。
  • Microsoft.PowerShell.Management
    這個 Windows PowerShell 嵌入式管理單元包含用於管理 Windows 元件的管理 Cmdlet。
  • Microsoft.PowerShell.Security
    這個 Windows PowerShell 嵌入式管理單元包含用來管理 Windows PowerShell 安全性的 Cmdlet。

上述單元內容,可執行 Get-PSSnapin 得知。

 

若是要增加,使用指令:Add-PSSnapin。
    Add-PSSnapin SqlServerProviderSnapin100
    Add-PSSnapin SqlServerCmdletSnapin100

若是要移除,使用指令:Move-PSSnapin。
    Move-PSSnapin SqlServerProviderSnapin100
    Move-PSSnapin SqlServerCmdletSnapin100

可執行 Get-PSSnapin –Registered 得知,目前所有外掛的 Snapin 有哪些?

PowerShell 要如何與 SQL Server 配搭?

首先,就以文章標題來探討,PowerShell 是不是可以和 SQL Server 整合,配搭在一起使用?
答案:Yes。

接下來是要如何使用呢?

你是可以在 SQL Server 環境中,執行、使用 PowerShell。共有四種方法:

  • 使用 SQLPS.exe 工具程式
  • 在 SQL Server Management Studio 環境中,指定需處理物件後,按右鍵 / PowerShell。
  • SQL Server Agent Job 中,Agent Job 的形態(Type)設定,就可以指定為 PowerShell。
  • 若是已在 PowerShell 的環境中,還可以再載入 PowerShell provider 進來,以增強功能。

 

使用 SQLPS.exe 工具程式,需了解:

  1. SQLPS 工具程式,需要將該程式的存放路徑設置到環境變數 PATH 中。
  2. 32位元電腦, SQLPS.exe 存放位置: \Program Files\Microsoft SQL Server\100\Tools\Binn
  3. 64位元電腦, SQLPS.exe 存放位置: \Program Files (x86)\Microsoft SQL Server\100\Tools\Binn

以什麼身份權限在執行 PowerShell

執行 PowerShell 會因不同的身份權限與執行方式,而有不同的結果。(請注意:視窗的抬頭文字)例如:

● 系統管理員, 以系統管理員身份執行

image

● 目前的開機使用者,在『執行』對話方塊中,鍵入 PowerShell

image

● Normal(All User / Non-elevated),在『開始』功能表中,執行 PowerShell

image

image

** 因為權限上有所不同,將會產生同一個程式,有的權限可以執行,有的不能執行的情形。

第一次執行 PowerShell @ Win 7

在 Win 7 環境,第一次執行 PowerShell 程式時,遇到下列運行錯誤:
Get-PowerShellRequirements.ps1 檔案無法載入,因為這個系統上已停用指令碼執行。如需詳細資訊,請參閱 "get-help about_signing"。
At line:0 char:0


運行 get-help about_signing 指令後,得知 Windows PowerShell 執行原則的預設值 Restricted,也就是在這原則下,系統是不會允許任何指令碼執行。
所以要修改它的執行原則設定值。

用下列命令將電腦的執行原則變更為 RemoteSigned:
    set-executionpolicy remotesigned


不過,沒想到執行 set-executionpolicy remotesigned 指令後,又遇到另外一個錯誤訊息:
    Set-ExecutionPolicy : 拒絕存取登錄機碼 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'。
    位於 行:1 字元:20
    + set-executionpolicy <<<<  remotesigned
        + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
        + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand


原來是電腦的註冊機碼,無法正常被修正;這時只好已手工方式來調整。
開始/執行/RegEdit,在 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell] 處,新增一個機碼 ExecutionPolicy,機碼值為 RemoteSigned。

你也可以把下列指令,寫成 ps1-RemoteSigned.reg 檔案 (文字檔格式),再執行之,以便直接匯入機碼值。

ps1-RemoteSigned.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"Path"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"ExecutionPolicy"="RemoteSigned"

 

因為 set-executionpolicy 有多種設定選項:Restricted、AllSigned、RemoteSigned、Unrestricted,
為了日後使用上的方便,可以先行建制好相關的註冊檔,如:
ps1-Restricted.reg、ps1-AllSigned.reg、ps1-RemoteSigned.reg、ps1-Unrestricted.reg
只要將註冊檔中的機碼值,改成和註冊檔名一樣的文字即可。
例如:ps1-Restricted.reg 它的機碼值,就是 Restricted。

ps1-Restricted.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"Path"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"ExecutionPolicy"="Restricted"

PowerShell 入門資源

Windows PowerShell 是以 .NET 技術為基礎,並且與現有的 WSH 保持回溯相容;Windows PowerShell 的腳本程式不僅能存取 .NET CLR,也能使用現有的 COM 技術。
Windows PowerShell 安裝完成後,可以利用以下三種方式執行 Windows PowerShell:
  • 位於『開始 / 所有程式』的『Windows PowerShell』捷徑。
  • 在『開始 / 執行』的『執行』交談窗輸入 powershell。
  • 在任何『命令提示字元』視窗的命令提示符號輸入 powershell。
 
入門學習資源:
Owner's Manual  http://technet.microsoft.com/zh-tw/library/ee221100(en-us).aspx
易學易用的 Windows PowerShell  http://technet.microsoft.com/zh-tw/library/dd125460.aspx
 
作者:賴榮樞   http://www.goodman-lai.idv.tw
講座 (1)—指令、重導、別名 http://technet.microsoft.com/zh-tw/library/dd125489.aspx
講座 (2)—自訂工作環境 http://technet.microsoft.com/zh-tw/library/dd125497.aspx
講座 (3)— PS 磁碟機 http://technet.microsoft.com/zh-tw/library/dd125498.aspx
講座 (4)—變數 http://technet.microsoft.com/zh-tw/library/dd125499.aspx
講座 (5)—儲存資料的其他方式及編寫指令碼的前置準備 http://technet.microsoft.com/zh-tw/library/dd125500.aspx
講座 (6)—運算子(上) http://technet.microsoft.com/zh-tw/library/dd125501.aspx
講座 (7)—運算子(下) http://technet.microsoft.com/zh-tw/library/dd125502.aspx
講座 (8)—迴圈與流程控制 http://technet.microsoft.com/zh-tw/library/dd125503.aspx
講座 (9)—模組化 http://technet.microsoft.com/zh-tw/library/dd125504.aspx


Windows PowerShell 小筆記

Related Resources

PowerShell V1.0 外觀
     %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
image
DOS Command Line 執行 PowerShell 的外觀
image
C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\

PowerShell Scripts 如何執行?

PowerShell Scripts 檔案,附檔名為 .ps1。(是數字的1,不是文字的小寫l)

對於 .ps1 的檔案,該如何執行?
  • 和一般的檔案執行一樣,雙擊 ps1 (double-click);不對
  • 在開始/執行 (Start / Run) 打入Script檔案名稱;不對
  • 直接在 PowerShell 的環境中,打入 Script檔案名稱 (含該檔案的完整路徑及附檔名);對的
  • 在啟動 PowerShell 環境時,一併將 Script檔案名稱 及相關參數指令打入;對的
針對,上述可以正常運作的方式,舉例說明如下:
直接在 PowerShell 的環境中
打入 D:\Learning\PowerShell\Ch02\GetFileShares.ps1
或是
先 cd D:\Learning\PowerShell\Ch02\
再打入 .\GetFileShares.ps1 (即便是在現行作用目錄區中,也要用 .\ )

在啟動 PowerShell 環境時
PowerShell -Noexit -command "D:\Learning\PowerShell\Ch02\GetWmiAndQuery.ps1"
參數
-Noexit:為了保持 PowerShell 的執行結果,能保留下來。
-command:為了是在啟動 PowerShell 時,一併載入指定的批次檔案。

===============================================
在路徑表示中,若是遇到含有 空白字元 的文件夾名稱,需要已 單引號 來標明。
例如:
PowerShell -Noexit -command "D:\Learning\'[MS Press] Windows PowerShell Scripting Guide'\Ch02\GetWmiAndQuery.ps1"