简体   繁体   English

PowerShell脚本仅可从PowerShell控制台运行,而不能从ISE运行

[英]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.

相关问题 Powershell脚本可在ISE中使用,但不能在控制台中使用 - Powershell script works in ISE but not in console 我的 PowerShell 脚本仅在从 ISE 运行时有效 - My PowerShell script only works when running from ISE 脚本可以在Powershell ISE中工作,但不能在PowerShell控制台中工作吗? - Script Works in Powershell ISE But not In PowerShell Console? Powershell 脚本适用于 ISE/控制台,但不适用于任务计划程序 - Powershell script works in ISE / Console but not Task Scheduler Powershell脚本通过ISE起作用,但不能通过控制台起作用 - Powershell script works through ISE but not the console PowerShell-脚本可以在ISE中完美运行,但不能在普通控制台中运行 - PowerShell - Script works perfectly in the ISE, but not in the normal console Powershell excel 到 csv 脚本在 ISE 中有效,但在 Z2F2D399F0EA8844733B 控制台中无效 - Powershell excel to csv script works in ISE but not in powershell console Powershell 从 ISE 控制台运行的脚本似乎没有结束 - Powershell Script run from ISE console seems not to end 用于显示通知气球和执行操作的PowerShell脚本仅适用于ISE gui,而不适用于命令行 - PowerShell script to display Notification balloon and take action only works in ISE gui, not from commandline 从控制台调用ISE样式的PowerShell? - Call ISE-style PowerShell from console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM