none
Get-Content with Invoke-Command - ItemNotFoundException RRS feed

  • Question

  • Trying something new, using Invoke-Command to install software, using this script.

    $computername = Get-Content 'c:\Computers.txt'
    
    $sourcefile = "\\server\path\install.exe"
    
    foreach ($computer in $computername) 
    
    {
    
    $destinationFolder = "\\$computer\C$\Temp"
    
    if (!(Test-Path -path $destinationFolder))
    
    {
    
    New-Item $destinationFolder -Type Directory
    
    }
    
    Copy-Item -Path $sourcefile -Destination $destinationFolder
    
    Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c "c:\Temp\Install.exe" /s /v" /l*v ""%TEMP%\install.log"" /qn"}
    
    }

    That script will result in:

    Get-Content : Cannot find path 'C:\Computers.txt' because it does not exist.
    At D:\PowerShell\RemoteInstall.ps1:10 char:1
    + Get-Content 'c:\Computers.txt' | ForEach-Object {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Computers.txt:String) [Get-Content], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

    But if specify computers ($computername = 'test-vm-001.test.com,test-vm-002.test.com'), it works fine.

    Also if I just type Get-Content 'c:\Computers.txt, I get a list of what is in computers.txt in console.

    I tried dropping copies of Computers.txt at the same location as the script, and on the remote computers with no change in results.

    What am I missing, that ForEach doesn't like about using Get-Content?



    There's no place like 127.0.0.1

    Wednesday, April 20, 2016 8:40 PM

Answers

  • :D :D :D

    Stupid error.  Server I was editing the script on, wasn't configured to show file extensions, so my text file was actually "Computers.txt.txt".  Doh! :D


    There's no place like 127.0.0.1

    • Marked as answer by Matt5150 Wednesday, April 20, 2016 9:12 PM
    Wednesday, April 20, 2016 9:12 PM