Auto-entering Password In Sn.exe

前端 未结 7 2050
猫巷女王i
猫巷女王i 2021-01-05 01:58

I need to create post build event to perform the following:

sn -i MyKey.pfx MyKeyContainerName
tlbimp $(ConfigurationName)\\MyCom.tlb /out:$(ConfigurationNam         


        
7条回答
  •  醉梦人生
    2021-01-05 02:41

    I needed this to be automatized and found this question. Following this answer (many thanks @Thomas Rijsewijk) I've written my version of it:

    # Start the sn.exe process
    Start-Process $SnExePath -ArgumentList "-i $PfxCertificatePath $LocalContainerName" -NoNewWindow
    # Wait for the process to start
    Start-Sleep 2
    # This workaround allows to forward the password to the standard input
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    [System.Windows.Forms.SendKeys]::SendWait("$($PfxCertificatePassword){ENTER}")
    Start-Sleep 2
    # This ENTER is to return from the sn.exe process
    [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
    

    Using the -Wait switch of the Start-process command did not solve the problem because the PS script was waiting the sn.exe process to terminate before forwarding the password to it.

提交回复
热议问题