PowerShell – Multiple (-and) (-or) within a single If statement

I had a hard time finding examples on the web on how or if you could code multiple If’s into a single If statement, so I thought I would just quick post up an example once I figured out how to do it. Perhaps I wasn’t looking in the right places but all the examples I saw were just a single (-and) or (-or) in the code.

So based on being able to code an If statement in either of the following fashions:

If ($someAttribute -eq $false -or $someAttribute -eq $Null){

}

or the following:

If (($someAttribute -eq $false) -or ($someAttribute -eq $Null)){

}

Then we should be able to do a combined effort:

If (($someAttribute -eq $false -or $someAttribute -eq $Null) -and ($someUser -eq “UserID123”)){
       Write-Host “Switching to True”
       Set-QADUser $someUser -Service “MyDomain.org” -ObjectAttributes @{$someAttribute = $True}
       If (!$?) {Write-Host “Error: $($error[0])”}
}

So if $someAttribute is either $false or $Null AND #someUser equalsUserID123″ then do something… in this case I switch the Attribute someAttribute to $true and do some error checking.

~ITNotes

PrimalForms/PowerShell – Event Behavior: Enter Key and Active Directory Lookup

The next event behavior I wanted to cover was hitting a Enter Key within a textbox control and have it trigger another event.

In this examble I have three basic controls a textbox,a button and a listbox.  I am using the textbox as a Search Textbox, after entering text to search the end user can either press Enter or the ‘find’ button.

First add the Event Handler for the Textbox to response only to the Enterkey.

Next add the Event Handler to search Active Directory using the text from the TextBox($txbFind) after the button($btnFind) is single clicked.

Next the code for handler_button1_Click. First the Event handler clears the listbox ($lstResults) then it searches Active Directory with the text from the Textbox ($txbFind). Then if the results if not null, the handler will list the results in the listbox($lstResults)

$handler_btnFind_Click={
$lstResults.Items.Clear()
$Users = get-qaduser $txbFind.text -service ‘MyDomain.edu’ -sizelimit 0 -includedproperties displayName | Sort displayName | select displayName
 if ($Users -ne $Null) {
  foreach ($User in $Users){
   $lstResults.Items.add($User.displayName)
  }
 }
}

Lastly we add code to the Event handler for the textbox to trigger the button($btnFind) when the Enter Key is pressed and give focus to the listbox($lstResults).

$handler_txbFind_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
If ($_.KeyChar -eq 13){
 $btnFind.PerformClick()
 $lstResults.Focus()
}
}

The results show the matching users in the listbox

Using this code I created an admin tool that modifys anything from address to exchange administration. Happy Coding!

PrimalForms/PowerShell – Event Behavior: Button Click and Switch Tab

I decided that I would post how to code a few Event Behaviors that could be coded with PowerShell using PrimalForms. You could just use your basic PowerShell Editor but PrimalForms is just my tool of choice.

So I start with a basic form (Two Tabs and a Button)

Then on the Events tab of $button1, double click ‘click’

Add the following code to the Event handler:

 $handler_button1_Click={
#TODO: Place custom script here
     If ($tabpage1.Visible -eq $true) {
          $tabcontrol1.SelectTab(‘tabpage2’)
     }
     ElseIf ($tabpage2.Visible -eq $true) {
           $tabcontrol1.SelectTab(‘tabpage1’)
     }
}

Clicking the button switches from tab to tab. Way cool!

Glimspe of the PrimalForms 2011 and PrimalScript 2011 Release?

I have been waiting impatiently for the release of PrimalForms 2011, my current project requires multi-form support so the sooner I can get my hands on this the better.

Today I found the product pages for PrimalForms 2011 and PrimalScript 2011. Neither product is listed under their current product list.

PrimalForms 2011: http://www.primaltools.com/products/primalforms2011.asp

PrimalScript 2011 http://www.primaltools.com/products/primalscript2011.asp

Simple Script to find MSI errors

Here’s a simple PowerScript to pull out MSI errors out of the EventViewer and the MSI logs.

Baiscally the script first clears the log file then clears everything out of the Temp directory. Next the script prompts for the admin to start the installation, once you are done installing the program click ok to continue. The script will then collect the error messages from the MSI logs and the last three windows installer entries from the EventViewer. If there are no errors then it will return sucessful installs entries from the Event Viewer and nothing from the logs. In short this script is just a time saver from having to dig though the event logs and the temp directory for the msi logs and then having to go through those long MSI logs. Troubleshooting the error codes will still need to happen outside of the script.

Be sure to enable Windows Installer logging before beginning. This can be set with the following PowerShell command line.

Set-ItemProperty -path “HKLM:\SOFTWARE\Policies\Microsoft\Windows\Installer” -name “Logging” -value voicewarmup

Here’s the final code:

MSIErrorExtractor.ps1

?View Code POWERSHELL
 
Clear-Content errors.txt
 
get-Childitem $env:Temp | remove-Item -force
 
$a = new-object -comobject wscript.shell
$b = $a.popup("Please run installation and click ok once installation is complete",0,"Waiting for MSI Installation and User Input",1)
 
Get-EventLog Application | ? {$_.Source -eq "MSIInstaller" } |  Select-Object TimeGenerated,Source, EntryType, Message -First 3| Format-List | out-file errors.txt
#-and $_.EntryType -eq "Error"
 
filter finderrors { if ($_.contains("Error")) {$_}}
Get-Content $env:temp\MSI*.log | finderrors | Out-File -Append errors.txt | notepad errors.txt

PowerShell 2.0 Release Candidate for Windows Vista and Server 2008

Still waiting for PowerShell 2.0 for Windows XP and Server 2003, but this is a good start.

Read more from the source: http://blogs.msdn.com/powershell/archive/2009/08/14/powershell-2-0-for-windows-vista-and-windows-server-2008-release-candidate.aspx

Download here: https://connect.microsoft.com/windowsmanagement/Downloads