Disabling Installshield's Next button with powershell - powershell

I have an Installshield project that uses powershell custom actions.
In one of my dialogs, I'm asking from the user to enter username and password, then I validate the credentials (with powershell) and I want to enable the Next button only if the credentials were correct.
Can this be achieved with powershell action item? The reason I'm using powershell is that I don't know InstallScript at all.
Here is my powershell script so far:
Function Test-UserCredential {
Param($username, $password)
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Machine, $env:computername
$opt = [System.DirectoryServices.AccountManagement.ContextOptions]::SimpleBind
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ct
$Result = $pc.ValidateCredentials($username, $password).ToString()
$Result
}
$comp_username = Get-Property -Name COMPUTER_USERNAME
$comp_password = Get-Property -Name COMPUTER_PASSWORD
$result = Test-UserCredential -username $comp_username -password $comp_password
if ($result)
{
#Enable "Next" button
}
else
{
#Disable "Next" button
}
Thanks.

There are three things you will have to do.
Choose a property that tracks whether the Next button should be enabled, and set that from your PowerShell. Typically you will set it to "1" or "" (empty string) for ease in the next step.
Create Control Conditions in the dialog editor that Enable and Disable the Next button referencing the property as your condition.
Separately trigger the UI to update after the powershell action completes. Unfortunately the UI does not evaluate control conditions after all property changes; it only does so after it changes a property it thinks is related. So the easiest way to do this is to add a Set Property control event that sets the property.
Note that for clarity of step 3's relevance, it can be useful to split this into two separate properties; set one in the powershell, reflect that into another in the Set Property control event, and have control conditions that read the latter.

Related

Powershell - Replace add_Click link on LinkLabel

