使用批处理脚本,我正在远程计算机上调用PowerShell脚本,因此在这里我可以看到该脚本正在执行,但是脚本日志正在部分生成。当我在本地计算机上手动运行时,日志正在按预期方式获取。附件是示例日志。我使用的代码是一个简单的内务处理代码。
我正在使用命令从批处理文件调用PowerShell脚本
powershell -inputformat none C:\temp\cleanup.ps1
这是我正在使用的代码
$VerbosePreference = "Continue"
$DaysToDelete = 2
$LogDate = get-date -format "MM-d-yy-HH"
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace(0xA)
$ErrorActionPreference = "silentlycontinue"
#$MemoryDmpPath = "C:\Windows\Memory.dmp"
$MemoryDmp = $env:systemroot
$MemoryDmp
Start-Transcript -Path C:\Build\DiskClean-up.log
$After = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
@{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
@{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
@{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } |
Format-Table -AutoSize | Out-String
$After
## Deletes the contents of the C:\Temp folder.
Get-ChildItem "C:\Users\*\AppData\Local\Microsoft\Windows\Explorer\*" -Recurse -Force -Verbose -ErrorAction SilentlyContinue |
Where-Object { ($_.LastWriteTIme -lt $(Get-Date).AddDays(-$DaysToDelete)) }
#| remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue
## The Contents of C:\Temp have been removed successfully!
## Deletes the contents of the C:\Temp folder.
Get-ChildItem "C:\Users\*\AppData\Local\Microsoft\Windows\Explorer\*" -Recurse -Force -Verbose -ErrorAction SilentlyContinue
#|remove-item -force -Verbose -recurse -ErrorAction SilentlyContinue
## The Contents of C:\Temp have been removed successfully!
## deletes the dmp files
$lastWrite =(Get-ItemProperty -Path $MemoryDmp\Memory.dmp -Name LastWriteTime).lastwritetime
$lastWrite
$timespan = new-timespan -days 30 -hours 00 -minutes 00
if (((get-date) - $lastWrite) -gt $timespan) {
Write-host MEMORY.dmp file is older then 60 days hence delete.
Remove-Item -path $MemoryDmp\MEMORY.DMP -force -Verbose
} else {
Write-host MEMORY.dmp file is newer than 60 days and it cannot be deleted.
}
Stop-Transcript
我期望的输出日志如下所示。
********************** Windows PowerShell transcript start Start time: 20190409151717 Username: home\admin1 RunAs User: home\admin1 Machine: home (Microsoft Windows NT 6.1.7601 Service Pack 1) Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Process ID: 9016 PSVersion: 5.1.14409.1018 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018 BuildVersion: 10.0.14409.1018 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\Temp\DiskClean-up.log VERBOSE: Performing the operation "Stop-Service" on target "Windows Update (wuauserv)". VERBOSE: Performing the operation "Remove Directory" on target "C:\Windows\Temp\BdeSqm". VERBOSE: Performing the operation "Remove Directory" on target "C:\Windows\Temp\DPTF". VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\DPTF\dptf_pnmwlanproxy.dll". VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\DPTF\dptf_wwanproxy.dll". VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\DPTF\dptf_pnmwlanproxy.dll". VERBOSE: Performing the operation "Remove File" on target "C:\Windows\Temp\DPTF\dptf_wwanproxy.dll". VERBOSE: Performing the operation "Remove Directory" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs\McTray_MC0X8CPC.log". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs\UpdaterUI_MC0X8CPC.log". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs\UpdaterUI_MC0X8CPC_error.log". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs\UpdaterUI_MC0X8CPC.log". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\McAfeeLogs\UpdaterUI_MC0X8CPC_error.log". VERBOSE: Performing the operation "Remove Directory" on target "C:\users\A747231\AppData\Local\Temp\Outlook Logging". VERBOSE: Performing the operation "Remove File" on target "C:\users\A747231\AppData\Local\Temp\Outlook Logging\Outlook-20190409T1112140152.etl". ********************** Windows PowerShell transcript end End time: 20190409151838 **********************
但是我得到的输出低于
********************** Windows PowerShell transcript start Start time: 20190409151717 Username: home\admin1 RunAs User: home\admin1 Machine: home (Microsoft Windows NT 6.1.7601 Service Pack 1) Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Process ID: 9016 PSVersion: 5.1.14409.1018 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1018 BuildVersion: 10.0.14409.1018 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is C:\Temp\DiskClean-up.log ********************** Windows PowerShell transcript end End time: 20190409151838 **********************