4

I am trying to get a content of a text file by using Get-Content and I want the value for the -path to be on a variable like so:

$MyFileName = "testfile"    
$MyFilePath = "(.\MyFolder\" + $MyFileName + ".txt)"
$ServerList = Get-Content -Path $MyFilePath

But I keep getting the error:

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

It works if I hard code the file path

$ServerList = Get-Content -Path (.\MyFolder\MyFile.txt)

Write-Host $MyFilePath
.\MyFolder\testfile.txt
4
  • 1
    Don't use parentheses in the filename unless the files literally contain them. -Path expects a string or string array. In your case, that would be -Path '.\MyFolder\MyFile.txt'. Commented Jun 20, 2019 at 16:49
  • tried removing them, still the same Commented Jun 20, 2019 at 16:51
  • please, unless you NEED the parens around your Get-Content section, remove them. they are unusual enuf that they are distracting from the newly changed point of your question. if you do need them, please explain why. Commented Jun 20, 2019 at 17:27
  • I don't specifically need them, I am new at powershell and I basically come up with this code from various sources, i'll update my question once i get home Commented Jun 20, 2019 at 17:31

4 Answers 4

2

If you look at the variable, the string literally has parentheses in it:

$MyFileName = "testfile"
$MyFilePath = "(.\MyFolder\" + $MyFileName + ".txt)"
$myfilepath

(.\MyFolder\testfile.txt)

This would work:

$MyFileName = "testfile"
$MyFilePath = ".\MyFolder\" + $MyFileName + ".txt"
$myfilepath

.\MyFolder\testfile.txt

You could put the parentheses on the outside, but you don't need to. Or

".\MyFolder\$MyFileName.txt"
Sign up to request clarification or add additional context in comments.

Comments

1

try setting the full file path like

$MyFilePath = "C:\My Folder\My File.txt"

or if you the relative path is really what you want remove the brackets like

$MyFilePath = ".\My Folder\My File.txt"

6 Comments

tried it, i still get the same error. I edited my question because the actual filename is a variable in itself minus the file extension .txt
try using Join-Path like $MyFilePath = Join-Path -Path "C:\My Folder" -ChildPath $MyFileName if the filetype is missing add this line $MyFilePath += '.txt'
still the same, i even tried $MyFilePath = "C:\My Folder\{0}.txt" -f $MyFileName i get the correct result when I write-host $MyFilePath. The issue is when using the $MyFilePath variable for the -Path option on Get-Content
so what is $MyFileName really? what does $MyFileName | Get-Member return?
TypeName: System.String
|
1

here's one way to do what you seem to want. [grin] the 1st part is your code with the very peculiar resulting file name. the 2nd part is broken out into parts that are easier to read/understand/modify.

$YourFileName = "testfile"    
$YourFilePath = "(.\MyFolder\" + $YourFileName + ".txt)"

$BaseName = 'testfile'
$Extension = 'txt'
$FileName = $FileName, $Extension -join '.'
$Directory = $env:TEMP
$FullFileName = Join-Path -Path $Directory -ChildPath $FileName

$YourFilePath
$FullFileName

output ...

(.\MyFolder\testfile.txt)
C:\Temp\testfile.txt

note that your code made a file name that is almost certainly invalid. [grin]

2 Comments

I think the issue stems from me using Get-Content inside a Workflow for parallel processing, when I take the code outside of the workflow, it worked flawlessly. I thought the issue was just with that block of code but I was mistaken, I will update my question.
@Ruben_PH - ah! that changes things a good deal. as long as you fix that horribly broken file name maker code, folks can focus on other aspects. [grin]
0

I found out that the issue is on using a variable inside a workflow. I was so focused on that block of code that I forgot to look at the bigger picture.

I have the code below which had the issue:

Workflow GetServerStatus{
   $ServerList = Get-Content -path $FullFileName
   $ServiceList = Get-Content service_list.txt
   ForEach -Parallel ($Server in $ServerList){
      InlineScript{
         Get-Service -ComputerName $Using:Server -name $Using:ServiceList
      }
   }
}

#credits to @Lee_Dailey
$Extension = 'txt'
$FileName = $BaseName, $Extension -join '.'
$Directory = '.\server'
$FullFileName = Join-Path -Path $Directory -ChildPath $FileName

GetServiceStatus

It turns out that the issue is that I am not passing the variables correctly to a workflow It should be:

Workflow GetServiceStatus{
    param(
        $FullFileName
    )

It is then called like so

GetServiceStatus $FullFileName

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.