การเขียนผู้ให้บริการนำทาง
หัวข้อนี้อธิบายวิธีการใช้วิธีการของผู้ให้บริการ Windows PowerShell ที่รองรับคอนเทนเนอร์ที่ซ้อนกัน (ที่เก็บข้อมูลหลายระดับ) การย้ายรายการ และเส้นทางสัมพัทธ์ ผู้ให้บริการการนำทางจะต้องได้รับมาจากคลาส System.Management.Automation.Provider.Navigationcmdletprovider
ผู้ให้บริการในตัวอย่างในหัวข้อนี้ใช้ฐานข้อมูล Access เป็นที่เก็บข้อมูล มีวิธีช่วยเหลือและคลาสต่างๆ มากมายที่ใช้ในการโต้ตอบกับฐานข้อมูล สำหรับตัวอย่างที่สมบูรณ์ที่มีวิธีการช่วยเหลือ โปรดดูที่ AccessDBProviderSample05
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับผู้ให้บริการ Windows PowerShell ดูภาพรวมผู้ให้บริการ Windows PowerShell
การใช้วิธีการนำทาง
คลาSystem.Management.Automation.Provider.Navigationcmdletproviderใช้วิธีการที่สนับสนุนคอนเทนเนอร์ที่ซ้อนกัน เส้นทางสัมพัทธ์ และรายการย้าย สำหรับรายการทั้งหมดของวิธีการเหล่านี้ โปรดดูที่วิธี NavigationCmdletProvider
บันทึก
หัวข้อนี้สร้างจากข้อมูลใน QuickStart ของผู้ให้บริการ Windows PowerShell หัวข้อนี้ไม่ครอบคลุมพื้นฐานของวิธีการตั้งค่าโครงการของผู้ให้บริการ หรือวิธีการใช้วิธีการที่สืบทอดมาจากคลาส System.Management.Automation.Provider.Drivecmdletprovider ที่สร้างและลบไดรฟ์ หัวข้อนี้ยังไม่ครอบคลุมถึงวิธีการใช้วิธีการที่ถูกเปิดเผยโดยคลาส System.Management.Automation.Provider.Itemcmdletprovider หรือ System.Management.Automation.Provider.Containercmdletprovider สำหรับตัวอย่างที่แสดงวิธีการใช้ cmdlet ของรายการ โปรดดูที่การเขียนตัวให้บริการรายการ สำหรับตัวอย่างที่แสดงวิธีการใช้ cmdlets ของคอนเทนเนอร์ โปรดดูที่การเขียนผู้ให้บริการคอนเทนเนอร์
ประกาศคลาสผู้ให้บริการ
ประกาศผู้ให้บริการเพื่อรับมาจากคลาส System.Management.Automation.Provider.Navigationcmdletprovider และตกแต่งด้วย System.Management.Automation.Provider.Cmdletproviderattribute
[CmdletProvider("AccessDB", ProviderCapabilities.None)]
public class AccessDBProvider : NavigationCmdletProvider
{
}
การใช้งาน IsItemContainer
เมธอด System.Management.Automation.Provider.Navigationcmdletprovider.Isitemcontainer* ตรวจสอบว่าสินค้าในเส้นทางที่ระบุเป็นคอนเทนเนอร์หรือไม่
protected override bool IsItemContainer(string path)
{
if (PathIsDrive(path))
{
return true;
}
string[] pathChunks = ChunkPath(path);
string tableName;
int rowNumber;
PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
if (type == PathType.Table)
{
foreach (DatabaseTableInfo ti in GetTables())
{
if (string.Equals(ti.Name, tableName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
} // foreach (DatabaseTableInfo...
} // if (pathChunks...
return false;
}
การใช้ GetChildName
กระบวนการSystem.Management.Automation.Provider.Navigationcmdletprovider.Getchildname*วิธีรับคุณสมบัติชื่อของรายการลูกในเส้นทางที่ระบุ หากสินค้าในเส้นทางที่ระบุไม่ใช่รายการลูกของคอนเทนเนอร์ วิธีการนี้ควรส่งคืนเส้นทาง
protected override string GetChildName(string path)
{
if (PathIsDrive(path))
{
return path;
}
string tableName;
int rowNumber;
PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
if (type == PathType.Table)
{
return tableName;
}
else if (type == PathType.Row)
{
return rowNumber.ToString(CultureInfo.CurrentCulture);
}
else
{
ThrowTerminatingInvalidPathException(path);
}
return null;
}
การใช้ GetParentPath
เมธอด System.Management.Automation.Provider.Navigationcmdletprovider.Getparentpath* ได้รับเส้นทางของพาเรนต์ของสินค้าในเส้นทางที่ระบุ หากไอเท็มในพาธที่ระบุเป็นรูทของที่เก็บข้อมูล (ดังนั้นจึงไม่มีพาเรนต์) ดังนั้นเมธอดนี้ควรส่งคืนพาธรูท
protected override string GetParentPath(string path, string root)
{
// If root is specified then the path has to contain
// the root. If not nothing should be returned
if (!String.IsNullOrEmpty(root))
{
if (!path.Contains(root))
{
return null;
}
}
return path.Substring(0, path.LastIndexOf(pathSeparator, StringComparison.OrdinalIgnoreCase));
}
การใช้ MakePath
เมธอด System.Management.Automation.Provider.Navigationcmdletprovider.Makepath* รวมพาธพาเรนต์ที่ระบุและพาธลูกที่ระบุเพื่อสร้างพาธภายในของผู้ให้บริการ (สำหรับข้อมูลเกี่ยวกับประเภทพาธที่ผู้ให้บริการสามารถรองรับได้ ดูภาพรวมของผู้ให้บริการ Windows PowerShell PowerShell โปรแกรมเรียกวิธีนี้เมื่อผู้ใช้เรียก Microsoft.PowerShell.Commands.JoinPathCommand cmdlet
protected override string MakePath(string parent, string child)
{
string result;
string normalParent = NormalizePath(parent);
normalParent = RemoveDriveFromPath(normalParent);
string normalChild = NormalizePath(child);
normalChild = RemoveDriveFromPath(normalChild);
if (String.IsNullOrEmpty(normalParent) && String.IsNullOrEmpty(normalChild))
{
result = String.Empty;
}
else if (String.IsNullOrEmpty(normalParent) && !String.IsNullOrEmpty(normalChild))
{
result = normalChild;
}
else if (!String.IsNullOrEmpty(normalParent) && String.IsNullOrEmpty(normalChild))
{
if (normalParent.EndsWith(pathSeparator, StringComparison.OrdinalIgnoreCase))
{
result = normalParent;
}
else
{
result = normalParent + pathSeparator;
}
} // else if (!String...
else
{
if (!normalParent.Equals(String.Empty) &&
!normalParent.EndsWith(pathSeparator, StringComparison.OrdinalIgnoreCase))
{
result = normalParent + pathSeparator;
}
else
{
result = normalParent;
}
if (normalChild.StartsWith(pathSeparator, StringComparison.OrdinalIgnoreCase))
{
result += normalChild.Substring(1);
}
else
{
result += normalChild;
}
} // else
return result;
}
การใช้ NormalizeRelativePath
วิธีการ System.Management.Automation.Provider.Navigationcmdletprovider.Normalizerelativepath* ใช้พารามิเตอร์ path และ basepath และส่งกลับเส้นทางปกติที่เทียบเท่ากับ path และสัมพันธ์กับพารามิเตอร์ basepath
protected override string NormalizeRelativePath(string path,
string basepath)
{
// Normalize the paths first
string normalPath = NormalizePath(path);
normalPath = RemoveDriveFromPath(normalPath);
string normalBasePath = NormalizePath(basepath);
normalBasePath = RemoveDriveFromPath(normalBasePath);
if (String.IsNullOrEmpty(normalBasePath))
{
return normalPath;
}
else
{
if (!normalPath.Contains(normalBasePath))
{
return null;
}
return normalPath.Substring(normalBasePath.Length + pathSeparator.Length);
}
}
การใช้ MoveItem
เมธอด System.Management.Automation.Provider.Navigationcmdletprovider.Moveitem* ย้ายสินค้าจากเส้นทางที่ระบุไปยังเส้นทางปลายทางที่ระบุ กลไกจัดการ PowerShell เรียกใช้วิธีนี้เมื่อผู้ใช้เรียก Microsoft.PowerShell.Commands.MoveItemCommand cmdlet
protected override void MoveItem(string path, string destination)
{
// Get type, table name and rowNumber from the path
string tableName, destTableName;
int rowNumber, destRowNumber;
PathType type = GetNamesFromPath(path, out tableName, out rowNumber);
PathType destType = GetNamesFromPath(destination, out destTableName,
out destRowNumber);
if (type == PathType.Invalid)
{
ThrowTerminatingInvalidPathException(path);
}
if (destType == PathType.Invalid)
{
ThrowTerminatingInvalidPathException(destination);
}
if (type == PathType.Table)
{
ArgumentException e = new ArgumentException("Move not supported for tables");
WriteError(new ErrorRecord(e, "MoveNotSupported",
ErrorCategory.InvalidArgument, path));
throw e;
}
else
{
OdbcDataAdapter da = GetAdapterForTable(tableName);
if (da == null)
{
return;
}
DataSet ds = GetDataSetForTable(da, tableName);
DataTable table = GetDataTable(ds, tableName);
OdbcDataAdapter dda = GetAdapterForTable(destTableName);
if (dda == null)
{
return;
}
DataSet dds = GetDataSetForTable(dda, destTableName);
DataTable destTable = GetDataTable(dds, destTableName);
DataRow row = table.Rows[rowNumber];
if (destType == PathType.Table)
{
DataRow destRow = destTable.NewRow();
destRow.ItemArray = row.ItemArray;
}
else
{
DataRow destRow = destTable.Rows[destRowNumber];
destRow.ItemArray = row.ItemArray;
}
// Update the changes
if (ShouldProcess(path, "MoveItem"))
{
WriteItemObject(row, path, false);
dda.Update(dds, destTableName);
}
}
}
ดูเพิ่มเติม
การเขียนผู้ให้บริการคอนเทนเนอร์
ภาพรวมผู้ให้บริการ Windows PowerShell