根据我在这个网站上发现的一些其他建议,我已经设法将大部分路径转移到我想要的位置,但是当我在链中传递额外的变量时我会陷入困境。
我需要以管理员身份运行特定脚本,因此我创建了一些不同的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 %2This 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 $credIf 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!
更多推荐
发布评论