麻烦在.ps1中的Powershell功能

时间:2013-11-18 17:11:42

标签: debugging powershell dpm

我正在尝试修改工作脚本,使其模块化。该脚本的目的是连接到DPM服务器,获取附加的库并对其进行盘点。清单完成后,脚本会将相应的磁带标记为“空闲”。脚本在

下面

我有两个问题。第一个来了又走了,因为我编辑了剧本。当我运行脚本:。\ script.ps1时,Powershell说:

  

C:\ it \ test.ps1:无法验证参数'DPMLibrary'的参数。参数为null。提供非null参数并再次尝试该命令。

     

在行:1字符:11   +。\ test.ps1<<<<

     
      
  • CategoryInfo:NotSpecified:(:) [Write-Error],WriteErrorException   FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  •   

第二个问题出现在我刚刚将函数复制到shell中时。 Get-Libraries函数工作正常,并返回连接库的属性。当我将参数传递给Inventory-DPMLibrary时,库存完成。当我将库参数传递给Update-TapeStatus函数时,出现错误

  

运算符'-notmatch'的错误参数:解析“ slot ” - Quantifier {x,y} follo   什么都没有..

     

在行:6字符:77

     
      
  • $ tapes = Get-DPMTape -DPMLibrary $ lib |其中{$ _。位置 - 不匹配   <<<< “* s​​lot *”} |排序位置   
        
    • CategoryInfo:InvalidOperation:(:) [],RuntimeException   ? + FullyQualifiedErrorId:BadOperatorArgument
    •   
  •   

看起来$ liblist参数为null,即使变量不是。是什么给了什么?

这是脚本:

[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

感谢。

1 个答案:

答案 0 :(得分:0)

您的函数Inventory-DPMLibraries需要一个参数($liblist):

Function Inventory-DPMLibraries ($liblist) {
  ...
}

但是,在调用函数时不提供该参数:

Try {
  Inventory-DPMLibraries
}
Catch {
  Write-Error $_
  Exit
}

将上述内容改为:

Try {
  Inventory-DPMLibraries $liblist
}
Catch {
  Write-Error $_
  Exit
}