How Do I Reference Local Files When Running Script on Remote Computer


I am able to connect to the remote host via pssession and run .PS1 files (located on my local computer) on that host. I am trying to run the script below on the remote host which runs, however it cannot locate the files “userlist.csv” and “addusers.ps1” (both files are located on my local computer). I suppose I am not referencing or pointing to the files correctly (“userlist.csv” and “addusers.ps1”) within the script. Any help is appreciated! Thanks!

$ComputerName = “localhost”
$objOu = [ADSI]“WinNT://$ComputerName”

$csv = Import-Csv “c:\Scripts\userlist.csv”
foreach ($_ in $csv) {
$Account = $.userid
$Password = $
$objUser = $objOU.Create(“User”, $Account)
$objUser.userflags = 65536 -bor 0x10000 #ADS_UF_DONT_EXPIRE_PASSWD flag is 0x10000
$adsi = [ADSI]“WinNT://$ComputerName/administrators,group”

del “c:\scripts\addusers.ps1”
del “c:\scripts\userlist.csv”

When a script is running on a remote computer, it is referring to its local resources. There’s no implicit connection back to your computer. So you either need to stage those files on the remote machine, or make them available via a file share (e.g., a UNC path).

Thanks Don! We originally considered copying the files to the target (easy to do). We thought it would be the most convenient if the targets could “see” our clients (which reside on our internal network). So to facilitate UNC back to the client, the client would have to be “NATTED”…meaning the internal client IP would have to be assigned a public address.

Question: if we reference a UNC path inside the remote session (SSL), does the UNC reference open a new SMB connection outside of the remote session? OR does it use SMB wrapped inside the SSL transmission? I know we could WireShark this, but just curious if you know of the particulars.

Thanks again!

So, are you using “SSL” or PowerShell Remoting? Just curious, it doesn’t impact the answer. There’s no such thing as “SMB wrapped inside” anything; the remote machine would open a new, normal SMB connection to whatever UNC path you told it to use. That would need to work from both a connectivity and an authentication perspective - and the connection may been seen by the “file server” as Anonymous.

But that actually does beg the question of whether you’re using SSL (did you mean SSH?) or Remoting. Because there are better ways to do this.

We are PS remoting via certificate authentication (cert-mapping (SSL cert)).

O-Kay, so we’d just call that “Remoting,” not SSL.

So here’s what you want to do. You want to change your approach.

Whatever code you send to the remove machine should, ideally, produce objects. Let me show you a super-simple example.

$local = Import-CSV whatever.csv
Invoke-Command -Arg $local -ComputerName SERVER -Script {
  param( $data )
  $props = @{ 'Property1' = 'Value1'
              'Property2' = 'Value2' }
  New-Object -Type PSObject -Prop $props

} | Export-CSV filename.csv -Append

The remote computer produces objects, which have properties, and those properties possess values. Those come back to your machine via the Remoting connection, and YOUR machine appends that data to a CSV, or puts it in a new CSV, or whatever. Notice, too, that the INPUT CSV was read in LOCALLY, and passed into the remote script block. The remote machine will see that entire data set in $data, although I didn’t use it in my example.

So you pass objects back and forth. Any writing to contents on your computer happens on YOUR computer. You don’t want one computer reaching out to another to write to a CSV file, and you don’t want to be copying files back and forth and all that crap.

Make kinda sense?

Hi Don,

I understand - was able to get everything to work just fine by not calling too many PS1’s from the primary script. This allowed the primary to see the multiple sessions/variables are the script was running. In the end, everything ran as expected.