2

この同一のコードは 3 つのサーバーで使用されており、そのうちの 1 つだけがサイレントにアイテムの移動に失敗します (それでもアイテムは削除されますが、共有には表示されません)。

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

移行-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 を起動すると、「'FileSystem' プロバイダーで InitializeDefaultDrives 操作を実行しようとして失敗しました」というエラー メッセージが表示されます。ただし、これは残りの操作 (バックアップ ファイルを BackupArchive フォルダーにコピーするなど) には実際には影響しません。

copy-item も共有へのコピーに失敗しますが、/BackupArchive フォルダーへのコピーには成功します。

4

2 に答える 2

0

これが役立つかどうかを確認してください。ただし、共有をマップするNew-PSDrive代わりにコマンドレットを使用してみてください。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 エクスプローラーなどの昇格されていない OS 機能から明らかに隠され、昇格されていないコマンド プロンプト セッションを介してその存在を表示しようとします。それが修正されなかったため、再起動中に自動的に再接続していたと思います。

ソリューションは、昇格したコマンド プロンプトnet use m: /deleteからコマンドを実行するのと同じくらい簡単でした。

于 2016-06-14T18:10:39.657 に答える