簡體   English   中英

在批處理文件中調用Powershell命令

[英]Invoking Powershell commands in a batch file

有人可以幫我編寫下面的Powershell命令作為批處理文件嗎? 我將通過命令提示符執行文件。 我沒有選擇創建ps1文件並通過命令提示符執行它的選項。

謝謝。

$limit = (Get-Date).AddDays(-4)   
$path = "T:\FolderName"   

Get-ChildItem -Path $path -Recurse -Force -include *.txt | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

Get-ChildItem \\123.456.78.910\Y$\Z\*.txt | Where { $_.CreationTime -ge (Get-Date).AddMinutes(-30) } | % { Copy-Item $_.FullName -destination T:\FolderName }

您可以使用-command參數調用powershell.exe來執行此操作。 您也可以使用分號將多個powershell命令分開。 您應該將雙引號雙引號引起來。

這是一個例子:

powershell -command "$limit = (Get-Date).AddDays(-4); $path = 'T:\Folder Name';Get-ChildItem -Path $path -Recurse -Force -include *.txt | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force"

powershell -command "$limit = (Get-Date).AddDays(-4); $path = 'T:\Folder Name';Get-ChildItem \\123.456.78.910\Y$\Z\*.txt | Where-Object { $_.CreationTime -ge (Get-Date).AddMinutes(-30) } | % { Copy-Item $_.FullName -destination 'T:\DestinationFolderName' }"

編輯:這是它的單行版本,只有一組“”; 里面的所有引號都是單'

powershell -command "$limit = (Get-Date).AddDays(-2); $path = 'D:\Folder\Folder Name'; Get-ChildItem -Path $path -Recurse -Force -include *.txt | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force;Get-ChildItem \\123.456.25.123\D$\folder*.txt | Where-Object { $_.CreationTime -ge (Get-Date).AddMinutes(-30) } | % { Copy-Item $_.FullName -destination 'D:\Folder\Folder' }";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM