windows - Powershell 锁定第一个和最后一个文件,如果不关闭 powershell ISE,我无法删除它们

标签 windows powershell powershell-ise

所以我有一个 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/

相关文章:

使用 CMD/C 暂停时,Powershell ISE 中断

powershell - 如何在 PowerShell ISE 中更改 Read-Host 提示的 WindowTitle?

.net - 线程是否可以在临界区中间被抢占?

c++ - 变量已在 .obj 中定义,但没有循环包含

c++ - 如何使用 MinGW gdb 调试器在 Windows 中调试 C++ 程序?

windows - 在现有的 emacs 框架中打开文件 (Windows)

powershell - 我无法使用Powershell更改多个文件的扩展名

json - 使用PowerShell遍历JSON文件

iis - 如何使用PowerShell停止IIS?

powershell - Powershell错误在ISE中正确输出,但在命令行执行中不正确