Multi-Select List Box in Primal Forms


I am still working my way through the learning process in PrimalForms, (excuse me Powershell Studio 2012). I was trying to create a multiselection list box without much success. I posted a plea for help on Sapien’s Scripting Answers board and got pointed to some articles on technet.


Here is the ListBox Control: ListBox Control

Here is the SelectionMode Property: SelectionMode Property

You can retrieve the selected items by enumerating the, what else,
‘SelectedItems property: SelectedItems property

When you build your form, grab the ListBox control from the left hand side of the page and place it on your new form. Size to your delight. Then on the far right side, Under BEHAVIOR, find SelectionMode and then select MultiSimple. This alows you to highlight more than one item in your ListBox. The first list box is named ListBox1 by default. Here is the code I used to process the items selected.


###### Start Posh Script ########

$status = $curItems = @()
	$curItems = $listBox1.SelectedItems
	foreach ($thing in $curItems) {
		$status += $thing
		$statusbar1.Text = "Curent Item = $thing"
		Command($thing)
		}
$richtextbox1.Text = "You selected these items $status"

############# End POSH Script ###############

How do I set up Auto Filters in Excel?

#How do I set up Auto Filters in Excel?

#This function sets up a spreadsheet and then sets Auto filters
Someone asked how to select both Jack and Elizebeth.
$xl.Selection.AutoFilter 2, Jack, $xlAnd, Elizabeth #Jack & Elizabeth items only

Thanks to an anonymous reader, we can now do a more than two.

$xlFilterValues = 7 # found in MS documentation
$filterList = “Jack”,”Elizabeth” # array
$xl.Selection.AutoFilter(2, $filterList ,$xlFilterValues)

