我在下面有一個 powershell 腳本,它在邏輯上應該可以作業,但會拋出以下錯誤,提示“名稱為“E”的驅動器不存在。”但實際上它存在。當我使用變數輸入驅動器時會出現此錯誤,但是如果我手動輸入路徑驅動器,如“E:”它會正常作業。不知道我做錯了什么。
Get-ChildItem : Cannot find drive. A drive with the name 'E' does not exist.
At line:24 char:10
$list = Get-ChildItem -path $CDDriveLetterToText
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (E:String) [Get-ChildItem], DriveNotFoundExcepti
on
FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
我的代碼如下。
Function Image-Windows10 () {
$CDDrives = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -Property name
$FlashDrives = get-wmiobject win32_diskdrive | where {$_.InterfaceType -eq 'SCSI'} | select-object -property index, size
[int]$NumberOfFlashDrives=$FlashDrives.Count
[int]$NumberOfCDDrives=$CDDrives.Count
$CDDriveLetterToText = Out-String -inputObject $CDDrives.Get($NumberOfCDDrives-1)
$CDDriveLetterToText = $CDDriveLetterToText.Replace("name","").Replace("----","").Replace("`n","").Replace(" ","")
$list = Get-ChildItem -Path $CDDriveLetterToText
}
Image-Windows10
uj5u.com熱心網友回復:
而不是選擇您可能想要的屬性名稱DriveLetter。該引數-ExpandProperty將回傳指定值的陣列 - 因此無需操作字串'
# get all drive letters from devices of type 'CDRom'
$driveArray = Get-WmiObject win32_volume | where {$_.DriveType -eq '5'} | Select-Object -ExpandProperty DriveLetter
# this array can then be iterated like
foreach($drive in $driveArray) {
$list = Get-ChildItem -Path $drive
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323344.html
標籤:电源外壳
上一篇:PowerShell多執行緒
