Cannot bind argument to parameter "FilePath" because it is null

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

You assign variables to other variables before they were assigned a value themselves. Change the above to this:

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

Or directly use $Computer in Out-File:

... | Out-File "$Computer.txt" -Append
Share:
14,577
Norrin Rad
Author by

Norrin Rad

Updated on June 14, 2022

Comments

  • Norrin Rad
    Norrin Rad almost 2 years

    Every time I run the script below I get

    Cannot bind argument to parameter 'FilePath' because it is null.

    It was working last night. No changes have been made and this morning it just fails. the funny thing is if i save the script and then run it, it works. However if I clear console then run it again it fails.

    Am I missing something obvious?

    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' }
    
    • Norrin Rad
      Norrin Rad almost 9 years
      Error is Out-File : Cannot bind argument to parameter 'FilePath' because it is null. At C:\Users\adminrrahul\Desktop\Svc_Accs Final03.ps1:14 char:135 + ... H2>"| Out-File $Report -Append} + ~~~~~~~ + CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Pow‌​erShell.Commands.Out‌​FileCommand