我们有一个PowerShell脚本文件(.ps1),它要求用户从PowerShell控制台提供输入,并将其作为参数传递给另一个PowerShell脚本文件(.ps1),该脚本文件还要求用户提供输入。为了获取输入,我们使用"Read-Host"。
以下是代码片段,在执行下面的代码时,test2脚本不会要求输入,进程会停止/暂停。
有人能帮我如何实现这个用例吗?
以下是代码片段,在执行下面的代码时,test2脚本不会要求输入,进程会停止/暂停。
有人能帮我如何实现这个用例吗?
Code Test1.ps1
[System.String] $ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$new_ip = Read-Host -Prompt "Enter IP"
if([string]::IsNullOrWhiteSpace($new_ip))
{
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "PowerShell.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.WindowStyle = 'Hidden'
$pinfo.CreateNoWindow = $True
$pinfo.Arguments = "-file `"$ScriptPath\Test2.ps1`" -ServerIP `"$new_ip`""
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$stderr = $p.StandardError.ReadToEnd()
$stdout = $p.StandardOutput.ReadToEnd()
$p.WaitForExit()
if (![string]::IsNullOrEmpty($stderr))
{
Write-Warning "stderr: $stderr"
}
if($p.ExitCode -ne 0)
{
$isValidIP = $false
Write-Warning " failed, Check above message"
}
else
{
Write-Host $stdout
$isValidIP = $true
Write-Host "Configured successfully"
}
}
**Test2.ps1**
Param([String]$ServerIP = $null
)
Write-Host "Server IP:"$ServerIP
$SqlServerIP = (Read-Host -prompt "Enter Sql Server IP address (Default : localhost) ")
Write-Host "Server IP:"$SqlServerIP
$new_ip = Read-Host -Prompt "输入IP"; .\script2.ps1 -ServerIP $new_ip
- undefinedRead-Host
?使用强制参数不是更好吗? - undefinedRead-Host
提示,你会使用什么值? - undefined