首页 文章

Windows Server启动脚本上的PSExec

提问于
浏览
-1

我是以编程方式启动运行Windows Server 2016的Google Cloud 计算实例,其中包含启动脚本 .

启动脚本中的可执行文件需要作为特定用户启动,因此我尝试使用 psexec 启动它以模拟所述用户:

C:/psexec.exe \\\\WIN-SERVER-2016 -u WIN-SERVER-2016\\customuser -p custompassword -accepteula -w "c:/app" cmd /c node index.js

c:/app/index.js 包含一个简单的hello世界,它应该写入文件 .

如果我以任何用户身份登录并从cmd启动此命令,则会写入该文件 . 从启动脚本(在Google Cloud Compute Engine实例中作为 windows-startup-script-cmd 提供)启动会导致无法写入文件 .

可能是什么解决方案?有没有更有效的方法来执行作为特定用户的启动脚本?

1 回答

  • 1

    看看这个问题,我不建议您使用PSEXEC .

    最重要的是,我们使用PSExec来调用PS本身不支持的远程系统中的GUI .

    在你的情况下,我建议你运行 Invoke-Command

    Something like this:

    $username = 'WIN-SERVER-2016\customuser'
    $password = "custompassword"
    $secstr = New-Object -TypeName System.Security.SecureString
    $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
    
    $Script_block = {cmd /c  node index.js}
    
    Invoke-Command -ComputerName  WIN-SERVER-2016 -Credential $cred -ScriptBlock $Script_block
    

    如果您使用的是windows-startup-script-cmd,这也应该从元数据键中获取

    注意:我没有考虑过acceptteula -w "c:/app"部分 . 请相应地合并占位符 .

    希望能帮助到你...!!!

相关问题