我编写了一个脚本,用于挂载(附加)一个血管内皮细胞使用磁盘分区,清除一些系统文件,然后卸载(分离)它。它使用 foreach 循环,应该使用相同的驱动器号清理多个 VHD。但是,在第一个 VHD 之后,它失败了。我还注意到,当我尝试使用 diskpart 手动连接 VHD 时,diskpart 成功了,磁盘管理器显示具有正确驱动器号的磁盘,但在同一个 PoSH 实例中,我无法连接(设置位置)到该驱动器。如果我第一次打开 PoSH 时执行手动 diskpart,我可以连接和分离所有我想要的,并且每次都会获得驱动器号。我需要做些什么来重置脚本中的 diskpart 吗?这是我正在使用的脚本的片段。
function Mount-VHD {
[CmdletBinding()]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
[string]$Path,
[Parameter(Position=1,Mandatory=$false,ValueFromPipeline=$false)]
[string]$DL,
[string]$DiskpartScript = "$env:SystemDrive\DiskpartScript.txt",
[switch]$Rescan
)
begin {
function InvokeDiskpart {
Diskpart.exe /s $DiskpartScript
}
## Validate Operating System Version ##
if (Get-WmiObject win32_OperatingSystem -Filter "Version < '6.1'") {throw "The script operation requires at least Windows 7 or Windows Server 2008 R2."}
}
process{
## Diskpart Script Content ## Here-String statement purposefully not indented ##
@"
$(if ($Rescan) {'Rescan'})
Select VDisk File="$Path" `nAttach VDisk
Exit
"@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
InvokeDiskpart
Start-Sleep -Seconds 3
@"
Select VDisk File="$Path"`nSelect partition 1 `nAssign Letter="$DL"
Exit
"@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
InvokeDiskpart
}
end {
Remove-Item -Path $DiskpartScript -Force ; ""
Write-Host "The VHD ""$Path"" has been successfully mounted." ; ""
}
}
function Dismount-VHD {
[CmdletBinding()]
param (
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false)]
[string]$Path,
[switch]$Remove,
[switch]$NoConfirm,
[string]$DiskpartScript = "$env:SystemDrive\DiskpartScript.txt",
[switch]$Rescan
)
begin {
function InvokeDiskpart {
Diskpart.exe /s $DiskpartScript
}
function RemoveVHD {
switch ($NoConfirm) {
$false {
## Prompt for confirmation to delete the VHD file ##
"" ; Write-Warning "Are you sure you want to delete the file ""$Path""?"
$Prompt = Read-Host "Type ""YES"" to continue or anything else to break"
if ($Prompt -ceq 'YES') {
Remove-Item -Path $Path -Force
"" ; Write-Host "VHD ""$Path"" deleted!" ; ""
} else {
"" ; Write-Host "Script terminated without deleting the VHD file." ; ""
}
}
$true {
## Confirmation prompt suppressed ##
Remove-Item -Path $Path -Force
"" ; Write-Host "VHD ""$Path"" deleted!" ; ""
}
}
}
## Validate Operating System Version ##
if (Get-WmiObject win32_OperatingSystem -Filter "Version < '6.1'") {throw "The script operation requires at least Windows 7 or Windows Server 2008 R2."}
}
process{
## DiskPart Script Content ## Here-String statement purposefully not indented ##
@"
$(if ($Rescan) {'Rescan'})
Select VDisk File="$Path"`nDetach VDisk
Exit
"@ | Out-File -FilePath $DiskpartScript -Encoding ASCII -Force
InvokeDiskpart
Start-Sleep -Seconds 10
}
end {
if ($Remove) {RemoveVHD}
Remove-Item -Path $DiskpartScript -Force ; ""
}
}
答案1
我看不到你的脚本失败的确切点,但是你提到的 Set-Location 测试让我想起了我在 PoSH 中尝试在脚本中重复映射/取消映射/映射位置时遇到的问题。
https://stackoverflow.com/questions/10994979/net-use-only-works-once-in-powershell
简短使用
FILESYSTEM::X:\
代替X:\
编辑 : 好吧,我实际上花了 30 秒阅读你的脚本。我会把
$Path = FILESYSTEM::$Path
在每个函数的末尾。
答案2
我知道这不是直接的答案,但你试过不把它挂载到驱动器号上吗?改用该assign mount=<PATH>
选项并操纵你的清理脚本以在该子目录中工作。