2

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' }
1
  • 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.PowerShell.Commands.OutFileCommand Commented Jun 5, 2015 at 10:38

1 Answer 1

4
$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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.