###### Start Posh Script ########
 Function XLAutoFilter { 
 $xlTop10Items = 3
 $xlTop10Percent = 5
 $xlBottom10Percent = 6
 $xlBottom10Items = 4
 $xlAnd = 1
 $xlOr = 2
 $xlNormal = -4143
 $xlPasteValues = -4163 # Values only, not formulas 
 $xlCellTypeLastCell = 11 # to find last used cell
 $xl = New-Object #NAME? Excel.Application 
 $xl.Visible = $true
 $xl.DisplayAlerts = $False
 $wb = $xl.Workbooks.Add()
 $ws = $wb.Worksheets.Item(1)
 $ws = $wb.Sheets.Add()
 $ws = $wb.Sheets.Add()
 $ws1 = $wb.worksheets | where {$_.name #NAME? Sheet1} #<------- Selects sheet 1
 $ws2 = $wb.worksheets | where {$_.name #NAME? Sheet2} #<------- Selects sheet 2
 $ws3 = $wb.worksheets | where {$_.name #NAME? Sheet3} #<------- Selects sheet 3
 $ws4 = $wb.worksheets | where {$_.name #NAME? Sheet4} #<------- Selects sheet 4
 $ws5 = $wb.worksheets | where {$_.name #NAME? Sheet5} #<------- Selects sheet 5
 $ws1.Tab.ColorIndex = 8
 $ws2.Tab.ColorIndex = 7
 $ws3.Tab.ColorIndex = 6
 $ws4.Tab.ColorIndex = 5
 $ws5.Tab.ColorIndex = 4
 $ws1.name = Detail
 $ws2.name = JackOnly
 $ws3.name = Top2
 $ws4.name = LowestHighest
 $ws5.name = Top25Percent
 $ws1.cells.Item(1,1) = Amount
 $ws1.cells.Item(1,2) = SalesPerson
 $ws1.cells.Item(2,1) = 1
 $ws1.cells.Item(2,2) = Jack
 $ws1.cells.Item(3,1) = 2
 $ws1.cells.Item(3,2) = Elizabeth
 $ws1.cells.Item(4,1) = 3
 $ws1.cells.Item(4,2) = Renee
 $ws1.cells.Item(5,1) = 4
 $ws1.cells.Item(5,2) = Elizabeth
 $ws1.cells.Item(6,1) = 5
 $ws1.cells.Item(6,2) = Jack
 $ws1.cells.Item(7,1) = 6
 $ws1.cells.Item(7,2) = Renee
 $ws1.cells.Item(8,1) = 7
 $ws1.cells.Item(8,2) = Elizabeth
 $ws1.cells.Item(9,1) = 8
 $ws1.cells.Item(9,2) = Jack
 $ws1.cells.Item(10,1) = 9
 $ws1.cells.Item(10,2) = Renee
 $ws1.cells.Item(11,1) = 10
 $ws1.cells.Item(11,2) = Jack
 $ws1.cells.Item(12,1) = 11
 $ws1.cells.Item(12,2) = Jack
 $ws1.cells.Item(13,1) = 12
 $ws1.cells.Item(13,2) = Elizabeth
 $ws1.cells.Item(14,1) = 13
 $ws1.cells.Item(14,2) = Renee
 $ws1.cells.Item(15,1) = 14
 $ws1.cells.Item(15,2) = Elizabeth
 $ws1.cells.Item(16,1) = 15
 $ws1.cells.Item(16,2) = Jack
 $ws1.cells.Item(17,1) = 16
 $ws1.cells.Item(17,2) = Renee
 $ws1.cells.Item(18,1) = 17
 $ws1.cells.Item(18,2) = Elizabeth
 $ws1.cells.Item(19,1) = 18
 $ws1.cells.Item(19,2) = Jack
 $ws1.cells.Item(20,1) = 19
 $ws1.cells.Item(20,2) = Renee
 $ws1.cells.Item(21,1) = 20
 $ws1.cells.Item(21,2) = Renee
 $used = $ws1.usedRange
 $lastCell = $used.SpecialCells($xlCellTypeLastCell)
 $lastrow = $lastCell.row
 $r = $ws1.Range("A1:B$lastrow")
 $ws1.Range("A1:B$lastrow").Copy()  
 $ws2.Select() | Out-Null
 [void]$ws2.Range("A1").PasteSpecial(-4163)  
 $ws3.Select() | Out-Null
 [void]$ws3.Range("A1").PasteSpecial(-4163)  
 $ws4.Select() | Out-Null
 [void]$ws4.Range("A1").PasteSpecial(-4163)  
 $ws5.Select() | Out-Null
 [void]$ws5.Range("A1").PasteSpecial(-4163)  
 #  
 $ws5.Range("A1").Select()  
 # AutoFilter structure - Field, Criteria, Operator  
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Items #top 10
 $xl.Range("A1").Select() | Out-Null
 $xl.Selection.AutoFilter(1, 2, $xlTop10Items) #top 2 
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Percent #top 10 percent  
 #$xl.Selection.AutoFilter 1, 25, $$xlTop10Percent #top 25 percent  
 #$xl.Selection.AutoFilter 1, 5, $xlBottom10Items #Lowest 5 Items  
 #$xl.Selection.AutoFilter 1, 10, $$xlBottom10Percent #Bottom 10 percent  
 #$xl.Selection.AutoFilter 1, >15 #size greater 15
 #$xl.Selection.AutoFilter 1, >19,XLOr , <2 #Lowest and Highest 
 #$xl.Selection.AutoFilter 2, Jack #Jack items only
 $ws5.cells.Item.EntireColumn.AutoFit  
 $ws2.Select()  
 $ws2.Range("A1").Select()  
 # AutoFilter structure - Field, Criteria, Operator  
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Items #top 10
 #$xl.Selection.AutoFilter 1, 2, $xlTop10Items #top 2
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Percent #top 10 percent  
 #$xl.Selection.AutoFilter 1, 25, $xlTop10Percent #top 25 percent  
 #$xl.Selection.AutoFilter 1, 5, $xlBottom10Items #Lowest 5 Items  
 #$xl.Selection.AutoFilter 1, 10, $xlBottom10Percent #Bottom 10 percent  
 #$xl.Selection.AutoFilter 1, >15 #size greater 15
 #$xl.Selection.AutoFilter 1, >19,XLOr , <2 #Lowest and Highest 
 $xl.Selection.AutoFilter(2, Jack) #Jack items only 
 $ws2.cells.Item.EntireColumn.AutoFit  
 $ws4.Select()  
 $ws4.Range("A1").Select()  
 # AutoFilter structure - Field, Criteria, Operator  
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Items #top 10
 #$xl.Selection.AutoFilter 1, 2, $xlTop10Items #top 2
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Percent #top 10 percent  
 #$xl.Selection.AutoFilter 1, 25, $xlTop10Percent #top 25 percent  
 #$xl.Selection.AutoFilter 1, 5, $xlBottom10Items #Lowest 5 Items  
 #$xl.Selection.AutoFilter 1, 10, $xlBottom10Percent #Bottom 10 percent  
 #$xl.Selection.AutoFilter 1, >15 #size greater 15
 $xl.Selection.AutoFilter(1, >19,$xlOr , <2) #Lowest and Highest  
 #$xl.Selection.AutoFilter 2, Jack #Jack items only
 $ws4.cells.Item.EntireColumn.AutoFit  
 # Top25Percent 
 $ws5.Select()  
 $ws5.Range("A1").Select()  
 # AutoFilter structure - Field, Criteria, Operator  
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Items #top 10
 #$xl.Selection.AutoFilter 1, 2, $xlTop10Items #top 2
 #$xl.Selection.AutoFilter 1, 10, $xlTop10Percent #top 10 percent  
 $xl.Range("A1").Select() | Out-Null
 $xl.Selection.AutoFilter(1,"25",$xlTop10Percent) #top 25 percent  
 #$xl.Selection.AutoFilter 1, 5, $xlBottom10Items #Lowest 5 Items  
 #$xl.Selection.AutoFilter 1, 10, $xlBottom10Percent #Bottom 10 percent  
 #$xl.Selection.AutoFilter 1, >15 #size greater 15
 #$xl.Selection.AutoFilter 1, >19,XLOr , <2 #Lowest and Highest 
 #$xl.Selection.AutoFilter 2, Jack, $xlAnd, Elizabeth #Jack & Elizabeth items only
 $ws5.cells.Item.EntireColumn.AutoFit  
 } # End Function
############# End POSH Script ###############

Get rid of the Excel Com Object, Once and for All

From Technet, November of 2007.

When you run $x.Quit() The Excel com object is left running. This can be a problem.
If you are writing and testing a script, you could wind up with 10-15 or more instance of Excel running in the background. Here is one way of solving that problem.


$x = New-Object -com Excel.Application
$x.Visible = $True
Start-Sleep 5
$x.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($x)
Remove-Variable x

How do I determine the last time a Web Site was visited?


COMMENT: This one looks at a list of IIS servers, checks the Site Logs
to determine the last time someone visited the site.


<# =====================================================================
========================================================================
#>
function Test-Port {
    Param(
        [string] $srv,
        $port=135,
        $timeout=3000,
        [switch]$verbose
    )
    # Test-Port.ps1
    # Does a TCP connection on specified port (135 by default)
    $ErrorActionPreference = "SilentlyContinue"
    # Create TCP Client
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    # Tell TCP Client to connect to machine on Port
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    # Set the wait time
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    # Check to see if the connection is done
    if(!$wait)
    {
        # Close the connection and report timeout
        $tcpclient.Close()
        if($verbose){Write-Host "Connection Timeout"}
        Return $false
    }
    else
    {
        # Close the connection and report the error if there is one
        $error.Clear()
        $tcpclient.EndConnect($iar) | out-Null
        if(!$?){if($verbose){write-host $error[0]};$failed = $true}
        $tcpclient.Close()
    }
    # Return $true if connection Establish else $False
    if($failed){
        return $false
    } else {
        return $true
    }
} # <a href="http://technet.microsoft.com/en-us/library/ff730959.aspx">http://technet.microsoft.com/en-us/library/ff730959.aspx</a>

$ErrorActionPreference = "SilentlyContinue"
$i = $j = 1
$strFile = "E:\scripts\LogFile.txt"
$oLogFile = New-Item -ItemType file $strFile -Force
$srvName = "IIS Servers"
$strDate = Get-Date -format g
Add-Content -Path $strFile "Server `tOS `tSite ID `tSite Name `tLog File Directory `tDate/Time `tHow many days ago `tLogging Enabled `t$strDate"

$servers = gc IISservers.txt
$C = $Servers.count
foreach($server in $servers)
{ Write-Progress -Activity "Servers..." -Status "$i of $C" `
    -percentcomplete ($i / $C*100) -id 1
      If (Test-Connection -computername $server -Quiet -count 1){  
           $a = Test-Port $server
           If ($a) {
               $WMIJob = Get-WmiObject Win32_OperatingSystem -Comp $server -AsJob
            Wait-Job -ID $WMIJob.ID -Timeout 20
            $os = Receive-Job $WMIJob.ID
            if ($os -ne $null) {
            switch ($os.version) {
                "5.1.2600" {$osVer = "XP/IIS5.1"}
                "5.2.3790" {$osVer = "Server 2003/IIS6"}
                "6.0.6001" {$osVer = "Server 2008/IIS7"}
                "6.1.7600" {$osVer = "Server 2008 R2/IIS7.5"}
                    }
                $iis = [adsi]"IIS://$server/w3SVC"
                $sites = $iis.Children | ?{$_.keytype -eq "IIsWebServer"}
                $sc = $sites.count
                $j = 1
        foreach($site in $sites)
        {     
        Write-Progress -Activity "Sites..." -Status "$j of $sc" `
        -percentcomplete ($j / $sc*100) -ParentID 1
         $daysago = $null
        $logdir = $site.LogFileDirectory
        $lfd = $logdir -replace ':\\', '$\'
        $lfd = "\\$server\$lfd\w3svc$($site.name)"
        $Comment = [string]$($site.ServerComment)
        $WebSite = [string]$($Site.Name)
        Switch ($site.LogType) {
                                0 { $result = "Disabled" }
                                1 { $result = "Enabled" }
                                }
        if(Test-Path $lfd)
            {
            $lwt = gci $lfd | sort lastwritetime -Descending | select -first 1 -ExpandProperty lastwritetime         
            If ($lwt) {
            $daysago = (New-TimeSpan -Start $lwt -End ([datetime]::Now)).days }
                Else {
                    $lwt = "Dir is Empty"
                    }
            $WebSite = [string]$($Site.Name)
            $lfd = [string]$lfd
            $Comment = [string]$($site.ServerComment)
            Add-Content -Path $strFile "$server`t$osVer`t$WebSite `t$Comment `t$lfd `t$lwt `t$daysago `t$result"
                    }
                    Else {
                    Add-Content -Path $strFile "$server`t$osVer`t$WebSite `t$Comment `t$lfd `tLogFile Directory not found `t `t$result"
                        }
                    $j++
                    }
                }
            #   GWMI fail    
            Else {
            $b = $error | select Exception
              $E = $b -split (":")
              $x = $E[1]
            $Error.Clear()
            $d = $x.StartsWith(" Access")
            If ($d){
                    Add-Content -Path $strFile "$server `tAccess Denied"
                    }
                Else {
                    Add-Content -Path $strFile "$server `t$x"
                    }
                }    
             }       
            Else {
                    Add-Content -Path $strFile "$server `tRPC Port not listening"
                    $i++
                    }
        }
    Else {Add-Content -Path $strFile "$server `tDoes Not Ping"
            $i++   
            }
    $i++
}

############# End POSH Script ###############

How do I Find all the Non Inherited perms on a list of dirs.


COMMENT: Finds all the Non Inherited perms on a list of dirs.
Change where {$_.IsInherited -ne $true} to where {$_.IsInherited -eq $true} for Inherited perms.
Or you can use ? instead of where ie. ? {$_.IsInherited -eq $true}
==============================================================================================
#>
###### Start Posh Script ########

$result = @()
$paths = gc C:\scripts\ps1\dirs.txt
foreach ($path in $paths) {
$all = dir $path -rec
$dirs = $all | ? {$_.psiscontainer -eq "true"}
$result += $dirs | Get-Acl | %{$_.Access | Add-Member -MemberType NoteProperty Path -Value ($_.Path -replace ".+::") -PassThru} |
select Path,FileSystemRights,AccessControlType,IdentityReference,IsInherited,InheritanceFlags,PropagationFlags |
where {$_.IsInherited -ne $true}
}

$result | Export-Csv mypaths.csv –NoTypeInformation