简体   繁体   English

从第一个脚本调用另一个Powershell脚本

[英]Calling another powershell script from first one

I am a very newbie to powershell. 我是Powershell的新手。 I just learnt scripting hello world in power shell. 我刚刚学习了在Power Shell中编写脚本hello世界的知识。

But, I have a task which i need to complete. 但是,我有一项任务需要完成。

From my hello world powershell (Say 'Script One') , I want to call another powershell (say 'Script Two'), which I am able to do. 我想从我的世界powershell(说“脚本一”)中调用另一个我可以做到的powershell(例如“脚本二”)。 But in Script Two, I want to pass different credentials. 但是在脚本二中,我想传递不同的凭据。

So Script One should call Script Two with the credentials I mention. 因此,脚本一应使用我提到的凭据调用脚本二。

Can anyone please help me up. 谁能帮我。

Script One (My first script script) :
Write-Host “Hello, World!”
invoke-expression -Command "C:\Scripts\Script Two.ps1"  **[BUT CALL WITH THE CREDENTIALS I WANT]**

Try this in your first script : 在第一个脚本中尝试以下操作:

$credential = New-Object System.Management.Automation.PsCredential("OTHERLOGIN", (ConvertTo-SecureString "OTHERPASSOWRD" -AsPlainText -Force))

Start-Process powershell.exe -Credential $credential -NoNewWindow -ArgumentList "-File 'C:\Scripts\Script Two.ps1'"

But storing credendials in a script is a bad habit, so you could use this instead (will prompt you for login/password for the second script) : 但是将凭据存储在脚本中是一个坏习惯,因此您可以改用它(将提示您输入第二个脚本的登录名/密码):

$credential = Get-Credential

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

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