powershell和.bat文件

powershell和.bat文件,powershell,Powershell,因为我对power shell不是很在行,所以我想问一下如何管理这个任务。我已经创建了.bat文件,用于执行以下操作: echo before delete > delete.log forfiles -p "C:\test1" -s -m *.* -d -0 -c "cmd /c echo @path" >> trace.log forfiles -p "C:\test1" -s -m *.* -d -1 -c

因为我对power shell不是很在行,所以我想问一下如何管理这个任务。我已经创建了.bat文件,用于执行以下操作:

echo before delete > delete.log

forfiles -p "C:\test1" -s -m *.* -d -0 -c "cmd /c echo @path" >> trace.log

forfiles -p "C:\test1" -s -m *.* -d -1 -c "cmd /c del @path" >> trace.log

echo preserved  >> trace.log

forfiles -p "C:\test1" -s -m *.* -d -0 -c "cmd /c echo @path" >> trace.log
我通过单独的powershell脚本成功地发送了trace.log的输出,如下所示:

$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply@company.com -to "nobody@company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com
Write-Host "before delete" > delete.log
(Get-ChildItem "C:\test1" -Recurse | 
    Select-Object -ExpandProperty Fullname) >>trace.log
(Get-ChildItem "C:\test1" -Recurse | 
  Where-Object {$_.LastWriteTime -ge (Get-Date).addDays(-1)} |
    Remove-Item -Force) >>trace.log
Write-Host "preserved"  >> trace.log
(Get-ChildItem "C:\test1" -Recurse | 
    Select-Object -ExpandProperty Fullname) >>trace.log
$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply@company.com -to "nobody@company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com
由于我对power shell的了解有限,有没有办法将.bat命令与我当前的power shell脚本合并?想法是像上面的示例一样执行删除操作,创建日志并在一个power shell脚本中通过电子邮件发送。

尝试如下:

$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply@company.com -to "nobody@company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com
Write-Host "before delete" > delete.log
(Get-ChildItem "C:\test1" -Recurse | 
    Select-Object -ExpandProperty Fullname) >>trace.log
(Get-ChildItem "C:\test1" -Recurse | 
  Where-Object {$_.LastWriteTime -ge (Get-Date).addDays(-1)} |
    Remove-Item -Force) >>trace.log
Write-Host "preserved"  >> trace.log
(Get-ChildItem "C:\test1" -Recurse | 
    Select-Object -ExpandProperty Fullname) >>trace.log
$body = Get-Content -Path "C:\test1\trace.log" -Raw
Send-MailMessage -from doNotReply@company.com -to "nobody@company.com" -subject "Trace log delete status test" -body $body -smtpServer smtp.company.com

@marsze Ok抱歉,所以我像admin user一样运行ISE,并执行这一行Set ExecutionPolicy-Scope LocalMachine Unrestricted。然后,我在同一个会话中复制并运行了上述所有建议的代码,并发现以下错误:

Get-ChildItem : Access to the path 'C:\Program Files\Websense\Websense Endpoint\certutil' is denied.
At line:8 char:2
+ (Get-ChildItem "C:\test1" -Recurse |
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Program File...dpoint\certutil:String) [Get-ChildItem], Unauth 
   orizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
 
Get-ChildItem : Access to the path 'C:\Windows\CCM\ScriptStore' is denied.
At line:8 char:2
+ (Get-ChildItem "C:\test1" -Recurse |
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\CCM\ScriptStore:String) [Get-ChildItem], UnauthorizedA 
   ccessException
    + FullyQualifiedErrorId : 

现在我遇到了这个错误。无法加载ps1,因为在此系统上禁用了运行脚本。有关详细信息,请参阅https://go.microsoft.com/fwlink/?LinkID=135170上的“关于执行”策略。我尝试了几种Set ExecutionPolicy组合,但失败了,有什么建议吗?@user3391373你应该更准确一些。你到底尝试了什么,又是如何失败的。另外,有很多关于stackoverflow的帖子都在讨论这个问题。