2

这个相同的代码已经在 3 个服务器中使用过,只有其中一个会默默地无法移动项目(它仍然会删除它们,但它们不会出现在共享中)。

Azure-MapShare.ps1

param (
    [string]$DriveLetter,
    [string]$StorageLocation,
    [string]$StorageKey,
    [string]$StorageUser
)

if (!(Test-Path "${DriveLetter}:"))
{
    cmd.exe /c "net use ${DriveLetter}: ${StorageLocation} /u:${StorageUser} ""${StorageKey}"""
}

获取排除天数.ps1

param (
    [datetime]$startDate,
    [int]$daysBack
)

$date = $startDate
$endDate = (Get-Date).AddDays(-$daysBack)

$allDays = 
    do {
        "*"+$date.ToString("yyyyMMdd")+"*"
        $date = $date.AddDays(-1)
    } until ($date -lt $endDate)

return $allDays

迁移文件.ps1

param(
    [string]$Source, 
    [string]$Filter, 
    [string]$Destination,
    [switch]$Remove=$False
)

#Test if source path exist
if((Test-Path -Path $Source.trim()) -ne $True) {
    throw 'Source did not exist'
} 

#Test if destination path exist
if ((Test-Path -Path $Destination.trim()) -ne $True) {
    throw 'Destination did not exist'
}

#Test if no files in source
if((Get-ChildItem -Path $Source).Length -eq 0) {
    throw 'No files at source'
}

if($Remove)
{
    #Move-Item removes the source files
    Move-Item -Path $Source -Filter $Filter -Destination $Destination -Force
} else {
    #Copy-Item keeps a local copy
    Copy-Item -Path $Source -Filter $Filter -Destination $Destination -Force
}

return $True

作业步骤是在所有 3 台服务器上键入“PowerShell”,并包含相同的代码:

#Create mapping if missing
D:\Scripts\Azure-MapShare.ps1 -DriveLetter 'M' -StorageKey "[AzureStorageKey]" -StorageLocation "[AzureStorageAccountLocation]\backup" -StorageUser "[AzureStorageUser]"


#Copy files to Archive
D:\Scripts\Migrate-Files.ps1 -Source "D:\Databases\Backup\*.bak" -Destination "D:\Databases\BackupArchive"


#Get date range to exclude
$exclusion = D:\Scripts\Get-Exclusion-Days.ps1 -startDate Get-Date -DaysBack 7

#Remove items that are not included in exclusion range
Remove-Item -Path "D:\Databases\BackupArchive\*.bak" -exclude $exclusion


#Move files to storage account. They will be destroyed
D:\Scripts\Migrate-Files.ps1 -Source "D:\Databases\Backup\*.bak" -Destination "M:\" -Remove


#Remove remote backups that are not from todays backup
Remove-Item -Path "M:\*.bak" -exclude $exclusion

如果我使用 SQL 运行作业步骤,则文件将被删除,但不会出现在存储帐户中。如果我手动运行此代码块,它们会被移动

当我在服务器上启动 PowerShell 时,我收到一条错误消息:“尝试对 'FileSystem' 提供程序执行 InitializeDefaultDrives 操作失败。” 但是,这不会真正影响其余操作(例如,将备份文件复制到 BackupArchive 文件夹)。

我应该提到,copy-item 也无法复制到共享,但成功复制到 /BackupArchive 文件夹

4

2 回答 2

0

请注意确定这是否会对您有所帮助,但您可以尝试使用New-PSDrivecmdlet 而不是net use映射您的共享:

param (
    [string]$DriveLetter,
    [string]$StorageLocation,
    [string]$StorageKey,
    [string]$StorageUser
)

if (!(Test-Path $DriveLetter))
{    
    $securedKey = $StorageKey | ConvertTo-SecureString -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential ($StorageUser, $securedKey)

    New-PSDrive -Name $DriveLetter -PSProvider FileSystem -Root $StorageLocation -Credential $credentials -Persist
}
于 2016-06-17T08:34:37.780 回答
0

显然我在这件事上欺骗了自己。在测试期间,我必须在提升的命令提示符下运行 net use 命令。这显然将映射驱动器从非提升的操作系统功能(如 Windows 资源管理器)中隐藏起来,并尝试通过非提升的命令提示符会话查看其存在。我想它在重新启动期间也会自动重新连接,因为这并没有解决它。

net use m: /delete该解决方案就像从提升的命令提示符运行命令一样简单。

于 2016-06-14T18:10:39.657 回答