所以我有一个 Powershell ISE(尝试以管理员身份和 w/o 运行)并且创建了虚拟 .mdf 文件,其中有 50 个
问题是它保留在 1st 和 last 上,所以复制或删除它们是行不通的......
这是我的脚本
param(
$amount = 50 # $(throw "Please give an amount of files to be created")
, $size = 5 # $(throw "Please give a the size of the files")
, $folder = "C:\dev\powershell\oldlocation" # $(throw "Please give an output folder wehere the files need to be created")
, $name = 'db' # $null
, $extension = '.mdf' # $null .mdf / .ldf
)
CLS
# Check for input
if(Test-Path $folder)
{
if($name -eq $null)
{
Write-Host "No filename given. Using default setting 'dummy'" -ForegroundColor Yellow
$name = 'dummy'
}
if($extension -eq $null)
{
Write-Host "No filename extension given. Using default setting '.txt'" -ForegroundColor Yellow
$extension = 'txt'
}
elseif($extension -contains '.')
{
$extension = $extension.Substring(($extension.LastIndexOf(".") + 1), ($extension.Length - 1))
}
for($i = 1; $i -le $amount; $i++)
{
$path = $folder + '\' + $name + '_' + $i + '.' + $extension
$file = [io.file]::Create($path)
$file.SetLength($size)
$file.Close
sleep 0.5
}
}
else{
Write-Host "The folder $folder doesn't exist" -ForegroundColor Red
Exit(0)
}
最佳答案
当在方法上省略 ()
时,它返回重载定义。因此,您尝试关闭文件的行只需要 ()
。
$file.Close()
如果您看到 OverloadDefinitions
曾经返回,那就是要查找的内容。
关于windows - Powershell 锁定第一个和最后一个文件,如果不关闭 powershell ISE,我无法删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549469/