使用存储的凭据从批处理文件和传递参数启动第二个Powershell脚本(Launching a second Powershell script using stored credentials, f

编程入门 行业动态 更新时间:2024-10-25 00:30:33
使用存储的凭据从批处理文件和传递参数启动第二个Powershell脚本(Launching a second Powershell script using stored credentials, from batch file & passing parameters)

根据我在这个网站上发现的一些其他建议,我已经设法将大部分路径转移到我想要的位置,但是当我在链中传递额外的变量时我会陷入困境。

我需要以管理员身份运行特定脚本,因此我创建了一些不同的PS1脚本来处理问题。 我已经创建了一个安全密码并将其保存到文件中。

我有一个批处理文件,现在有以下调用:

powershell -ExecutionPolicy RemoteSigned -File "C:\Scripts\ScriptLauncher.ps1" "<DOMAIN>\<username>" "C:\Scripts\pwd.txt" "C:\Scripts\Test.ps1" %1 %2

这传入,用户名,密码文件路径,要运行的脚本和其他两个变量。 *批处理文件中提供了DOMAIN和USERNAME。

在内部ScriptLauncher中,我收集了以下参数:

param( $username, $passwordPath, $scriptToRun, $p1, $p2, $p3, $p4 )

然后我创建要使用的凭证:

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,ConvertTo-SecureString (Get-Content -Literalpath $passwordPath))

现在这是我遇到问题的地方,我可以在使用下面的Keith建议后运行它,同时使用字符串作为我希望运行的脚本的路径。 但是,我正在尝试让这个应用程序能够运行多个脚本,所以我希望能够将路径传递给我拥有的任何PS脚本,然后运行它。

所以考虑到这一点,我现在转到以下代码:

$blockString = '{ param($p1,$p2,$p3,$p4) &"'+$scriptToRun+'" @($p1,$p2,$p3,$p4)}' $scriptBlock = [scriptblock]::Create($blockString) Invoke-Command -ScriptBlock $scriptBlock -ArgumentList @($p1,$p2,$p3,$p4) -ComputerName localhost -Credential $cred

如果我只使用字符串运行上面的Invoke命令,它可以正常工作。 但是使用替换它会运行,但没有任何反应。 有任何想法吗?

Following a few other suggestions I've found on this site, I've managed to get most of the way to where I want to be, but I'm stuck when it comes to passing on extra variables along the chain.

I need to run a particular script as an admin, so I've created a few different PS1 scripts to handle the problem. I've created a secure password and saved it to file.

I have a batch file which now has the following call in it:

powershell -ExecutionPolicy RemoteSigned -File "C:\Scripts\ScriptLauncher.ps1" "<DOMAIN>\<username>" "C:\Scripts\pwd.txt" "C:\Scripts\Test.ps1" %1 %2

This passes in, username, password filepath, script to run, and two other variables. *DOMAIN & USERNAME are supplied in the Batch File.

The inside ScriptLauncher, I have the following arguments getting collected:

param( $username, $passwordPath, $scriptToRun, $p1, $p2, $p3, $p4 )

I then create the credential to be used:

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,ConvertTo-SecureString (Get-Content -Literalpath $passwordPath))

Now this is where I'm having problems, I can get it to run after using Keith's suggestions below, whilst using a string as the path to the script I wish to run. However, I'm trying to get this application to be able to run multiple scripts, so I want to be able to pass in the path to any PS script which I have, and have this run it.

So with that in mind, I've now go the following code:

$blockString = '{ param($p1,$p2,$p3,$p4) &"'+$scriptToRun+'" @($p1,$p2,$p3,$p4)}' $scriptBlock = [scriptblock]::Create($blockString) Invoke-Command -ScriptBlock $scriptBlock -ArgumentList @($p1,$p2,$p3,$p4) -ComputerName localhost -Credential $cred

If I run the Invoke command above just using a string, it works fine. But using the substitution, it runs, but nothing happens. Any ideas?

最满意答案

好的,所以我终于解决了。 感谢基思指出我正确的方向。

因此,为了按照我的意图运行脚本,我需要以下Invoke-Command:

Invoke-Command -ScriptBlock { param($script,$p1,$p2,$p3,$p4) &$script @($p1,$p2,$p3,$p4)} -ArgumentList @($scriptToRun,$p1,$p2,$p3,$p4) -ComputerName localhost -Credential $cred

很高兴现在一切正常!

Okay, so I finally worked it out. Thanks to Keith for pointing me in the right direction.

So, in order to run the script as I entended, I needed the following Invoke-Command:

Invoke-Command -ScriptBlock { param($script,$p1,$p2,$p3,$p4) &$script @($p1,$p2,$p3,$p4)} -ArgumentList @($scriptToRun,$p1,$p2,$p3,$p4) -ComputerName localhost -Credential $cred

Happy it's all working now!

更多推荐

本文发布于:2023-08-07 04:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:凭据   第二个   脚本   批处理文件   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!