简体   繁体   English

远程运行 PowerShell 脚本

[英]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.所以我在服务器上有一个脚本,我可以远程使用 Enter-PSSession 并从我的本地环境运行脚本就好了。 What I do is... Enter-PSSession hostname cd c:\\temp\\myscript.ps1我所做的是... 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.如果我通过 RDP 直接在主机名上运行 .ps1,它会起作用。 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.如果我 RDP 进入服务器并返回到我使用上面的代码输入 PSSession 的本地环境,它会很好地提交到数据库。 But when I close out of the RDP session and just try running the script again locally, it doesn't go to the database.但是当我关闭 RDP 会话并尝试在本地再次运行脚本时,它不会转到数据库。

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.重要的部分在 myscripts.ps1 中,因此无法从您提供的内容中说出来。

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.尝试使用 $psexecscript 作为配置文件路径的根。 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.通过这种方式,您的凭据也会被存储,您可以安全地使用“运行方式”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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