- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Stupid question
We have a server that I have to backup using powershell and robocopy because it is still stuck on Server 2003 SP1. I have the following script and it runs just fine but I'd like to add some error detection. Would it be possible to have the script return an email of robocopy returned error level 1 or something similar?
code:
$Today = (Get-Date -format "yyyy-MM-dd")
$targetFolder = "\\BACKUP\backups$\APP10\FlatFile"
$backupDir = "$targetFolder\$Today"
function Remove-Items
{
$testFolderPath = $targetFolder
if (Test-Path -Path $testFolderPath)
{
$fiveDaysAgo = (Get-Date).AddDays(-5)
Get-ChildItem -Path $testFolderPath -Recurse | Where-Object { $_.CreationTime -lt $fiveDaysAgo } | Remove-Item -recurse -force
}
else
{
Write-Host "$testFolderPath does not exist."
#Kill Script
Exit
}
}
function Send-Email
{
$emailFrom = "APP10Backup@somewhere.lan"
$emailTo = "sysadmins@somewhere.lan"
$subject = "Backup Completed Sucessfully"
$body = "APP10 was backed up successfully "
$smtpServer = "exch.somewhere.lan"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
function Create-Folders
{
if (Test-Path -Path $backupDir)
{
#do nothing. Folders have been setup already
}
else
{
md "$backupDir" -force
}
}
# Remove Folders older than $fivedaysago
Remove-Items
# Start backup operation
Create-Folders
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "C:\Documents and Settings" "$backupDir\c\Documents and Settings" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "C:\Inetpub" "$backupDir\c\Inetpub" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "C:\usr" "$backupDir\c\usr" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\dev" "$backupDir\g\dev" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\ifas" "$backupDir\g\ifas" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\media" "$backupDir\g\media" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\migdir" "$backupDir\g\migdir" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\mfaslmf" "$backupDir\g\mfaslmf" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\sbi" "$backupDir\g\sbi" /LOG+:"$backupDir\backup.log"
Robocopy /MIR /Z /COPYALL /R:5 /W:2 /NP /TEE "G:\SystemState" "$backupDir\g\SystemState" /LOG+:"$backupDir\backup.log"
# Send an email telling somebody this script ran successfully
Send-Email
|
#
¿
Mar 14, 2012 16:30
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 26, 2024 05:57
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
adaz, thanks. I'll give that some testing
|
#
¿
Mar 14, 2012 20:45
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
stubblyhead posted:
Getting an unexpected error with a script I've used successfully on other servers without any problems:
code:$password = ConvertTo-SecureString "fakepassword" -AsPlainText -Force
ConvertFrom-SecureString $password | Set-Content c:\passwordfile.txt
(I know, -AsPlainText is not ideal, but for various reasons I can't do this interactively)
Anyway, I get the following error:
ConvertFrom-SecureString: The system cannot find the file specified
Some googling implies that this might be a UAC thing, can anyone confirm? I don't think it's related to c:\passwordfile.txt, since I would expect an error to be thrown on Set-Content in that case.
Unless you're running as admin in 7/2008R2 with UAC enabled. You won't be able to write to the root of a drive, it's a security thing. Try ENV:\temp or something else?
edit: damnit adaz, you're quick
|
#
¿
Mar 15, 2012 18:27
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Stupid question time again, regarding the following script
code:$Today = (Get-Date -format "yyyy-MM-dd")
$targetFolder = "C:\Backups\FlatFile"
$backupDir = "$targetFolder\$Today"
function Remove-Items
{
$testFolderPath = $targetFolder
if (Test-Path -Path $testFolderPath)
{
$fiveDaysAgo = (Get-Date).AddDays(-5)
#Take ownership of the folders and files in $testFolderPath
Invoke-Expression "takeown /f $testFolderPath /a /r /d y"
#Assign the Administrators group Full Control permissions to $testFolderPath
Invoke-Expression "icacls $testFolderPath /grant administrators:F /t"
#Delete folders in $targetFolder older than 5 days
Get-ChildItem -Path $testFolderPath -Recurse | Where-Object { $_.CreationTime -lt $fiveDaysAgo } | Remove-Item -recurse -force
}
else
{
Write-Host "$testFolderPath does not exist."
#Kill Script
Exit
}
}
Remove-Items
It throws receive the following errors
code:Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (C:\backups\servername\flatfile\2012-04-01:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (C:\backups\shda...le\2012-04-01\c:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (C:\backups\shda...ts and Settings:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (C:\backups\shda...ings\user_ad:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\backups\servername\flatfile\2012-04-01\c\Documents and Settings\user_ad\Local Settings\Temp\hsperfdata_user_ad: Access to the path is denied.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : InvalidArgument: (hsperfdata_user_ad:DirectoryInfo) [Remove-Item], ArgumentException
+ FullyQualifiedErrorId : RemoveFileSystemItemArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\backups\servername\flatfile\2012-04-01\c\Documents and Settings\user_ad\Local Settings\Temp: The directory is not empty.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (Temp:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\backups\servername\flatfile\2012-04-01\c\Documents and Settings\user_ad\Local Settings: The directory is not empty.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (Local Settings:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\backups\servername\flatfile\2012-04-01\c\Documents and Settings\user_ad: The directory is not empty.
At line:1 char:125
+ Get-ChildItem -Path C:\backups\servername\flatfile -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Remove-Item <<<< -recurse -force
+ CategoryInfo : WriteError: (C:\backups\shda...ngs\user_ad:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
I tried to end run around Remove-Item by creating the variable $olddata and passing that to the a command prompt invoking RD(rmdir), but that also errors out
code:$olddata = (Get-ChildItem -Path $testFolderPath -Recurse | where { ((get-date)-$_.creationTime).days -ge 4} | Where {$_.psIsContainer -eq $true} )
#Remove-Item -Path $olddata -recurse -force
Invoke-Expression "rd $olddata /q /s"
Pass to rd error
code:Remove-Item : A positional parameter cannot be found that accepts argument 'c'.
At line:1 char:3
+ rd <<<< 2012-04-01 c Documents and Settings user_ad user_ad /q /s
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
My brain is fried, any idea how I can get Powershell to delete these leftovers?
|
#
¿
Apr 7, 2012 00:47
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Hey thanks for this, i actually just now have a change to look at this script again so good timing. I'll see how it works and report back.
|
#
¿
Apr 11, 2012 22:23
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Nebulis01 posted:
Hey thanks for this, i actually just now have a change to look at this script again so good timing. I'll see how it works and report back.
Alrighty, well I ended up not getting it to work the way I wanted. I used ntbackup to create a .bkf of the flatfile stuff that needed to be archived and just had ps move that over, it works out pretty well and deletes when it's supposed to.
|
#
¿
Apr 18, 2012 18:05
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Any ideas on why the following returns items that are from yesterday instead of just the stuff 5 days old and older?
code:$targetFolder = "c:\backups"
Get-ChildItem -Path $targetFolder -recurse | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-5))} | Remove-Item -recurse -force -whatif
The directory has items from 6/16-6/21 and when run it returns the following:
code:What if: Performing operation "Remove Directory" on Target "C:\Backups\2012-06-16".
What if: Performing operation "Remove Directory" on Target "C:\Backups\2012-06-16\Flat File".
What if: Performing operation "Remove Directory" on Target "C:\Backups\2012-06-16\SystemState".
What if: Performing operation "Remove File" on Target "C:\Backups\2012-06-16\backup.log".
What if: Performing operation "Remove File" on Target "C:\Backups\2012-06-21\Flat File\Daily FlatFile Backup.bks".
What if: Performing operation "Remove File" on Target "C:\Backups\2012-06-21\Flat File\FlatFile.bkf".
What if: Performing operation "Remove File" on Target "C:\Backups\2012-06-21\SystemState\Daily SystemState Backup.bks".
What if: Performing operation "Remove File" on Target "C:\Backups\2012-06-21\SystemState\SystemState.bkf".
Nebulis01 fucked around with this message at 00:36 on Jun 23, 2012
|
#
¿
Jun 23, 2012 00:24
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Have you tried with LastWriteTime instead of CreationTime? It may be that the CreationTime does not reflect the correct date for whatever reason, I'd recommend checking the attributes for the incorrectly matched files with
code:select fullname, creationtime, lastwritetime
in place of the Remove-Item part.
It looks like something is touching the files and while the directories are named by date created the file timestamps are today so I've got a bit of work to figure out what's touching these that shouldn't be. But that would solve the issue.
|
#
¿
Jun 27, 2012 18:45
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 26, 2024 05:57
|
|
- Nebulis01
- Dec 30, 2003
-
Technical Support Ninny
|
Alright, powershell is pissing me off right now.
I created a bunch of accounts with a python script that called dsadd, but I hosed it up and now I'm trying to fix the profile paths. I want to find all the profile paths that are on server2, instead of server1.
So I say to myself, this sounds perfect for Powershell! Except Powershell can't read the profile paths out of most of my accounts, they just show up as null. dsget works just fine, and looking at them in AD Users & Computers I see the bad profile path, but get-aduser shows nothing.
Nevermind what a loving task it was to get powershell to even search. And nevermind how impossible Microsoft has made it to actually get the AD cmdlets. Why in the hell would I have to run a special instance of Powershell (Active Directory Module for Windows PowerShell) to actually be able to query AD. Shouldn't running Powershell on the DC be enough?
Curious, if you have time would you spin up a Server 2012 DC in test and see if the query succeeds in powershell v3? It's supposed to have improved/fixed a bunch of this poo poo
|
#
¿
Aug 16, 2012 03:53
|
|