【问题标题】:Cannot bind argument to parameter "FilePath" because it is null无法将参数绑定到参数“FilePath”,因为它为空
【发布时间】:2015-06-05 10:29:32
【问题描述】:

每次我运行下面的脚本时,我都会得到

无法将参数绑定到参数“FilePath”,因为它为空。

昨晚还在工作。没有进行任何更改,今天早上它只是失败了。有趣的是,如果我保存脚本然后运行它,它就可以工作。但是,如果我清除控制台然后再次运行它会失败。

我是否遗漏了一些明显的东西?

New-Item -ItemType Directory -Force -Path C:\NonStandard_Services
set-location C:\NonStandard_Services 
$Computers= Get-Content C:\computers.txt
$Report= $file
$file= $Computer
ForEach ($Computer in $Computers)
{
Get-WmiObject -ComputerName $Computer -class Win32_Service -ErrorAction SilentlyContinue | 
Where-Object -FilterScript {$_.StartName -ne "LocalSystem"}|
Where-Object -FilterScript {$_.StartName -ne "NT AUTHORITY\NetworkService"} | 
Where-Object -FilterScript {$_.StartName -ne "NT AUTHORITY\LocalService"} |
Select-Object -Property StartName,Name,DisplayName|
    ConvertTo-Html -Property StartName,Name,DisplayName -head $HTML -body "<H2>Non-    Standard Service Accounts on $Computer</H2>"| Out-File $Report -Append}
    #Rename-Item c:\GP_Services\Report.htm $file 
    Get-ChildItem | Where-Object {$_.extension -ne ".htm"} | Rename-Item -newname { $_.name + '.htm' }

【问题讨论】:

  • 错误是文件外:无法将参数绑定到参数“FilePath”,因为它为空。在 C:\Users\adminrrahul\Desktop\Svc_Accs Final03.ps1:14 char:135 + ... H2>"| Out-File $Report -Append} + ~~~~~~~ + CategoryInfo : InvalidData: (: ) [Out-File], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand

标签: powershell


【解决方案1】:
$Report= $file
$file= $Computer
ForEach ($Computer in $Computers)
{
  ...
}

在变量本身被赋值之前,您将变量赋值给其他变量。将上面的内容改为:

ForEach ($Computer in $Computers) {
  $file   = $Computer
  $Report = $file
  ...
}

或者在Out-File中直接使用$Computer

... | Out-File "$Computer.txt" -Append

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 1970-01-01
    • 2021-02-20
    • 2017-03-09
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多