![](/img/trans.png)
[英]PowerShell - Script works perfectly in the ISE, but not in the normal console
[英]Powershell Script works in Debugger but not at normal run(ISE)
仅当我在ISE中调试此脚本(版本1)时,该脚本才能工作。 但是如果执行它,它将不会删除oldFilesPath中的文件,而是将新文件从backupPath复制到那里。 我尝试了其他方法,如管道的Remove-Item。 德尔也不会工作。 现在,我做了一些实验,并编写了第二个版本(版本2)。 我将自己的操作放入函数中,并获得了相同的意外行为。 但是,如果我在两个函数之间停顿了例如60秒钟,就我而言,脚本运行良好。 所以我认为Powershell不能很好地管理执行或类似的事情。.我不知道。 也许有人对此有解释?
版本1
function createIfDontExist ($directory)
{
if (-not (Test-Path -Path "$directory" -PathType Container)) {
New-Item -Path "$directory" -ItemType directory
}
}
#current user
$user="$env:username"
#Driveletter of the backup drive
$driveLetter="D:"
#date from today
$currentDate = Get-Date
################################################################################################################
#if no HDD detected, error message appears
$backupDrive = Test-Path "$driveLetter\"
if($backupDrive -eq $false) {
"Backup-HDD not connected!" | Out-File "C:\Users\$user\AppData\Local\hddTest.txt"
#"$currentDate : Backup failed!" | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append
exit
}
else {
"Backup-HDD connected." | Out-File "C:\Users\$user\AppData\Local\hddTest.txt"
#"$currentDate : Backup success." | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append
#path where the .pst file is located
$sourcePath = "C:\Users\$user\Documents\Outlook\"
#location where the .pst-file should be backed up
$backupPath = "$driveLetter\Outlook-Backup\$user"
#location for old .pst-file
$oldFilesPath = "$driveLetter\Outlook-Backup\Old-Files\$user"
#date 7 days ago
$deleteDate = $currentDate.AddDays(-7)
#Creates the Outlook-Backup and Old-Files-Path if they don´t exist
createIfDontExist($backupPath)
createIfDontExist($oldFilesPath)
#gets non directory files from Outlook-Backup
$filesInBackupPath = Get-ChildItem $backupPath
#Checks if files in Backup-Path are there longer than seven days since the last backup.
#Files older than seven days will be moved to the Old-Files-Path and replace their older copies
$filesInBackupPath | foreach {
if($_.CreationTime -lt $deleteDate)
{
if(Test-Path -Path "$oldFilesPath\$_" -PathType Leaf){
Remove-Item -Path $oldFilesPath\$_ -Force
}
Move-Item $_.FullName $oldFilesPath -Force
}
}
#gets non directory files from Source
$filesInSourcePath = Get-ChildItem $sourcePath
#Checks if Backup-Path contains same files like Source-Path and copies them possibly from Source-Path to Backup-Path
$filesInSourcePath | foreach {
if(-not (Test-Path -Path "$backupPath\$_" -PathType Leaf))
{
Copy-Item $_.FullName "$backupPath"
}
}
}
版本2
function createIfDontExist ($directory)
{
if (-not (Test-Path -Path "$directory" -PathType Container)) {
New-Item -Path "$directory" -ItemType directory;
}
}
#Checks if there are files in the Backup-Path which are 7 days old and deletes identically named files in the Old-Files-Path.
function removeOldFiles($folders)
{
$backupidx = 0;
$oldfilesidx = 1;
$backupfolder = $folders[$backupidx];
$oldfilesfolder = $folders[$oldfilesidx];
ForEach ($backupFile in (Get-ChildItem -Path $backupfolder)) {
if($backupFile.CreationTime -lt $deleteDate)
{
if(Test-Path -Path "$oldfilesfolder\$($backupFile.Name)" -PathType Leaf ){
$file = Get-ChildItem -Path "$oldfilesfolder\$($backupFile.Name)";
$file.Delete();
}
}
}
return;
}
#Checks if there are files in the Backup-Path which are 7 days old and moves them into the Old-Files-Path.
function moveBackupFilesToOldPath($folders)
{
$backupidx = 0;
$oldfilesidx = 1;
$backupfolder = $folders[$backupidx];
$oldfilesfolder = $folders[$oldfilesidx];
ForEach ($backupFile in (Get-ChildItem -Path $backupfolder)) {
if($backupFile.CreationTime -lt $deleteDate)
{
$backupFile.MoveTo("$oldfilesfolder\$($backupFile.Name)");
}
}
return;
}
#Checks if Backup-Path contains same files like Source-Path and copies them possibly from Source-Path to Backup-Path.
function syncFolder($folders)
{
$sourceidx = 0;
$destinationidx = 1;
$sourcefolder = $folders[$sourceidx];
$destinationfolder = $folders[$destinationidx];
ForEach ($sourceFile in (Get-ChildItem -Path $sourcefolder)) {
if(-not (Test-Path -Path "$destinationfolder\$($sourceFile.Name)" -PathType Leaf))
{
$sourceFile.CopyTo("$destinationFolder\$($sourceFile.Name)");
}
}
return;
}
#current user
$user="$env:username"
#Driveletter of the backup drive
$driveLetter="D:"
#date from today
$currentDate = Get-Date
################################################################################################################
#if no HDD detected, error message appears
$backupDrive = Test-Path "$driveLetter\"
if($backupDrive -eq $false) {
"Backup-HDD not connected!" | Out-File "C:\Users\$user\AppData\Local\festplattenTest.txt"
#"$currentDate : Backup failed!" | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append
exit
}
else {
"Backup-HDD connected." | Out-File "C:\Users\$user\AppData\Local\hddTest.txt"
#"$currentDate : Backup success." | Out-File "C:\Users\$user\AppData\Local\outlookBackup.txt" -Append
#path where the .pst file is located
$sourcePath = "C:\Users\$user\Documents\Outlook-Dateien";
#location where the .pst-file should be backed up
$backupPath = "$driveLetter\Outlook-Backup\$user";
#location for old .pst-file
$oldFilesPath = "$driveLetter\Outlook-Backup\Old-Files\$user";
#date 7 days ago
$deleteDate = $currentDate.AddDays(-7);
#Creates the Outlook-Backup and Old-Files-Path if they don´t exist
createIfDontExist($backupPath);
createIfDontExist($oldFilesPath);
removeOldFiles($backupPath, $oldFilesPath);
Start-Sleep -Seconds 60;
moveBackupFilesToOldPath($backupPath, $oldFilesPath);
Start-Sleep -Seconds 60;
syncFolder($sourcePath, $backupPath);
return;
}
您是说实际上是逐步在ISE中调试程序,还是仅在ISE中运行脚本。 如果是后者,则您尝试访问的目录可能存在权限问题。 在ISE中运行脚本会使用您的用户凭据运行它。
即使在调试中,这也可能如何工作? 您的Test-Path
命令应永远不会返回true,因为
"$oldFilesPath\$_"
是相同的
D:\Outlook-Backup\Old-Files\$user\D:\Outlook-Backup\$user\<filename>
您需要将其更改为
"$oldFilesPath\$($_.Name)"
为此工作。 脚本结尾附近的"$backupPath\\$_"
。
我遇到了类似的问题,其中Powershell脚本可以在Debugger中运行,或者通过在控制台上键入每个命令,但不能在正常运行(ISE)或调用脚本时运行。
我意识到我使用的是没有时间在正常运行中执行的加速方法(太快了)。 为了进行测试,我在这里和那里引入了一些超时 ,并且代码按预期运行。
然后,只需确定异步方法并等待它完成就可以继续。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.