gpt4 book ai didi

Reworking powershell line to handle no items(改造PowerShell生产线,不处理任何物品)

转载 作者:bug小助手 更新时间:2023-10-25 13:58:51 28 4
gpt4 key购买 nike



Given this code block

给出此代码块


    Get-Childitem -Filter '*.bak' -Recurse -exclude *baseline*  |  Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |  foreach-object 
{

i am receiving a prompt if there are no files that match the criteria.

如果没有符合条件的文件,我会收到提示。


cmdlet ForEach-Object at command pipeline position 3
Supply values for the following parameters:
Process[0]:

Cmdlet ForEach-位于命令管道位置3的对象为以下参数提供值:Process[0]:


How would I rewrite this code to stop the script from hanging, waiting for user input?

我如何重写这段代码以阻止脚本挂起,等待用户输入?


In response to the filimonic...

作为对电影的回应..。


write-host ""
write-host $DBA_BackupFileShare_NME
cd $DBA_BackupFileShare_NME\SQLServerBackup
# @( Get-Childitem -Filter '*.bak' -Recurse -exclude *baseline* ) |
# Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
# ForEach-Object
# {
# write-host ("{0}" -f $_.FullName)
## [System.IO.File]::Delete($_.FullName)
# }

[System.IO.Directory]::EnumerateFiles('.', '*.bak',System.IO.SearchOption]::AllDirectories <# Means Recursive#>) | Where-Object {[System.IO.Path]::GetFileName($_) -notcontains baseline'}
ForEach-Object { Get-Item -Path $_ } |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
ForEach-Object
{
write-host ("{0}" -f $_.FullName)
# [System.IO.File]::Delete($_.FullName)
}

更多回答

We cant see what your ForEach-Object is doing or if it is even needed. You need to also add that missing part of your code.

我们看不到您的ForEach对象正在做什么,也看不到是否需要它。您还需要添加代码中缺失的部分。

Given the error that you have given, it sounds like you need to provide a scriptblock for your ForEach-Object call. You should be getting that error regardless of if you have files found or not. Or remove the |ForEach-Object from the end of that line.

鉴于您给出的错误,似乎需要为ForEach-Object调用提供一个ScriptBlock。无论是否找到文件,您都应该收到该错误。或者从该行的末尾删除|ForEach-Object。

Santiago - the for each block work is to delete the file.

圣地亚哥--每个块的工作就是删除文件。

MadTech - I do not receive the error when there are files that are to be deleted.

Madtech-当存在要删除的文件时,我未收到该错误。

优秀答案推荐

Try forcing Get-Childitem be an array by enclosing it into @( ) :

尝试通过将Get-ChildItem封装在@()中将其强制为数组:


@( Get-Childitem -Filter '*.bak' -Recurse -exclude *baseline* ) |  
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
ForEach-Object {[System.IO.File]::Delete($_.FullName)}

Or use EnumerateFiles

或使用EnumerateFiles


[System.IO.Directory]::EnumerateFiles('.', '*.bak', [System.IO.SearchOption]::AllDirectories <# Means Recursive#>) | 
Where-Object {[System.IO.Path]::GetFileName($_) -notcontains 'baseline'} |
ForEach-Object { Get-Item -Path $_ } |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} |
ForEach-Object {[System.IO.File]::Delete($_.FullName)}

And also you can run script as powershell.exe -NonInteractive -File c:\script.ps1 which will throw error instead of waiting user input.

您还可以像Powershell.exe-non interactive-Filec:\script.ps1那样运行脚本,这将抛出错误,而不是等待用户输入。


更多回答

first code block returns this .. cmdlet ForEach-Object at command pipeline position 2 Supply values for the following parameters: Process[0]:

第一个代码块返回这个..命令管道位置2处的cmdlet ForEach-Object为以下参数提供值:Process[0]:

second block returns same results, but parameter 4

第二个块返回相同的结果,但参数4

I think you missing { in | ForEach-Object { somewhere

我认为您在|ForEach-Object{某处遗漏了

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com