1

I've lost a handful of hair today trying to figure out the best way to grab the data from the clipboard on a remote server. I thought simply:

$cred = New-Object System.Management.Automation.PSCredential('USERNAME', $password);
Invoke-Command -ComputerName 'theFQDN' -Credential $cred -ScriptBlock {
Get-Clipboard
}

would do the trick but it's always null and nothing is returned, no errors. I've tried doing with commandline:

$cred = New-Object System.Management.Automation.PSCredential('USERNAME', $password);
Invoke-Command -ComputerName 'theFQDN' -Credential $cred -ScriptBlock {
cmd.exe /c "powershell Get-Clipboard"
}

Manually, it works fine and dandy, but passing it through Invoke-Command returns nothing and I've most certainly confirmed it has data in the clipboard.

Any help is appreciated.

ADDENDUM (after realizing this might be more complicated than originally thought): The whole process is that an app will automatically copy some string to clipboard on a remote server. I need to grab that data off the clipboard and was hoping the Invoke-Command would do it for me.

Mike
  • 29
  • 6
  • From an interactive session? The cred you’re using is the same as that session? – Doug Maurer Oct 16 '20 at 00:09
  • 1
    The moment I read "interactive" is when I started to panic. This will only get from clipboard if something is copied to it from the same Invoke-Command session won't it? Looks like I need to update my question a little. Nuts. There will be something that will automatically copy some text to clipboard on a remote server and then I need to grab that data from the clipboard. Is it possible? – Mike Oct 16 '20 at 01:19
  • 2
    As an aside: Please avoid pseudo method syntax: instead of `New-Object SomeType(arg1, ...)`, use `New-Object SomeType [-ArgumentList] arg1, ...` - PowerShell cmdlets, scripts and functions are invoked like _shell commands_, not like _methods_. That is, no parentheses around the argument list, and _whitespace_-separated arguments (`,` constructs an _array_ as a _single argument_, as needed for `-ArgumentList`). – mklement0 Oct 16 '20 at 01:30
  • 2
    Just copy the text to a file and grab it there :) – Doug Maurer Oct 16 '20 at 02:20
  • 1
    @mklement0 or, if he prefers (v5) `[System.Management.Automation.PSCredential]::new('USERNAME', $password)` – marsze Oct 16 '20 at 10:10
  • @DougMaurer I was hoping for a more direct approach and was curious if there was one. IF there truly isn't, I suppose I could make the app paste the clipboard data to a file. It's a little messy for what I need to though. – Mike Oct 16 '20 at 13:04
  • @DougMaurer Actually, I'm running into the same issue. I just can't get access to the clipboard (It's a function I am testing so I have to grab from clipboard). – Mike Oct 16 '20 at 13:13
  • Why not create a scheduled task on the remote machine that runs interactively in the user context and get the clip board from there? – Dennis Aug 26 '21 at 21:00

0 Answers0