Solved

error with powershell script Out-File : Cannot bind argument to parameter 'FilePath' because it is null.

Posted on 2016-11-22
3
36 Views
Last Modified: 2016-11-26
Hello,
I just started learning powershell and doing training. This is the script that I am practicing. This is exactly how they show in th video from Lynda.com but I am getting this error. Please help. thank you. I am posting the script that I wrote below

Also, I am getting this "message" prompt. See the screenshot. Where is this coming from? This script should not be prompting for "message"

and here is the error
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\cfscripts\function1.ps1:45 char:26
+     $computer | out-file $logFile -Append
+                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand

Open in new window

Function Get-DJOSInfo {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    [Alias('hostname')]
    [string[]]$ComputerName,
    [switch]$nameLog

)
Begin {
   if ($nameLog)
   {
       Write-Verbose "Finding name log file"
       $i = 0
    do {
    $logFile = "names-$i.txt"
    $i++
    } while (Test-Path $logFile)
    Write-verbose "Log file name will be stored $logfile"

    }
else {
write-verbose "Name logging off"
   } 


}
Process {
    if ($nameLog)
    {
        Write-Verbose
         "Name log on"
    }
    else
    {
       Write-Verbose "Name log off" 
    }

    foreach ($computer in $computername) {
    Write-Verbose "Now connecting to $computer"
    if ($nameLog){
    $computer | out-file $logFile -Append
    }
        $os = Get-WmiObject -ComputerName $computer -class Win32_OperatingSystem |
            Select Caption,BuildNumber,OSArchitecture,ServicePackMajorVersion
        $bios = Get-WmiObject -ComputerName $computer -Class Win32_BIOS |
            Select SerialNumber
        $processor = Get-WmiObject -ComputerName $computer -Class Win32_Processor |
            Select AddressWidth -first 1
        $osarchitecture = $os.osarchitecture -replace '-bit',''
        $properties = @{'ComputerName'=$computer;
                        'OSVersion'=$os.Caption;
                        'OSBuild'=$os.BuildNumber;
                        'OSArchitecture'=$osarchitecture;
                        'OSSPVersion'=$os.servicepackmajorversion;
                        'BIOSSerial'=$bios.SerialNumber;
                        'ProcArchitecture'=$processor.addresswidth}
        $obj = New-Object -TypeName PSObject -Property $properties
        Write-Output $obj

#write-host $computername   
}

}

End {}


}


#Get-DJOSInfo -computername server01

Open in new window

error message
0
Comment
Question by:creative555
3 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41898327
Lines 33 and 34 belong together into a single line - this is the "message" thingy you get.
The out-file error results from - no clue. The script should search for existing log files and create a new one (by incrementing the index number).
You can help yourself by adding -verbose when calling of the procedure; that activates the write-verbose messages, which otherwise are suppressed. They will print some debugging info.
0
 

Author Closing Comment

by:creative555
ID: 41902683
thanks! tested and it worked!!!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now