none
WriteAllBytes method throws error in Windows Power Shell version 2.0 RRS feed

  • Question

  • Dear all,

    I'm new to powershell and trying to parse the value in WriteAllBytes method, after looping through the filenames with specific extension in a directory. I'm getting an error 'Exception calling "WriteAllBytes" with "2" argument(s): "Empty path name is not legal."'

    The goal is to read the filename and the file contents in host machine, and create the same file name with same contents in a remote system, under a different directory.

    Script runs smoothly on hard coding complete path name, but that's not the requirement. Below is the sample code which I am using. Valuable suggestions are much appreciated.

    $path = $Env:userprofile+"\AppData\Local\Temp\Remote_Data\Test_Data\"

    $servers = @("xyz")
    foreach($server in $servers) 
    {
        $username = 'UN'
        $password = 'PWD'
        $pw   = ConvertTo-SecureString $password -AsPlainText -Force
        $cred = New-Object Management.Automation.PSCredential ($username, $pw)

        foreach ($file in Get-ChildItem $path -Filter *.txt)
            {
            $TextFileName = $file.Name
            
            $outPutFile = "D:\Logs\" + $TextFileName

            $myfile = [System.IO.File]::ReadAllBytes($path+$file)
            $s = New-PSSession -computerName $server -credential $cred
            
            Enter-PSSession $s
            Invoke-Command -Session $s -ArgumentList $myfile -Scriptblock {[System.IO.File]::WriteAllBytes([string]$outPutFile, $args)} 
            Remove-PSSession $s
            }
    }
    Write-Host "Completed"


    Tuesday, April 10, 2018 1:20 PM