我正在尝试修改一个工作脚本,使其模块化。该脚本的目的是连接到 DPM 服务器、获取附加的库并清点它们。清点完成后,脚本会将相应的磁带标记为“空闲”。脚本如下
我有两个问题。第一个来了又走了,因为我已经编辑了脚本。当我运行脚本:.\script.ps1 时,Powershell 说:
C:\it\test.ps1:无法验证参数“DPMLibrary”上的参数。参数为空。提供一个非空参数并再次尝试该命令。
在 line:1 char:11 + .\test.ps1 <<<<
- CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExceptionfullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
当我刚刚将函数复制到 shell 中时,第二个问题就出现了。Get-Libraries 函数工作正常并返回连接库的属性。当我将参数传递给 Inventory-DPMLibrary 时,库存完成。当我将库参数传递给 Update-TapeStatus 函数时,我收到一条错误消息
运算符“-notmatch”的错误参数:解析“槽” - 量词 {x,y} 没有任何内容..
在行:6 字符:77
- $tapes = 获取-DPMTape -DPMLibrary $lib | 其中 {$_.Location -notmatch <<<< " *slot *"} | 排序位置
- CategoryInfo : InvalidOperation: (:) [], RuntimeException ?+ FullyQualifiedErrorId : BadOperatorArgument
看起来 $liblist 参数为空,即使变量不是。是什么赋予了?
这是脚本:
[CmdletBinding()]
param(
[ValidateSet("Fast","Full")]
[string]$InventoryType = 'Fast',
[string]$DPMServerName = 'server1'
)
Function Import-DPMModule {
Try {
Import-Module DataProtectionManager -ErrorAction Stop
}
Catch [System.IO.FileNotFoundException] {
Throw ("The DPM Powershell module is not installed or is not importable. The specific error message is: {0}" -f $_.Exception.Message)
}
Catch {
Throw ("Unknown error importing DPM powershell module. The specific error message is: {0}" -f $_.Exception.Message)
}
}
Function Get-Libraries {
Write-Verbose ("Getting list of libraries connected to {0}." -f $DPMServerName)
Try {
$libraries = Get-DPMLibrary $DPMServerName -ErrorAction Stop | Where {$_.IsOffline -eq $False}
}
Catch [Microsoft.Internal.EnterpriseStorage.Dls.Utils.DlsException] {
Write-Error ("Cannot connect to the DPM library. It appears that the servername is not valid. The specific error message is: {0}" -f $_.Exception.Message)
Return
}
Catch {
Write-Error ("Unknown error getting library. The specific error message is: {0}" -f $_.Exception.Message)
Return
}
Return $libraries
}
Function Inventory-DPMLibraries ($liblist) {
Foreach ($lib in $liblist) {
If ($InventoryType -eq "Fast") {
Write-Verbose ("Starting fast inventory on {0}" -f $lib)
$inventoryStatus = Start-DPMLibraryInventory -DPMLibrary $lib -FastInventory -ErrorAction SilentlyContinue
}
Else {
Write-Verbose ("Starting detailed inventory on {0}" -f $lib)
$inventoryStatus = Start-DPMLibraryInventory -DPMLibrary $lib -DetailedInventory -ErrorAction SilentlyContinue
}
While ($inventoryStatus.HasCompleted -eq $False) {
Write-Output ("Running {0} inventory on library: {1}" -f $InventoryType.ToLower(),$lib.UserFriendlyName)
Start-Sleep 5
}
If ($inventoryStatus.Status -ne "Succeeded") {
Throw ("Unknown error in inventory process. The specific error message is: {0}" -f $_.Exception.Message)
Return
}
}
}
Function Update-TapeStatus ($liblist) {
Foreach ($lib in $liblist) {
write-host ("in tapestatus. the lib is: {0}" -f $lib)
Write-Verbose ("Beginning the process to determine which tapes to mark 'free' on {0}" -f $lib)
Write-Verbose ("Getting list of tapes in {0}." -f $lib)
$tapes = Get-DPMTape -DPMLibrary $lib | Where {$_.Location -notmatch "*slot*"} | Sort Location
Foreach ($tape in $tapes) {
If ($tape.DisplayString -eq "Suspect") {
Write-Verbose ("Remove suspect tapes from the DPM database.")
Invoke-Command -ScriptBlock {osql -E -S server2 -d DPMDB_server1 -Q "UPDATE tbl_MM_ArchiveMedia SET IsSuspect = 0"} -whatif
Start-DPMLibraryInventory -DPMLibrary $lib -FastInventory -Tape $tape -whatif
}
#Run a full inventory on "unknown" tapes
#Make recyclable tapes "free"
If (($tape.DisplayString -notlike "Free*" -and $tape.DataSetState -eq "Recyclable") -or ($tape.DisplayString -like "Unrecognized")) {
Write-Output ("Marking the tape in slot {0} as free." -f $tape.Location)
Set-DPMTape $tape -Free -whatif
}
If ($tape.OMIDState -eq "Unknown") {
Write-Warning ("Unknown tape found in slot {0}. Beginning detailed inventory." -f $tape.location)
$inventoryStatus = Start-DPMLibraryInventory -DPMLibrary $lib -DetailedInventory -Tape $tape -whatif
While ($inventoryStatus.HasCompleted -eq $False) {Write-Output ("Running full inventory on the tape in slot {0} (label {1})" -f $tape.Location,$tape.Label); Start-Sleep 10}
}
}
}
}
#Calling functions
Try {
Import-DPMModule
}
Catch {
Write-Error $_
Exit
}
Try {
$liblist = Get-Libraries
}
Catch {
Write-Error $_
Exit
}
Try {
Inventory-DPMLibraries
}
Catch {
Write-Error $_
Exit
}
Update-TapeStatus $liblist
谢谢。