这个简单的PowerShell脚本"填写" USB闪存盘的坏扇区。但我需要用包含0x00(所有位为零)和0x255(所有位1)的文件填充驱动器。如何在PowerShell中使用hex?
function filler {
Param( [byte]$hex )
$filearray = @()
$count = 1
$freespace = Get-PSDrive H
$maxfiles = [int]($freespace.Free / 1048576)
do {
$randomnum = Get-Random -Minimum 100000000 -Maximum 999999999
"$hex" * 1048576 | Out-File $randomnum
$filecontent = Get-Content $randomnum -Raw
if ($filecontent -notcontains ('$hex')) {
# do nothing because the content is incorrect
} else {
$filearray += $randomnum
}
$count++
} while ($count -le $maxfiles)
foreach ($filename in $filearray) {
Remove-Item $filename
}
}
filler -hex 0x00
filler -hex 0xFF
答案 0 :(得分:1)
字符串"0x00"
与0x00
所代表的数值不同。
确保在尝试向/从文件读取和写入原始数据时指定-Encoding Byte
:
,$hex * 1048576 | Set-Content $randomnum -Encoding Byte
同样,从文件中读取时:
$filecontent = Get-Content $randomnum -Encoding Byte
if($filecontent |Where-Object {$_ -notin @(0x00,0xFF)}){
# do nothing
}