找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 12341|回复: 1

[问题求助] 怎样用powershell静默格式化操作

[复制链接]
发表于 2023-9-15 19:26:45 | 显示全部楼层 |阅读模式
在下面链接看到一个文章, 调用的系统API实现了静默格式化, 我把代码用gpt转成了powershell, 但是不成功
https://blog.csdn.net/dpsying/article/details/18195835
  1. function FormatDisk
  2. {
  3.         param (
  4.                 [char]$Disk
  5.         )
  6.        
  7.         $hDll = Add-Type -Name SHFormatDrive -MemberDefinition @"
  8.         [DllImport("Shell32.dll")]
  9.         public static extern int SHFormatDrive(IntPtr hWnd, uint drive, uint fmtID, uint options);
  10. "@
  11.         if ($hDll -eq $null)
  12.         {
  13.                 return
  14.         }
  15.        
  16.         $drive = [System.Char]::ToLower($Disk) - [System.Char]::ToLower('A')
  17.         $result = $hDll::SHFormatDrive([IntPtr]::Zero, $drive, 0, 0)
  18.        
  19.         if ($result -eq 0)
  20.         {
  21.                 Write-Host "格式化成功"
  22.         }
  23.         elseif ($result -eq 1)
  24.         {
  25.                 Write-Host "用户取消了格式化操作"
  26.         }
  27.         elseif ($result -eq 2)
  28.         {
  29.                 Write-Host "指定驱动器无法格式化"
  30.         }
  31. }
  32. FormatDisk "G"
复制代码
发表于 2023-9-15 22:16:27 | 显示全部楼层
看下面的
https://learn.microsoft.com/zh-c ... _core-shformatdrive

用format命令不行么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 23:14 , Processed in 0.018761 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表