Debugging .ps1中的Powershell函数有问题

Debugging .ps1中的Powershell函数有问题,debugging,powershell,dpm,Debugging,Powershell,Dpm,我试图修改一个工作脚本,使其模块化。该脚本的目的是连接到DPM服务器,获取连接的库,并清点它们。盘点完成后,脚本会将相应的磁带标记为“空闲”。脚本如下 我有两个问题。第一个已经来了又走了,因为我已经编辑了脚本。运行脚本时:.\script.ps1,Powershell会显示: C:\it\test.ps1:无法验证参数“DPMLibrary”上的参数。参数为空。请提供非null参数,然后重试该命令 第1行字符:11 +.\test.ps1您的函数库存DPMLibraries需要一个参数($lib

我试图修改一个工作脚本,使其模块化。该脚本的目的是连接到DPM服务器,获取连接的库,并清点它们。盘点完成后,脚本会将相应的磁带标记为“空闲”。脚本如下

我有两个问题。第一个已经来了又走了,因为我已经编辑了脚本。运行脚本时:.\script.ps1,Powershell会显示:

C:\it\test.ps1:无法验证参数“DPMLibrary”上的参数。参数为空。请提供非null参数,然后重试该命令

第1行字符:11
+.\test.ps1您的函数
库存DPMLibraries
需要一个参数(
$liblist
):

功能清单DPMLibraries($liblist){
...
}
但是,调用函数时不提供该参数:

试试看{
库存数据库
}
抓住{
写入错误$_
出口
}
将上述内容更改为:

试试看{
库存DPMLibraries$liblist
}
抓住{
写入错误$_
出口

}
谢谢,这解决了第一个问题。我知道这很简单,因为我曾经修复过:)现在,为什么$liblist参数在传递到Update TapeStatus时为空?
“*slot*”
不是有效的正则表达式<代码>*指定前面字符的“零或多个”。由于以
*
开头,因此前面没有字符。将
-notmatch
更改为
-notlike
或将正则表达式更改为简单的
“slot”
。或者,如果您希望与文字字符匹配,请使用
“\*槽\*”
[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