New-PSDrive
建立與物品資料庫中某位置相關的臨時且持久硬碟。
語法
All
New-PSDrive
[-Name] <String>
[-PSProvider] <String>
[-Root] <String>
[-Description <String>]
[-Scope <String>]
[-Persist]
[-Credential <PSCredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
New-PSDrive 指令檔會建立暫時且持久的磁碟,這些磁碟會映射到或關聯到資料儲存中的某個位置,例如網路磁碟機、本地電腦的目錄或登錄檔金鑰;以及與遠端電腦檔案系統位置相關聯的持久Windows映射網路磁碟機。
暫存磁碟只存在於目前的 PowerShell 會話中,以及你在目前會話中建立的會話中。 它們可以有 PowerShell 中有效的任何名稱,並且可以映射到任何本地或遠端資源。 你可以使用臨時的 PowerShell 磁碟機存取相關資料儲存庫的資料,就像使用任何映射網路磁碟機一樣。 你可以透過使用 Set-Location,將硬碟位置變更,並透過使用 Get-Item 或 Get-ChildItem存取磁碟內容。
由於暫存磁碟僅由 PowerShell 已知,無法透過檔案總管、Windows管理儀器(WMI)、元件物件模型(COM)、Microsoft .NET 框架或像 net use 這類工具存取它們。
PowerShell 3.0 新增 New-PSDrive 了以下功能:
- 映射網路磁碟機。 你可以使用 的
New-PSDrive參數來建立Windows映射的網路磁碟機。 與臨時 PowerShell 磁碟不同,Windows 映射的網路磁碟並非針對會話特定。 這些檔案會儲存在Windows中,並可透過標準Windows工具管理,例如檔案總管和 net use。 映射的網路磁碟機必須有磁碟機代號名稱,並連接到遠端檔案系統位置。 當你的指令是本地範圍設定,沒有 dot-sourcing,Persist 參數不會持續存在 PSDrive 建立超出指令執行範圍的範圍。 如果你是在腳本裡執行New-PSDrive,並且想讓硬碟無限期持續存在,你必須用點源(dot-source)來處理腳本。 為了達到最佳效果,要強制新硬碟無限期持續存在,請在指令中加入 Scope 參數,並將其值設為 全域。 欲了解更多點源資訊,請參見 about_Scripts。 - 外接硬碟。 當外接硬碟連接到電腦時,PowerShell 會自動將代表新硬碟的 PSDrive 加入檔案系統。 你不需要重啟 PowerShell。 同樣地,當外接硬碟從電腦斷開時,PowerShell 會自動刪除代表該硬碟的 PSDrive 。
- 通用命名慣例(UNC)路徑的憑證。
當 根參數的 值為 UNC 路徑(例如 \\Server\Share), 憑證參數值 中指定的憑證用於建立 PSDrive。 否則,當你建立新的檔案系統磁碟時, 憑證 功能就沒什麼用。
部分程式碼範例會使用濺寫來縮短行長並提升可讀性。 如需詳細資訊,請參閱 about_Splatting。
Note
除非你使用 Scope 參數,否則 PSDrive 會建立在執行指令的範圍 New-PSDrive 中。
範例
範例 1:建立一個暫存磁碟,映射到網路共享
這個範例會建立一個暫存的 PowerShell 磁碟,並對應到網路共享。
New-PSDrive -Name "Public" -PSProvider "FileSystem" -Root "\\Server01\Public"
Name Provider Root
---- -------- ----
Public FileSystem \\Server01\Public
New-PSDrive 使用 Name 參數來指定 PowerShell 磁碟機的命名 Public ,並使用 PSProvider 參數來指定 PowerShell FileSystem 提供者。
根參數指定網路共享的 UNC 路徑。
要查看 PowerShell 會話的內容: Get-ChildItem -Path Public:
範例 2:建立一個暫存磁碟,映射到本地目錄
此範例建立一個暫存的 PowerShell 磁碟機,提供對本地電腦目錄的存取。
$parameters = @{
Name = "MyDocs"
PSProvider = "FileSystem"
Root = "C:\Users\User01\Documents"
Description = "Maps to my My Documents folder."
}
New-PSDrive @parameters
Name Provider Root
---- -------- ----
MyDocs FileSystem C:\Users\User01\Documents
Splatting 會產生參數鍵和值。
名稱參數指定磁碟機名稱 MyDocs。
PSProvider 參數指定了 PowerShell FileSystem 提供者。
Root 指定本地電腦的目錄。
描述參數描述硬碟的功能。
New-PSDrive 利用 splat 參數來建立 MyDocs 硬碟。
要查看 PowerShell 會話的內容: Get-ChildItem -Path MyDocs:
範例 3:建立暫存磁碟以儲存登錄檔金鑰
此範例建立一個臨時的 PowerShell 磁碟機,提供登錄檔金鑰的存取權限。 它會建立一個名為 MyCompany 的磁碟,並對應到 HKLM:\Software\MyCompany 登錄檔金鑰。
New-PSDrive -Name "MyCompany" -PSProvider "Registry" -Root "HKLM:\Software\MyCompany"
Name Provider Root
---- -------- ----
MyCompany Registry HKLM:\Software\MyCompany
New-PSDrive 使用 Name 參數來指定 PowerShell 磁碟機的命名 MyCompany ,並使用 PSProvider 參數來指定 PowerShell Registry 提供者。
Root 參數指定登錄檔位置。
要查看 PowerShell 會話的內容: Get-ChildItem -Path MyCompany:
範例 4:使用憑證建立持久映射網路磁碟
這個例子是對應一個用網域服務帳號憑證認證的網路磁碟機。 欲了解更多關於儲存憑證的 PSCredential 物件,以及密碼如何以 SecureString 形式儲存,請參閱 憑證 參數的說明。
$cred = Get-Credential -Credential Contoso\ServiceAccount
New-PSDrive -Name "S" -Root "\\Server01\Scripts" -Persist -PSProvider "FileSystem" -Credential $cred
Net Use
Status Local Remote Network
---------------------------------------------------------
OK S: \\Server01\Scripts Microsoft Windows Network
Note
記得,如果你在腳本中使用上述片段,請將 Scope 參數設為「Global」,以確保硬碟能持續存在於目前的範圍之外。
變 $cred 數儲存一個 PSCredential 物件,內含服務帳號的憑證。
Get-Credential 它會提示你輸入儲存在 SecureString 中的密碼。
New-PSDrive 透過使用多個參數建立映射網路磁碟機。
Name 指定Windows接受的 S 磁碟代號。 而 Root\\Server01\Scripts 定義為遠端電腦上的位置。
Persist 會建立一個Windows映射的網路磁碟,並儲存在本地電腦上。
PSProvider 會指定提供者。FileSystem
憑證 會利用變 $cred 數取得服務帳號的憑證來進行驗證。
映射磁碟可在本地電腦的 PowerShell 工作階段、檔案總管及 net use 等工具中查看。 要查看 PowerShell 會話的內容: Get-ChildItem -Path S:
範例 5:建立持久性與暫存硬碟
此範例展示了持久映射網路磁碟與映射至同一網路共享的臨時 PowerShell 磁碟機之間的差異。
如果你關閉 PowerShell 會話再開啟新會話, PSDrive: 暫時磁碟就不存在,但持久 X: 磁碟是可用的。 在決定用哪種方法來映射網路磁碟時,請考慮你將如何使用這顆磁碟機。 例如,硬碟是否必須是持久的,以及硬碟是否必須對其他 Windows 功能可見。
# Create a temporary PowerShell drive called PSDrive:
# that's mapped to the \\Server01\Public network share.
New-PSDrive -Name "PSDrive" -PSProvider "FileSystem" -Root "\\Server01\Public"
# Use the Persist parameter of New-PSDrive to create the X: mapped network drive,
# which is also mapped to the \\Server01\Public network share.
New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public"
# Now, you can use the Get-PSDrive drive cmdlet to examine the two drives.
# The drives appear to be the same, although the network share name appears only
# in the root of the PSDrive: drive.
Get-PSDrive -Name "PSDrive", "X"
Name Provider Root
---- -------- ----
PSDrive FileSystem \\Server01\public
X FileSystem X:\
# Get-Member cmdlet shows that the drives have the same object type,
# System.Management.Automation.PSDriveInfo.
Get-PSDrive "PSDrive", "x" | Get-Member
TypeName: System.Management.Automation.PSDriveInfo
Name MemberType Definition
---- ---------- ----------
CompareTo Method System.Int32 CompareTo(PSDriveInfo drive),
Equals Method System.Boolean Equals(Object obj),
GetHashCode Method System.Int32 GetHashCode()
...
# Net Use and Get-CimInstance for the Win32_LogicalDisk class,
# and Win32_NetworkConnection class find only the persistent X: drive.
# PowerShell temporary drives are known only to PowerShell.
Net Use
Get-CimInstance Win32_LogicalDisk | Format-Table -Property DeviceID
Get-CimInstance Win32_NetworkConnection
Status Local Remote Network
--------------------------------------------------------
OK X: \\contoso-pc\data Microsoft Windows Network
deviceid
--------
C:
D:
X:
LocalName RemoteName ConnectionState Status
--------- ---------- --------------- ------
X: \\products\public Disconnected Unavailable
範例 6:在腳本中建立持久磁碟
PSDrive 是在執行指令 New-PSDrive 的範圍內建立的。 當指令在腳本中執行時,磁碟機映射是本地於腳本的。 當腳本退出時,該硬碟就不再可用。
New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public" -Scope Global
為了確保硬碟在腳本之外可用,你必須使用 Scope 參數在 全域 範圍內建立硬碟。
參數
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 參見 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Credential
指定具有執行此動作許可權的用戶帳戶。 預設值為目前的使用者。
自 PowerShell 3.0 起,當 Root 參數的值是 UNC 路徑時,你可以用憑證來建立檔案系統磁碟機。
輸入使用者名稱,例如 User01 或 Domain01\User01,或輸入 Cmdlet 所產生的 Get-Credential 物件。 如果您輸入使用者名稱,系統會提示您輸入密碼。
認證會儲存在 PSCredential 物件中,密碼會儲存為 secureString 。
Note
如需 SecureString 數據保護的詳細資訊,請參閱 SecureString 有多安全?。
參數屬性
| 類型: | PSCredential |
| 預設值: | Current user |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Description
指定硬碟的簡短文字描述。 輸入任何字串。
要查看所有會話中磁碟的描述,。 Get-PSDrive | Format-Table Name, Description
要查看特定硬碟的描述,請輸入 (Get-PSDrive <DriveName>).Description。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Name
指定新硬碟的名稱。 對於持久映射的網路磁碟機,請使用磁碟代號。 對於臨時的 PowerShell 硬碟,你不只限於磁碟機代號,可以使用任何有效的字串。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Persist
表示此指令檔會建立一個 Windows 映射的網路磁碟機。 Persist 參數僅在 Windows 上可用。
映射的網路磁碟會儲存在 Windows 的本地電腦上。 它們是持久性的,不是針對特定工作階段的,可以在檔案總管和其他工具中查看和管理。
當你在本地設定指令範圍時,沒有網路外見, Persist 參數不會持久化 PSDrive 的建立,超出你執行指令的範圍範圍。 如果你在腳本裡執行 New-PSDrive ,且想讓新硬碟無限期地持續存在,你必須用點源(dot-source)這個腳本。 為了達到最佳效果,要強制新硬碟持續存在,請將 Scope 參數的值指定為 Global,並在指令中加入 Persist。
磁碟機名稱必須是字母,例如 D 或 E。 根 參數的 值必須是另一台電腦的 UNC 路徑。
PSProvider 參數的值必須為 FileSystem。
要斷開Windows映射網路磁碟機,請使用 Remove-PSDrive cmdlet。 當你斷開 Windows 映射的網路磁碟時,該映射會永久從電腦中刪除,而不只是從當前工作階段刪除。
映射的網路磁碟機是針對特定使用者帳號的。 在提升級會話中建立的映射硬碟,或使用其他使用者憑證的會話,在使用不同憑證啟動的會話中是看不到的。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-PSProvider
指定支援此類硬碟的 PowerShell 提供者。
例如,若硬碟與網路分享或檔案系統目錄相關聯,PowerShell 提供者為 FileSystem。 如果磁碟機與登錄檔金鑰相關聯,則提供者為 Registry。
臨時 PowerShell 磁碟機可以與任何 PowerShell 提供者綁定。 映射網路磁碟只能與 FileSystem 提供者綁定。
要查看 PowerShell 會話中的提供者清單,請使用 Get-PSProvider cmdlet。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Root
指定 PowerShell 磁碟機對應的資料儲存位置。
例如,指定網路共享,如 \\Server01\Public,本地目錄如 C:\Program Files,或登錄檔金鑰,如 HKLM:\Software\Microsoft。
臨時 PowerShell 磁碟可綁定在任何支援的供應商磁碟上的本地或遠端位置。 映射的網路磁碟機只能與遠端電腦上的檔案系統位置關聯。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 2 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Scope
指定驅動器的示波器。 此參數的可接受值為: 全域(Global)、 本地(Local)和 腳本(Script),或相對於目前範圍的數字。 瞄準鏡編號從0到瞄準鏡數量。 目前的範圍號為 0,父值為 1。 如需詳細資訊,請參閱 about_Scopes。
參數屬性
| 類型: | String |
| 預設值: | Local |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 該 Cmdlet 未被執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 威斯康辛 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
None
你無法將物件管道傳送到這個指令小子
輸出
PSDriveInfo
此 cmdlet 會回傳一個代表所建立磁碟機的 PSDriveInfo 物件。
備註
PowerShell 包含下列 Get-PSDrive的別名:
- 所有平臺:
ndr
- 窗戶:
mount
New-PSDrive 的設計目的是要處理任何提供者所公開的數據。 若要列出會話中可用的供應商,請使用 Get-PSProvider。 欲了解更多提供者資訊,請參閱 about_Providers。
映射的網路磁碟機是針對特定使用者帳號的。 在提升級會話中建立的映射硬碟,或使用其他使用者憑證的會話,在使用不同憑證啟動的會話中是看不到的。