[英]PowerShell Script only works from PowerShell Console and not from ISE
I am very very new to PowerShell. 我对PowerShell非常陌生。 I am trying to delete a set of Test Results files in folders through PowerShell.
我正在尝试通过PowerShell删除文件夹中的一组“测试结果”文件。 When I run the code through PowerShell console, it works.
当我通过PowerShell控制台运行代码时,它可以工作。 The same code doesn't work through ISE.
相同的代码不适用于ISE。 Below is the code :
下面是代码:
Set-Location -Path "Path";
$testResultsFolder = Get-ChildItem -Include *TestResults -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName;
$date = Get-Date;
$limit = 30;
foreach($testResult in $testResultsFolder)
{
$resultsPath = Get-Item $testResult.FullName;
Get-ChildItem -Path $resultsPath -Recurse -Force | Where-Object { -not $_.PSIsContainer -and $date.Subtract($_.LastWriteTime).Days -gt $limit } | Remove-Item -Force;
}
This works fine from PowerShell console but not from ISE. 在PowerShell控制台上运行正常,但在ISE上运行不正常。 The ISE does not show any error.
ISE不显示任何错误。 It says 'Completed'.
它说“完成”。 Can somebody throw some light on what I am doing wrong?
有人可以阐明我做错了什么吗?
Try this: 尝试这个:
Delete.ps1 Delete.ps1
#Requires -Version 3
$Path = 'C:\Temp'
$Cleanup = (Get-ChildItem -Path $Path -Include '*TestResults' -Recurse -Directory).FullName
ForEach ($Folder in $Cleanup)
{
Get-ChildItem -Path $Folder -File -Recurse -Force |
Where-Object { $PSItem.LastWriteTime -lt (Get-Date).AddDays(-30) } |
Remove-Item -Force
}
Using Set-Location
can result in inconsistent results, especially if you're using dynamic variables. 使用
Set-Location
可能导致结果不一致,尤其是在使用动态变量的情况下。 I'd suggest using a variable target so you're getting consistent results. 我建议使用可变目标,以便获得一致的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.