![](/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.