Sending keys to Cmdlet in background process

Topics: Developer Forum, User Forum
Feb 18 at 12:20 AM
Edited Feb 18 at 12:23 AM
Hi I am trying to call the function Unregister-AdfsAuthenticationProvider as part of an installer executable. The installer is running my powershell script in a background process i.e. non-interactive. The problem, is that this function requires user interaction to confirm Yes/No, and as far as I know, it does not provide any option to run unattended. When I run the script in an interactive powershell session I can use SendKeys from System.Windows.Forms i.e. :
[Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("Y" + [char]13)
Unregister-AdfsAuthenticationProvider -Name $name
Register-AdfsAuthenticationProvider -TypeName $typeName -Name $name -Verbose;
...and this works, but this obviously does not work when the script is run non-interactively in a background process. I have also tried spawning a new powershell process from the main script and redirecting stdin, but this does not seem to work either i.e. :
$name = "Miracl.ZFA.ADFS.AuthenticationAdapter"
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; 
$psi.Arguments = "Unregister-AdfsAuthenticationProvider -Name '$name'";
$psi.UseShellExecute = $false; 
$psi.RedirectStandardInput = $true; 

$p = [System.Diagnostics.Process]::Start($psi);
$p.StandardInput.WriteLine("Y[char]13");
sleep 2
$p.StandardInput.WriteLine("Y[char]13");
$p.WaitForExit()
When I run the above script as powershell -File script.ps1 it hangs waiting for user input.

Why doesnt Unregister-AdfsAuthenticationProvider read from the redirected stdin?

Andy