File operation failed with New-PSSession

Ray Cheng

I'm loading a json file that contains a list of computer names and each of them contains a list of files I need to operate on. For this example, I'm just displaying the file sizes.

But I'm getting file not found errors. I think it's because new-pssession is not activated or opened. I confirmed the file does exist on the remote computers. Is there something I need to do to "activate/open" the session after new-pssession?

$cred = Get-Credential -UserName admin -Message "Enter Password" 
$computers = Get-Content "sample.json" | ConvertFrom-Json    
foreach($computer in $computers){
    $s = new-pssession -ComputerName $computer.computer -Credential $cred
    foreach($file in $computer.files){        
        write-host $file has (get-item $file).length "bytes"
    }
    remove-pssession $s 
}

json file

[
    {
        "computer": "machine1",
        "files": [
            "c:\\temp\\done.png",
            "c:\\temp\\Permissions.xlsx"
        ]
    },
    {
        "computer": "machine2",
        "files": [
            "c:\\software\\TortoiseSVN.msi",
            "c:\\software\\TortoiseSVN.txt"
        ]
    }
]
Santiago Squarzon

As mklement0 points out in his helpful comment, New-PSSession will only establish the persistent connection with the remote hosts, however if you need to execute code on them you would need to use Invoke-Command.

I have removed New-PSSession for this example as there is no need for it in this case, but note, when using a PSSession, you would be using the -Session parameter instead of -ComputerName.

$cred = Get-Credential -UserName admin -Message "Enter Password" 
$computers = Get-Content "sample.json" | ConvertFrom-Json
foreach($computer in $computers)
{
    Invoke-Command -ComputerName $computer.computer {
        $computer = $using:computer
        foreach($file in $computer.files)
        {
            Write-Host "$file has $((Get-Item $file).Length) bytes"
        }
    } -Credential $cred 
}

Since Invoke-Command allows parallel execution of script blocks on remote hosts, the same can be done with your code, however it would be a bit more complex. $computers would be passed to each remote session and each host would need to figure out which object of the object[] has to run:

$cred = Get-Credential -UserName admin -Message "Enter Password" 
$computers = Get-Content "sample.json" | ConvertFrom-Json
Invoke-Command -ComputerName $computers.computer {
    $computers = $using:computers
    $files = $computers.Where({ $_.computer -eq $env:ComputerName }).files
    foreach($file in $files)
    {
        Write-Host "$file has $((Get-Item $file).Length) bytes"
    }
} -Credential $cred

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

New-PSSession to localhost fails

New-PSSession Parellel Execution

systemctl: Failed to execute operation: No such file or directory

apt upgrade: Failed to execute operation: No such file or directory

New-PSSession Cmdlet using Oauth credentials

New-PSSession does not work locally

"file or LOB operation FILEOPEN failed" when loading an XML file into a table

multiprocess open a same file cause file operation failed

Returning a file from a pssession to its invoker

failed to save new file with time stamp

iTunes Store Operation Failed, Missing required icon file

reCaptcha file_get_contents(): SSL operation failed

PHP: file_get_contents(): SSL operation failed with code 1

Program 'tsc' failed to run: No application is associated with the specified file for this operation

How to fix "the save file operation failed to connect to the open and save panel"

PowerShell 5.1 What is wrong with my New-PSSession syntax

Copy-Item from New-PSDrive within PSSession

New-PSSession Fails Ubuntu to Windows using .NET Core Packages

New-MailboxExportRequest don't work in remote PSsession

Error: new-PSSession:One or more computer names is not valid

Why is invoke-command and new-pssession not working?

New-PSSession using python only (no subprocess module)

file_get_contents(): SSL operation failed with code 1, Failed to enable crypto

Migrating to the New Places SDK Client | E/SchedPolicy: set_timerslack_ns write failed: Operation not permitted

Failed to create new java File for path /storage/emulated/0/

Synchronize operation failed

zramswapon failed: Operation not permitted

reagentc: Operation failed: 70

Ipadm:operation failed