在下面链接看到一个文章, 调用的系统API实现了静默格式化, 我把代码用gpt转成了powershell, 但是不成功
https://blog.csdn.net/dpsying/article/details/18195835- function FormatDisk
- {
- param (
- [char]$Disk
- )
-
- $hDll = Add-Type -Name SHFormatDrive -MemberDefinition @"
- [DllImport("Shell32.dll")]
- public static extern int SHFormatDrive(IntPtr hWnd, uint drive, uint fmtID, uint options);
- "@
- if ($hDll -eq $null)
- {
- return
- }
-
- $drive = [System.Char]::ToLower($Disk) - [System.Char]::ToLower('A')
- $result = $hDll::SHFormatDrive([IntPtr]::Zero, $drive, 0, 0)
-
- if ($result -eq 0)
- {
- Write-Host "格式化成功"
- }
- elseif ($result -eq 1)
- {
- Write-Host "用户取消了格式化操作"
- }
- elseif ($result -eq 2)
- {
- Write-Host "指定驱动器无法格式化"
- }
- }
- FormatDisk "G"
复制代码
|