PowerShell SQL 作业步骤移动项目无法在 1 台服务器上运行

PowerShell SQL Job Step Move-Item not working on 1 server

这个相同的代码已在 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}"""
}

Get-Exclusion-Days.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

Migrate-Files.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 时,收到一条错误消息:"Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed." 但是,这并不会真正影响其余操作(例如,将备份文件复制到 BackupArchive 文件夹)。

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

显然我在这个问题上欺骗了自己。在测试期间,我必须在提升的命令提示符中使用 运行 net use 命令。这显然从非提升的 OS 功能(例如 Windows 资源管理器)隐藏了映射的驱动器,并试图通过非提升的命令提示符会话查看其存在。我想它在重新启动期间也会自动重新连接,因为这并没有解决它。

解决方案非常简单,只需从 提升的命令提示符

中执行 net use m: /delete 命令即可

请注意这是否对您有帮助,但您可以尝试使用 New-PSDrive cmdlet 而不是 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
}