从第一个调用另一个powershell脚本
问题描述:
我是一个非常新手的PowerShell。 我刚刚在power shell中学习了脚本hello world。从第一个调用另一个powershell脚本
但是,我有一个任务,我需要完成。
从我的hello world powershell(说'脚本一'),我想调用另一个powershell(说'脚本二'),我可以做。 但在脚本二中,我想通过不同的凭据。
所以脚本一应该用我提到的凭据调用脚本二。
任何人都可以帮助我。
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]**
答
在第一个脚本试试这个:
$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'"
但是,在一个脚本保持credendials是个坏习惯,所以你可以用这个代替(会提示你输入登录/密码的第二个脚本):
$credential = Get-Credential