本主題說明如何建立一個能在多層資料儲存上運作的 Windows PowerShell 提供者。 對於這種類型的資料儲存,儲存的頂層包含根項目,每個後續層級稱為子節點。 透過允許使用者在這些子節點上工作,使用者可以透過資料儲存庫進行階層式互動。
能在多層資料儲存上運作的提供者稱為 Windows PowerShell 容器提供者。 不過請注意,Windows PowerShell 容器提供者只能在有一個容器(無巢狀容器)且包含項目時使用。 如果有巢狀容器,那你必須實作 Windows PowerShell 導覽提供者。 欲了解更多關於實作 Windows PowerShell 導覽服務的資訊,請參閱 建立 Windows PowerShell 導覽服務提供者。
備註
你可以使用 Windows Vista 和 .NET Framework 3.0 執行元件的 Microsoft Windows 軟體開發套件下載 C# 原始碼檔案(AccessDBSampleProvider04.cs)。 如需下載說明,請參閱 《如何安裝 Windows PowerShell》及《Download the Windows PowerShell SDK》。 下載的原始碼檔案可在 <PowerShell 範例> 目錄中取得。 欲了解更多關於其他 Windows PowerShell 提供者實作的資訊,請參閱 「設計您的 Windows PowerShell 提供者」。
此處描述的 Windows PowerShell 容器提供者定義資料庫為其單一容器,資料庫的表格與列定義為容器中的項目。
謹慎
請注意,此設計假設資料庫中有一個名為 ID 的欄位,且欄位類型為 LongInteger。
定義 Windows PowerShell 容器提供者類別
Windows PowerShell 容器提供者必須定義一個 .NET 類別,該類別源自 System.Management.Automation.Provider.ContainerCmdletProvider 基底類別。 以下是本節所述 Windows PowerShell 容器提供者的類別定義。
[CmdletProvider("AccessDB", ProviderCapabilities.None)]
public class AccessDBProvider : ContainerCmdletProvider
請注意,在這個類別定義中, System.Management.Automation.Provider.CmdletProviderAttribute 屬性包含兩個參數。 第一個參數指定了 Windows PowerShell 所使用的使用者友善提供者名稱。 第二個參數指定了 Windows PowerShell 在指令處理過程中,向 Windows PowerShell 執行時所暴露的能力。 對於這個提供者,並沒有新增任何 Windows PowerShell 專屬的功能。
定義基礎功能
如《 設計您的 Windows PowerShell 提供者》所述, System.Management.Automation.Provider.ContainerCmdletProvider 類別源自多個提供不同提供者功能的類別。 因此,Windows PowerShell 容器提供者需要定義這些類別所提供的所有功能。
若要實作新增會話特定初始化資訊及釋放提供者使用的資源的功能,請參見 建立基本 Windows PowerShell 提供者。 然而,大多數提供者(包括此處描述的提供者)都能使用 Windows PowerShell 提供的預設實作。
要取得資料儲存庫的存取權,提供者必須實作 System.Management.Automation.Provider.DriveCmdletProvider 基底類別的方法。 欲了解更多實作這些方法的資訊,請參閱 建立 Windows PowerShell 磁碟機提供者。
為了操作資料庫中的項目,例如取得、設定和清除項目,提供者必須實作 System.Management.Automation.Provider.ItemCmdletProvider 基底類別所提供的方法。 欲了解更多實作這些方法的資訊,請參閱 建立 Windows PowerShell 項目提供者。
取回兒童物品
要取得子項目,Windows PowerShell 容器提供者必須覆寫 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法,以支援該 Get-ChildItem 指令的呼叫。 此方法從資料儲存庫取得子項目,並將其寫入管線作為物件。 如果 recurse 指定 cmdlet 的參數,該方法會擷取所有子節點,不論它們處於何層級。 若未指定參數 recurse ,該方法只會取得單一層級的子節點。
以下是 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作。 請注意,當路徑指示為 Access 資料庫時,此方法會擷取所有資料庫資料表中的子項目;若路徑指示資料表,則從該資料表的列中擷取子項目。
protected override void GetChildItems(string path, bool recurse)
{
// If path represented is a drive then the children in the path are
// tables. Hence all tables in the drive represented will have to be
// returned
if (PathIsDrive(path))
{
foreach (DatabaseTableInfo table in GetTables())
{
WriteItemObject(table, path, true);
// if the specified item exists and recurse has been set then
// all child items within it have to be obtained as well
if (ItemExists(path) && recurse)
{
GetChildItems(path + pathSeparator + table.Name, recurse);
}
} // foreach (DatabaseTableInfo...
} // if (PathIsDrive...
else
{
// Get the table name, row number and type of path from the
// path specified
string tableName;
int rowNumber;
PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
if (type == PathType.Table)
{
// Obtain all the rows within the table
foreach (DatabaseRowInfo row in GetRows(tableName))
{
WriteItemObject(row, path + pathSeparator + row.RowNumber,
false);
} // foreach (DatabaseRowInfo...
}
else if (type == PathType.Row)
{
// In this case the user has directly specified a row, hence
// just give that particular row
DatabaseRowInfo row = GetRow(tableName, rowNumber);
WriteItemObject(row, path + pathSeparator + row.RowNumber,
false);
}
else
{
// In this case, the path specified is not valid
ThrowTerminatingInvalidPathException(path);
}
} // else
} // GetChildItems
實作 GetChildItems 時需要注意的事項
以下條件可能適用於您的 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems*:
在定義提供者類別時,Windows PowerShell 容器提供者可能會宣告 ExpandWildcard、Filter、Include 或 Exclude 這些提供者能力,這些功能來自 System.Management.Automation.Provider.ProviderCapabilities 。 在這些情況下, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作需要確保傳遞給該方法的路徑符合指定能力的要求。 為此,方法應存取適當的屬性,例如 System.Management.Automation.Provider.CmdletProvider.Exclude* 與 System.Management.Automation.Provider.CmdletProvider.Include* 屬性。
此方法的實作應考慮任何可能讓使用者可見的存取方式。 例如,若使用者透過 FileSystem 提供者(由 Windows PowerShell 提供)對檔案有寫入權限,但無法讀取,該檔案仍然存在,而 System.Management.Automation.Provider.ItemCmdletProvider.ItemExists* 會回傳
true。 你的實作可能需要檢查父項目,看看子項目是否能被列舉。當撰寫多個項目時, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法可能會花一些時間。 你可以用 System.Management.Automation.Provider.CmdletProvider.WriteItemObject* 方法,一個一個來設計你的服務提供者來寫入這些項目。 使用此技術後,將物品呈現給使用者串流。
你對 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 的實作負責防止在存在循環連結等情況下的無限遞迴。 應拋出適當的終止例外以反映此條件。
將動態參數附加於 Get-ChildItem 指令小子
有時呼叫 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 的 Get-ChildItem cmdlet 需要在執行時動態指定額外參數。 為了提供這些動態參數,Windows PowerShell 容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItemsDynamicParameters* 方法。 此方法會取得指定路徑項目的動態參數,並回傳一個具有類似 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件的屬性與欄位的物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 Get-ChildItem cmdlet。
這個 Windows PowerShell 容器提供者並未實作此方法。 然而,以下程式碼是此方法的預設實作。
取回子項目名稱
為了取得子項目的名稱,Windows PowerShell 容器提供者必須覆寫 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildNames* 方法,以支援在Name指定參數時從Get-ChildItem該指令元呼叫。 此方法會取得指定路徑的子項目名稱,或若 returnAllContainers cmdlet 參數指定,則取得所有容器的子項目名稱。 子名稱是路徑中葉子部分。 例如,路徑 C:\windows\system32\abc.dll 的子名稱為「abc.dll」。 目錄 C:\windows\system32 的子名稱是「system32」。
以下是 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildNames* 方法的實作。 請注意,若指定路徑為存取資料庫(磁碟機),則會取得資料表名稱;若路徑為資料表,則取得列號。
protected override void GetChildNames(string path,
ReturnContainers returnContainers)
{
// If the path represented is a drive, then the child items are
// tables. get the names of all the tables in the drive.
if (PathIsDrive(path))
{
foreach (DatabaseTableInfo table in GetTables())
{
WriteItemObject(table.Name, path, true);
} // foreach (DatabaseTableInfo...
} // if (PathIsDrive...
else
{
// Get type, table name and row number from path specified
string tableName;
int rowNumber;
PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
if (type == PathType.Table)
{
// Get all the rows in the table and then write out the
// row numbers.
foreach (DatabaseRowInfo row in GetRows(tableName))
{
WriteItemObject(row.RowNumber, path, false);
} // foreach (DatabaseRowInfo...
}
else if (type == PathType.Row)
{
// In this case the user has directly specified a row, hence
// just give that particular row
DatabaseRowInfo row = GetRow(tableName, rowNumber);
WriteItemObject(row.RowNumber, path, false);
}
else
{
ThrowTerminatingInvalidPathException(path);
}
} // else
} // GetChildNames
實作 GetChildNames 時需要注意的事項
以下條件可能適用於您的 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems*:
在定義提供者類別時,Windows PowerShell 容器提供者可能會宣告 ExpandWildcard、Filter、Include 或 Exclude 這些提供者能力,這些功能來自 System.Management.Automation.Provider.ProviderCapabilities 。 在這些情況下, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作需要確保傳遞給該方法的路徑符合指定能力的要求。 為此,方法應存取適當的屬性,例如 System.Management.Automation.Provider.CmdletProvider.Exclude* 與 System.Management.Automation.Provider.CmdletProvider.Include* 屬性。
備註
當
returnAllContainers指令本的參數被指定時,此規則有例外。 在這種情況下,方法應該會取得容器的任何子名稱,即使它與 System.Management.Automation.Provider.CmdletProvider.Filter*、 System.Management.Automation.Provider.CmdletProvider.Include 或 System.Management.Automation.Provider.CmdletProvider.Exclude* 屬性的值不符。預設情況下,除非指定 System.Management.Automation.Provider.CmdletProvider.Force* 屬性,否則此方法的覆寫不應取得通常對使用者隱藏的物件名稱。 若指定路徑為容器,則不需要 System.Management.Automation.Provider.CmdletProvider.Force* 屬性。
你對 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildNames* 的實作負責防止在有循環連結等情況下出現無限遞迴。 應拋出適當的終止例外以反映此條件。
將動態參數附加於 Get-ChildItem 指令匣(名稱)
有時 Get-ChildItem cmdlet(包含參數 Name )需要額外參數,這些參數會在執行時動態指定。 為了提供這些動態參數,Windows PowerShell 容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.GetChildNamesDynamicParameters* 方法。 此方法會取得指定路徑項目的動態參數,並回傳一個具有屬性與欄位的物件,解析屬性類似於 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 Get-ChildItem cmdlet。
該提供者並未實作此方法。 然而,以下程式碼是此方法的預設實作。
項目更名
要重新命名項目,Windows PowerShell 容器提供者必須覆寫 System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 方法,以支援該 Rename-Item 指令的呼叫。 此方法將指定路徑上的項目名稱改為新名稱。 新名稱必須始終相對於父項目(容器)而言。
此提供者不會覆蓋 System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 方法。 不過,以下是預設實作。
實作 RenameItem 時需要注意的事項
以下條件可能適用於您對 System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 的實作:
在定義提供者類別時,Windows PowerShell 容器提供者可能會宣告 ExpandWildcard、Filter、Include 或 Exclude 這些提供者能力,這些功能來自 System.Management.Automation.Provider.ProviderCapabilities 。 在這些情況下, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作需要確保傳遞給該方法的路徑符合指定能力的要求。 為此,方法應存取適當的屬性,例如 System.Management.Automation.Provider.CmdletProvider.Exclude* 與 System.Management.Automation.Provider.CmdletProvider.Include* 屬性。
System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 方法僅用於修改項目名稱,不適用於移動操作。 如果
newName參數包含路徑分隔符,或可能導致物件改變父位置,你的方法實作應該會寫錯。預設情況下,除非指定 System.Management.Automation.Provider.CmdletProvider.Force* 屬性,否則此方法的覆寫不應重新命名物件。 若指定路徑為容器,則不需要 System.Management.Automation.Provider.CmdletProvider.Force* 屬性。
你實作的 System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 方法,應該在對資料庫做任何變更前,先呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess ,並檢查其回傳值。 此方法用於在系統狀態變更時確認操作執行,例如重新命名檔案。 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 會將待更改的資源名稱傳送給使用者,Windows PowerShell 執行環境會考慮任何命令列設定或偏好變數來決定顯示哪些內容。
在呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 返回
true後, System.Management.Automation.Provider.ContainerCmdletProvider.RenameItem* 方法應呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldContinue 方法。 此方法會向使用者發送確認訊息,以允許額外回饋以判斷是否應該繼續操作。 提供者應致電 System.Management.Automation.Provider.CmdletProvider.ShouldContinue ,作為潛在危險系統修改的額外檢查。
將動態參數附加於 Rename-Item 指令小子
有時 Rename-Item cmdlet 需要額外參數,這些參數會在執行時動態指定。 為了提供這些動態參數,Windows PowerShell 容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.RenameItemDynamicParameters* 方法。 此方法取得指定路徑上的項目參數,並回傳一個具有屬性與欄位的物件,解析屬性類似 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 Rename-Item cmdlet。
此容器提供者未實作此方法。 然而,以下程式碼是此方法的預設實作。
創建新物品
要建立新項目,容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法來支援 cmdlet 的呼叫 New-Item 。 此方法會建立位於指定路徑的資料項目。
type cmdlet 的參數包含新項目的提供者定義型別。 例如,檔案系統提供者使用 type 一個參數,值為「file」或「directory」。
newItemValue cmdlet 的參數指定新項目的提供者專屬值。
以下是 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法的實作。
protected override void NewItem( string path, string type, object newItemValue )
{
// Create the new item here after
// performing necessary validations
//
// WriteItemObject(newItemValue, path, false);
// Example
//
// if (ShouldProcess(path, "new item"))
// {
// // Create a new item and then call WriteObject
// WriteObject(newItemValue, path, false);
// }
} // NewItem
{
case 1:
{
string name = pathChunks[0];
if (TableNameIsValid(name))
{
tableName = name;
retVal = PathType.Table;
}
}
break;
case 2:
{
string name = pathChunks[0];
實施 NewItem 時需要記住的事項
以下條件可能適用於您對 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 的實作:
System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法應該會對參數中傳遞的
type字串進行大小寫不區分的比較。 這也應該能讓配對變得最不模糊。 例如,對於「file」和「directory」這兩種類型,只需使用第一個字母即可消歧義。 如果參數type顯示提供者無法建立的型別, System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法應該會寫入一個 ArgumentException,並附上訊息,說明提供者可建立的類型。對於參數
newItemValue,建議實作 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法,至少接受字串。 它也應該接受 System.Management.Automation.Provider.ItemCmdletProvider.GetItem* 方法所取得的物件類型,且該物件類型相同路徑。 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法可使用 System.Management.Automation.LanguagePrimitives.ConvertTo* 方法將型別轉換成所需的型別。你實作 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法,應該在對資料庫做任何變更前,先呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 並檢查其回傳值。 在呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 回傳為真後, System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 方法應呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldContinue 方法,作為潛在危險系統修改的額外檢查。
將動態參數附加於 New-Item 指令小子
有時 New-Item cmdlet 需要額外參數,這些參數會在執行時動態指定。 為了提供這些動態參數,容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.NewItemDynamicParameters* 方法。 此方法取得指定路徑上的項目參數,並回傳一個具有屬性與欄位的物件,解析屬性類似 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 New-Item cmdlet。
該提供者並未實作此方法。 然而,以下程式碼是此方法的預設實作。
移除物品
要移除項目,Windows PowerShell 提供者必須覆寫 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItem* 方法,以支援從 cmdlet 呼叫 Remove-Item 。 此方法會在指定路徑上從資料儲存中刪除一個項目。 若 recurseRemove-Item cmdlet 參數設為 true,該方法會移除所有子項目,不論其等級如何。 若參數設為 false,該方法僅移除指定路徑上的單一項目。
此服務提供者不支援物品移除。 然而,以下程式碼是 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItem* 的預設實作。
關於實作 RemoveItem 需要注意的事項
以下條件可能適用於您對 System.Management.Automation.Provider.ContainerCmdletProvider.NewItem* 的實作:
在定義提供者類別時,Windows PowerShell 容器提供者可能會宣告 ExpandWildcard、Filter、Include 或 Exclude 這些提供者能力,這些功能來自 System.Management.Automation.Provider.ProviderCapabilities 。 在這些情況下, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作需要確保傳遞給該方法的路徑符合指定能力的要求。 為此,方法應存取適當的屬性,例如 System.Management.Automation.Provider.CmdletProvider.Exclude* 與 System.Management.Automation.Provider.CmdletProvider.Include* 屬性。
預設情況下,除非 System.Management.Automation.Provider.CmdletProvider.Force* 屬性設為 true,否則此方法的覆寫不應移除物件。 若指定路徑為容器,則不需要 System.Management.Automation.Provider.CmdletProvider.Force* 屬性。
你對 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItem* 的實作,負責防止在有循環連結等情況下出現無限遞迴。 應拋出適當的終止例外以反映此條件。
你實作的 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItem* 方法,應該在對資料庫做任何變更前,先呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 並檢查其回傳值。 在呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 返回
true後, System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItem* 方法應呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldContinue 方法,作為潛在危險系統修改的額外檢查。
將動態參數附加於 Remove-Item 指令小子
有時 Remove-Item cmdlet 需要額外參數,這些參數會在執行時動態指定。 為了提供這些動態參數,容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItemDynamicParameters* 方法來處理這些參數。 此方法會取得指定路徑項目的動態參數,並回傳一個具有屬性與欄位的物件,解析屬性類似於 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 Remove-Item cmdlet。
此容器提供者未實作此方法。 然而,以下程式碼是 System.Management.Automation.Provider.ContainerCmdletProvider.RemoveItemDynamicParameters* 的預設實作。
查詢子項目
為了檢查指定路徑是否存在子項目,Windows PowerShell 容器提供者必須覆寫 System.Management.Automation.Provider.ContainerCmdletProvider.HasChildItems* 方法。 若該項目有子節點false,則此方法會回傳,否則則回傳true。 對於空路徑或空路徑,方法將資料儲存中的任意項目視為子節點,並回傳 true。
這是 System.Management.Automation.Provider.ContainerCmdletProvider.HasChildItems* 方法的覆寫。 如果 ChunkPath 輔助方法產生超過兩個路徑部分,該方法會回傳 false,因為只定義了資料庫容器和資料表容器。 欲了解更多關於此輔助方法的資訊,請參閱「 建立 Windows PowerShell 項目提供者」中討論的 ChunkPath 方法。
protected override bool HasChildItems( string path )
{
return false;
} // HasChildItems
ErrorCategory.InvalidOperation, tableName));
}
return results;
實作 HasChildItems 時需要注意的事項
以下條件可能適用於您的 System.Management.Automation.Provider.ContainerCmdletProvider.HasChildItems*:
- 如果容器提供者暴露了一個包含有趣掛載點的根節點, System.Management.Automation.Provider.ContainerCmdletProvider.HasChildItems* 方法的實作應該會在路徑傳遞空字串或空字串時回傳
true。
複製項目
要複製項目,容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 方法來支援指令長的呼叫 Copy-Item 。 此方法將資料項目從指令本參數指示 path 的位置複製到該參數所指示 copyPath 的位置。 若 recurse 參數指定,該方法會複製所有子容器。 若未指定參數,方法僅複製單一層級的項目。
該提供者並未實作此方法。 然而,以下程式碼是 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 的預設實作。
實作 CopyItem 時需要注意的事項
以下條件可能適用於您實作的 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem:
在定義提供者類別時,Windows PowerShell 容器提供者可能會宣告 ExpandWildcard、Filter、Include 或 Exclude 這些提供者能力,這些功能來自 System.Management.Automation.Provider.ProviderCapabilities 。 在這些情況下, System.Management.Automation.Provider.ContainerCmdletProvider.GetChildItems* 方法的實作需要確保傳遞給該方法的路徑符合指定能力的要求。 為此,方法應存取適當的屬性,例如 System.Management.Automation.Provider.CmdletProvider.Exclude* 與 System.Management.Automation.Provider.CmdletProvider.Include* 屬性。
預設情況下,除非 將 System.Management.Automation.Provider.CmdletProvider.Force* 屬性設定為
true。 例如,FileSystem 提供者不會在現有的 C:\abc.txt 檔案上複製 C:\temp\abc.txt,除非將 System.Management.Automation.Provider.CmdletProvider.Force* 屬性設定為true。 如果參數中指定的copyPath路徑存在且標示容器,則不需要 System.Management.Automation.Provider.CmdletProvider.Force* 屬性。 在這種情況下, System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 應該將參數所指示path的項目複製到該參數所copyPath指示的容器,作為子節點。你對 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 的實作負責防止在存在循環連結等情況下的無限遞迴。 應拋出適當的終止例外以反映此條件。
你實作的 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 方法,應該在對資料庫做任何變更前,先呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess ,並檢查其回傳值。 在呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldProcess 後, System.Management.Automation.Provider.ContainerCmdletProvider.CopyItem 方法應呼叫 System.Management.Automation.Provider.CmdletProvider.ShouldContinue 方法,作為潛在危險系統修改的額外檢查。 欲了解更多關於呼叫這些方法的資訊,請參見 「重命名項目」。
將動態參數附加於 Copy-Item 指令小子
有時 Copy-Item cmdlet 需要額外參數,這些參數會在執行時動態指定。 為了提供這些動態參數,Windows PowerShell 容器提供者必須實作 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItemDynamicParameters* 方法來處理這些參數。 此方法取得指定路徑上的項目參數,並回傳一個具有屬性與欄位的物件,解析屬性類似 cmdlet 類別或 System.Management.Automation.RuntimeDefinedParameterDictionary 物件。 Windows PowerShell 執行時會使用回傳的物件來將參數加入 Copy-Item cmdlet。
該提供者並未實作此方法。 然而,以下程式碼是 System.Management.Automation.Provider.ContainerCmdletProvider.CopyItemDynamicParameters* 的預設實作。
程式碼範例
完整範例程式碼請參見 AccessDbProviderSample04 程式碼範例。
建置 Windows PowerShell 提供者
請參閱 如何註冊指令長、提供者及主機應用程式。
測試 Windows PowerShell 提供者
當你的 Windows PowerShell 提供者已經註冊到 Windows PowerShell 後,你可以在命令列執行支援的 cmdlets 來測試。 請注意,以下範例輸出使用了一個虛構的 Access 資料庫。
執行
Get-ChildItemcmdlet 從 Access 資料庫中的 Customers 表取得子項目清單。Get-ChildItem mydb:customers以下輸出結果顯示。
PSPath : AccessDB::customers PSDrive : mydb PSProvider : System.Management.Automation.ProviderInfo PSIsContainer : True Data : System.Data.DataRow Name : Customers RowCount : 91 Columns :再次執行
Get-ChildItemcmdlet 以取得資料表的資料。(Get-ChildItem mydb:customers).Data以下輸出結果顯示。
TABLE_CAT : C:\PS\northwind TABLE_SCHEM : TABLE_NAME : Customers TABLE_TYPE : TABLE REMARKS :現在用
Get-Itemcmdlet 在資料表第 0 列取得項目。Get-Item mydb:\customers\0以下輸出結果顯示。
PSPath : AccessDB::customers\0 PSDrive : mydb PSProvider : System.Management.Automation.ProviderInfo PSIsContainer : False Data : System.Data.DataRow RowNumber : 0重用
Get-Item以取得第 0 列項目的資料。(Get-Item mydb:\customers\0).Data以下輸出結果顯示。
CustomerID : 1234 CompanyName : Fabrikam ContactName : Eric Gruber ContactTitle : President Address : 4567 Main Street City : Buffalo Region : NY PostalCode : 98052 Country : USA Phone : (425) 555-0100 Fax : (425) 555-0101現在用
New-Itemcmdlet 把一列加入現有的資料表。 參數Path指定了該列的完整路徑,且必須表示一列數大於表格中現有列數的列數。 參數Type表示Row要指定要新增的項目類型。 最後,參數Value指定了該列的逗號分隔欄位值列表。New-Item -Path mydb:\Customers\3 -ItemType "Row" -Value "3,CustomerFirstName,CustomerLastName,CustomerEmailAddress,CustomerTitle,CustomerCompany,CustomerPhone, CustomerAddress,CustomerCity,CustomerState,CustomerZip,CustomerCountry"驗證新項目操作的正確性如下。
PS mydb:\> cd Customers PS mydb:\Customers> (Get-Item 3).Data以下輸出結果顯示。
ID : 3 FirstName : Eric LastName : Gruber Email : ericgruber@fabrikam.com Title : President Company : Fabrikam WorkPhone : (425) 555-0100 Address : 4567 Main Street City : Buffalo State : NY Zip : 98052 Country : USA