Powershell 锁定第一个和最后一个文件,如果不关闭 powershell ISE,我无法删除它们
Powershell locking first and last files and I cannot delete them without close powershell ISE
所以我有一个 Powershell ISE(尝试 运行 作为管理员和 w/o),它创建了虚拟 .mdf 文件,其中有 50 个
问题是它保留在第一个和最后一个上,因此复制或删除它们不起作用...
这是我的脚本
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)
}
当方法上省略 ()
时,它 returns 重载定义。所以你试图关闭文件的行只需要 ()
.
$file.Close()
如果您看到 OverloadDefinitions
曾返回,那就是要查找的内容。
所以我有一个 Powershell ISE(尝试 运行 作为管理员和 w/o),它创建了虚拟 .mdf 文件,其中有 50 个
问题是它保留在第一个和最后一个上,因此复制或删除它们不起作用...
这是我的脚本
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)
}
当方法上省略 ()
时,它 returns 重载定义。所以你试图关闭文件的行只需要 ()
.
$file.Close()
如果您看到 OverloadDefinitions
曾返回,那就是要查找的内容。