powershell使用“3”参数调用“Replace”的异常:“Value不能为null

时间:2015-01-09 22:38:45

标签: powershell-v3.0

调用此函数时,我得到调用null错误的异常。

代码:


if(Test-Path($Env:TEMP))  {

    $hsPrefDataDirectories = Get-ChildItem "$Env:TEMP" | where {$_.Name -match "hsperfdata"} | Format-Table name -hideTableHeaders | Out-String -stream    
    $hsPrefDataDirectories = [regex]::Replace($hsPrefDataDirectories, "^\s+", '')
    $hsPrefDataDirectories = [regex]::Replace($hsPrefDataDirectories, "\s+", ',')
    $hsPrefDataDirectories = [regex]::Replace($hsPrefDataDirectories, ",$", '')
    $hsPrefDataDirectories = $hsPrefDataDirectories.split(",")
    .....
    $hsPrefDataDirectories = $hsPrefDataDirectories.split(",")

Error: 

Exception calling "Replace" with "3" argument(s): "Value cannot be null.
Parameter name: input"

...

You cannot call a method on a null-valued expression.

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为在您的" $ Env:TEMP"文件夹没有匹配字符串" hsperfdata"。

在尝试替换字符之前,请确保$ hsPrefDataDirectories不为空。