i made a script for backup pst files on computer but i have a problem. i copied this script on remote computer and when i start this script the copying of pst files is not working. why? on my local computer script is working ok. also, i want that from the searching exclude path d:\backupmail. how?
If ((get-process | Where-Object{$_.Name -eq "OUTLOOK"}).Count -gt 0)
{
Stop-Process -name "OUTLOOK" -force
}
$today= Get-Date -format "MM-dd-yyyy HH.mm"
$CurrentDate = Get-Date
$days="-1"
$filterlastwritetime=$CurrentDate.AddDays($days)
$drv=Get-WmiObject -class win32_Logicaldisk | Where { $_.DriveType -eq 3 -and $_.DeviceID -ne 'C:'}
$location= $drv.DeviceID +"\backupmail"
if (!(Test-Path -path $location))
{
md $location
$pst=Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'pst'"
Copy-Item $pst.Name $location -Force -recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
else
{
$pst=Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'pst'"
Copy-Item $outlookpst.Name $location -Force -Recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
$Daysback = "-45"
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $location | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
thanks
Perhaps because $location already exists, which means it will execute the else-clause, which are referencing $outlookpst, a variable you never set. I guess you want to rename this to $pst
i changed code
If ((get-process | Where-Object{$_.Name -eq "OUTLOOK"}).Count -gt 0)
{
Stop-Process -name "OUTLOOK" -force
}
$today= Get-Date -format "MM-dd-yyyy HH.mm"
$CurrentDate = Get-Date
$days="-1"
$filterlastwritetime=$CurrentDate.AddDays($days)
$pst=Get-WmiObject -Query "Select * from CIM_DataFile Where Extension = 'pst'"
$drv=Get-WmiObject -class win32_Logicaldisk | Where { $_.DriveType -eq 3 -and $_.DeviceID -ne 'C:'}
$location= $drv.DeviceID +"\backupmail"
if (!(Test-Path -path $location))
{
md $location
Copy-Item $pst.Name $location -Force -recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
else
{
Copy-Item $pst.Name $location -Force -Recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
$Daysback = "-45"
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $location | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
but i receive error
Copy-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\backupmail.ps1:23 char:12
- Copy-Item <<<< $pst.Name $location -Force -Recurse
- CategoryInfo : InvalidData: ( [Copy-Item], ParameterBindingVa
lidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.CopyItemCommand
This is indicating that the value $pst.name is $null, which would imply that
$pst=Get-WmiObject -Query “Select * from CIM_DataFile Where Extension = ‘pst’”
is not returning anything. Scenario for this would be if there isn’t a PST file available.
Maybe an idea to put a
Get-Variable
after
$location= $drv.DeviceID +“\backupmail”
, to confirm all your assignments are good.
Worthwhile also maybe executing the
$pst=Get-WmiObject -Query “Select * from CIM_DataFile Where Extension = ‘pst’”
with the -Computer parameter to confirm it is returning anything.
but pst file is available but i cannot copying pst file on location \backupmail
also i tryed with get-childitem. my code is
If ((get-process | Where-Object{$_.Name -eq "OUTLOOK"}).Count -gt 0)
{
Stop-Process -name "OUTLOOK" -force
}
$today= Get-Date -format "MM-dd-yyyy HH.mm"
$CurrentDate = Get-Date
$days="-1"
$filterlastwritetime=$CurrentDate.AddDays($days)
$drv=Get-WmiObject -class win32_Logicaldisk | Where { $_.DriveType -eq 3 -and $_.DeviceID -ne 'C:'}
$location= $drv.DeviceID +"\backupmail"
$drvpath=$drv.DeviceID+'\'
$pst=get-childitem "c:\", $drvpath -recurse -ErrorAction SilentlyContinue | where{$_.Path -notlike "*backupmail*" -and $_.extension -like "*.pst"}
if (!(Test-Path -path $location))
{
md $location
Copy-Item $pst.fullname $location -Force -recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
else
{
Copy-Item $pst.fullname $location -Force -Recurse
cd $location
dir | where-object{$_.LastWriteTime -gt $filterlastwritetime} | rename-item -newname {$_.name+ ($today)+".pst"}
}
$Daysback = "-45"
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $location | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
but i receive error
Copy-Item : Cannot bind argument to parameter ‘Path’ because it is null.
At C:\backupmail.ps1:17 char:12
- Copy-Item <<< $pst.fullname
C:\Users\user\goran.ljubic@dri.rs backup.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.gov.rs (1).pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.gov.rs.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.rs (1).pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.rs.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@drims.rs.pst
C:\Users\user\Documents\Outlook Files\My Outlook Data File(1).pst
C:\Users\user\Documents\Outlook Files\podrska@dri.rs.pst
C:\Users\user\Documents\Outlook Files\sit@dri.gov.rs.pst
C:\Users\user\Documents\Outlook Files\sit@dri.rs (1).pst
C:\Users\user\Documents\Outlook Files\sit@dri.rs.pst
where i made error?
thanks
when manually start. pst files is availabled
PS D:\backupmail> $pst.fullname
C:\Users\user\goran.ljubic@dri.rs backup.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.gov.rs (1).pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.gov.rs.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.rs (1).pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@dri.rs.pst
C:\Users\user\Documents\Outlook Files\goran.ljubic@drims.rs.pst
C:\Users\user\Documents\Outlook Files\My Outlook Data File(1).pst
C:\Users\user\Documents\Outlook Files\podrska@dri.rs.pst
C:\Users\user\Documents\Outlook Files\sit@dri.gov.rs.pst
C:\Users\user\Documents\Outlook Files\sit@dri.rs (1).pst
C:\Users\user\Documents\Outlook Files\sit@dri.rs.pst