I am creating a PowerShell GUI that uses a link label. My code for this link is
$ExLinkLabel = New-Object System.Windows.Forms.LinkLabel
$ExLinkLabel.Location = New-Object System.Drawing.Size(15,130)
$ExLinkLabel.Size = New-Object System.Drawing.Size(150,20)
$ExLinkLabel.LinkColor = "BLUE"
$ExLinkLabel.ActiveLinkColor = "RED"
$ExLinkLabel.Text = "Link Example"
$ExLinkLabel.add_Click({[system.Diagnostics.Process]::start("https://google.com")})
$Form.Controls.Add($ExLinkLabel)
Now say I want to change it another website later in the code based on certain conditions, I tried doing this:
$ExLinkLabel.add_Click({[system.Diagnostics.Process]::start("https://yahoo.com")})
The problem that this now has two links open, both google and then yahoo.
Is there a way to clear or just replace that first link with my new one?
Thank you
Adding an event handler with an .add_<EventName>() method call does just that: It adds an additional event handler, of which there can be many.
In order to replace an event handler, you must first remove its old incarnation with .remove_<EventName>(), and then add the new incarnation with .add_<EventName>().
To that end, you must store the original incarnation in a variable that you can later pass to the .remove_EventName>() call:
# Define the original event handler, store it in a variable,
# and add it to the control.
$currEventHandler = { Start-Process https://google.com }
$ExLinkLabel.add_Click($currEventHandler)
# ...
# Remove the current event handler...
$ExLinkLabel.remove_Click($currEventHandler)
# ... and add the new one:
$currEventHandler = { Start-Process https://yahoo.com }
$ExLinkLabel.add_Click($currEventHandler)
Note that I've replaced [system.Diagnostics.Process]::start($url) with a simpler, PowerShell-idiomatic call to the Start-Process cmdlet.
In your simple case, where the two event handlers only differ by the URL they open, consider the alternative recommended by Theo:
Retain the original event handler and make it retrieve the URL to open from a variable defined outside the event handler, namely in the script scope. That way, all you need to do is to update the variable.
# Set the original URL
$url = 'https://google.com'
# Due to PowerShell's dynamic scoping, the event-handler script
# block - even though it runs in a *child* scope - sees the
# script scope's definition of variable $url
# You can make this cross-scope access explicit by using $script:url
# (If you wanted to *update* the variable from inside the child
# scope $script:url *must* be used.)
$ExLinkLabel.add_Click({ Start-Process $url })
# ...
# Update the variable, after which the event handler will
# use the new URL.
$url = 'https://yahoo.com'

Using a PowerShell Script to change the default tray selected on a printer

I've been looking online at ways to automatically change the default tray on a printer. I'm in need of this because my job uses printers via an old IBM server, and these specific printers don't seem to respond to any instruction from the IBM server to change the tray used for the job. My only option to automatically change the preferences of a printer seems to be through a PowerShell script. I found a script on Stack that seems to achieve most of the functionality I want, however, I have very limited scripting knowledge and as a result, I'm unsure how to add the additional functionality required. First off, here's the code:
$Printer = "Example Printer Name"
$InputBin = "AutoSelect","AutoSheetFeeder","Cassette","Manual","Tractor" #choose one
Add-Type -AssemblyName System.Printing
$Permissions = [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
$QueuePerms = [System.Printing.PrintSystemDesiredAccess]::AdministratePrinter
$PrintServer = new-object System.Printing.LocalPrintServer -ArgumentList $Permissions
$NewQueue = New-Object System.Printing.PrintQueue -ArgumentList $PrintServer,$Printer,1,$QueuePerms
$InputBinCaps = $NewQueue.GetPrintCapabilities().InputBinCapability
if ($null -ne $InputBinCaps) {
if ($InputBinCaps.Contains([System.Printing.InputBin]::$InputBin)) {
$NewQueue.DefaultPrintTicket.InputBin = [System.Printing.InputBin]::$InputBin
$NewQueue.UserPrintTicket.InputBin = [System.Printing.InputBin]::$InputBin
} else {
Write-Error "$InputBin unavailable on $Printer"
}
}
$NewQueue.commit()
$NewQueue.dispose()
$PrintServer.commit()
$PrintServer.dispose()
What I need to add is a way to detect what tray is currently set to default and use that data to direct an if statement to change it to the opposite (I only have two trays on these printers, Cassette 1 and Bypass Tray). Additionally, I need the script to immediately close its window upon execution of the task. The reason being is that I plan to put this as a shortcut on their Windows taskbar and have them click the shortcut every time they need to switch printer trays. I understand this setup is jank, but it is the only way to get the correct type of paper printed, save opening up the default tray, and laying one yellow sheet on top.
Any help would be greatly appreciated.
Thanks

Update GUI in Powershell

I want to write a programm which has a Kind of traffic light funtion. The color of the Frame should be changed depending on an check. I already solved the check of the criterias, so in this example the while-Loop is vicarious for a bigger check. But to focus on the problem i shortend it like this. My problem is that i canĀ“t update the Color of the UI.
So the program should be running all the time, but it seems to be stuck at:
[void] $Form.ShowDialog()
and only will continue after I close the form. So how can i bypass this section to get into the infinit Loop, so that my Form is shown all the time with changing Color?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Test"
$Form.Size = New-Object System.Drawing.Size(300,300)
$Form.StartPosition = "CenterScreen"
$Form.BackColor = "Green"
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
while ($True)
$test = Get-Content -Path C:\Users\admin\Documents\pro.txt
if ($test -eq 2)
{
$Form.BackColor = "Green"
} else {
$Form.BackColor = "Red"
}
Start-Sleep -s 2
}
Thanks for you help!
$Form.ShowDialog()
This line requires interaction on the form. The code following that line will not run until the form is closed. Another way to launch the form would be like the following
$Form.Show()
If you launch the form like that, the code should continue to run. Although you still may need a better solution, as your following code uses start-sleep -s 2. I believe this might also make your form sleep, which would render it potentially unusable.
I would recommend learning about PowerShell Event Handlers here. There might be a better solution using an event handler for your solution. An example of an event handler follows
$Form_Load {
# Your commands and stuff here #
}
The $Form is the object that the event is looking at, then you have a separator _, then Load is the name of the actual event that when executed the code runs. So any of the code inside those brackets will run right away when form $Form is launched. This can be anything from loading a form, clicking a button, or even a selected index changing inside of a listbox.
Event handlers took my PowerShell GUI building abilities to then next level when it comes to what I had the ability to do with the form. So I would highly recommend learning about event handlers if PowerShell GUIs are the normal for you, the most helpful tool you could buy is SAPIEN PowerShell Studio which is an extremely helpful development environment. It is basically a Visual Studio built just for PowerShell. I think it's in the $400-$500 range.

Cannot get Powershell GUI to modify variable

I'm creating a GUI to enter in data then store into some variables to work with later. The problem I'm having is modifying the variables once the user clicks the OK button. I have the following code:
$button4 = New-Object system.windows.Forms.Button
$button4.Text = "button"
$button4.Width = 60
$button4.Height = 30
$button4.Add_Click({
$variable = "test"
})
$variable gets assigned with the Add_Click function but I'm not able to access it anywhere else. This makes sense as I read about Powershell scopes. But how am I supposed to access the information I set in there? I'm using this Microsoft guide as to build the GUI. In their example I should be able to return $x at the end of the script. But when I do it the variable isn't available.

PowerShell IE9 ComObject has all null properties after navigating to webpage

I have a PowerShell script that navigates to a (presumably) classic ASP page on our intranet to stop a Windows Service running on our server as part of the deployment process for that service (and restarts it after deploying the new files). It ran fine until we recently upgraded to IE9. Here's the script.
# Open service page in IE
$ie = new-object -comobject InternetExplorer.Application
$ie.visible = $true
$ie.navigate($serviceUrl)
while($ie.busy) { start-sleep 1 }
# Stop service
$ie.Document.getElementById("dropDownActionList").value = "Stop"
$ie.Document.getElementById("buttonTakeAction").click()
while($ie.busy) { start-sleep 1 }
Now when I run the script, it successfully launches IE, but throws the following error:
You cannot call a method on a null-valued expression.
At C:\Projects\ABC\Scripts\Deploy.ps1:85 char:28
+ $ie.Document.getElementById <<<< ("dropDownActionList").value = "Stop"
+ CategoryInfo : InvalidOperation: (getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
When I investigate in PowerShell, I find that if I create the IE ComObject, it at first has valid properties, but as soon as I navigate to the the service control page, all the properties are null (almost as if the ComObject gone away?). For example, before the HWND property had a valid value, but now it's null ($ie.hwnd -eq $null returns true). No error is displayed in PowerShell when I navigate to the page.
I looked at some similar questions, but the first one doesn't match my circumstance (the Document property is null in my case) and as for the latter one, IE9 defaults to compatibility mode for intranet sites. I saved the ASP page and ran it through the w3c validator and it threw some errors (although none related to the elements I'm trying to deal with). Unfortunately I can't fix those. Other sites don't seem to have this problem. Any suspicions on what the problem may be and recommendations on work-arounds?
I just worked through this.. sort of. I was seeing the same behavior until I turned off protected mode in IE. This seems to have something to do with submitting from one security zone to the next. So.. assuming that your original page is in the internet zone, with protected mode on, you submit to a page in a trusted zone or intranet or whatever, it seems like the COM context is lost. Probably intentional. I'm going to try fixing the zones, and keeping protected mode on.
Hope this helps.
EDIT: This is also a non-issue if you run your powershell in elevated mode (run as admin)
In addition:
http://msdn.microsoft.com/en-us/library/bb625962.aspx
This problem is caused by integrity levels since Internet Explorer 8.
That is also the reason, why the application runs well as administrator.
Since IE-8 runs in "low integrity" mode, it is not possible to automate IE from within a script. This is because the script runs as an user which belongs to "medium integrity" mode. The security design is such that it can send instructions from medium to low integrity, but can not receive data from low to medium integrity.
Update: Here is a working example how to do it without changing any settings. It gets back the lost com-Object.
function ConnectIExplorer() {
param($HWND)
$objShellApp = New-Object -ComObject Shell.Application
try {
$EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
$objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
$objNewIE.Visible = $true
} catch {
#it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
Write-Host "Waiting for page to be loaded ..."
Start-Sleep -Milliseconds 500
try {
$objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
$objNewIE.Visible = $true
} catch {
Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
$objNewIE = $null
}
} finally {
$ErrorActionPreference = $EA
$objShellApp = $null
}
return $objNewIE
}
$HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
$objIE.Navigate("https://www.google.com")
$objIE = ConnectIExplorer -HWND $HWND