简体   繁体   中英

Running a PowerShell script remotely

So I have a script sitting on a server that I can remote into using Enter-PSSession and run the script from my local environment just fine. What I do is... Enter-PSSession hostname cd c:\\temp\\myscript.ps1

or

$s = New-PSSession -ComputerName hostname
Invoke-Command -Session $s -ScriptBlock {C:\temp\myscript.ps1}

these run the script just fine. The problem I am having is the data is supposed to get sent to a database. If I run the .ps1 on the hostname by RDPing directly into it, it works. If I RDP into the server and go back to my local environment where I Enter-PSSession using the code above, it submits to the database just fine. But when I close out of the RDP session and just try running the script again locally, it doesn't go to the database.

Maybe it is something stupid I am missing but any help would be greatly appreciated.

The important bits are in the myscripts.ps1, so it's impossible to say from what you have provided.

I suggest you check that script file for where the credentials are stored. When you run it locally, I assume you run it from that working directory.

Try using $psexecscript as the root of the config file's path. That's whatever path the script is in, assuming the credentials are in the same or sub- directory.

Is it possible that the user lacks permission to write in your DB or the credentials to write in that DB are not provided in the script?

If after verifying the credential issue, it still doesn't work, you might want to register your script as a task. You can set it as a manual task. Then, whenever you need it, you (remotely) run that task. In that way your credentials are stored as well and you can safely use the 'run as' feature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM