Pure Capsaicin at Spiceworks!
vor 2 Jahren
This blog is about PowerShell Scripting, therefor it's name is just PowerShell. Ok using german orthografie. I have a Python background and my office duties are databases and reporting related.
$workdir = Split-Path $MyInvocation.MyCommand.Path $logdir = 'C:\temp\log' $server1 = 'my_sql2008' $server2 = 'my_sql2005' $datbase = 'PowerShellTest' $cmd1 = [scriptblock]::Create(". $workdir\script_containing_function_for_background_job.ps1 Some_function -server $server1 -database $datbase $logdir -asJob" ) $cmd1 = [scriptblock]::Create(". $workdir\script_containing_function_for_background_job.ps1 Some_function -server $server2 -database $datbase $logdir -asJob" ) "...Starting the background jobs" $SQLJobStartTime = Get-date $jnr1 = (Start-Job -ScriptBlock $cmd1).Id $jnr2 = (Start-Job -ScriptBlock $cmd2).Id # ***************************************** ' Doing something slow here (it happens to be with an Oracle Server' <# .... #> function Get-WaitTime { param( [DateTime]$start, $dauer ) $rest = $dauer - ((Get-Date) - $start).Totalseconds if ($rest -lt 0) {$rest = 0} $rest } '...Waiting for job on SQL Server 2008' Wait-job -id $jnr1 -timeout ( Get-WaitTime $SQLJobStartTime 960 ) # max 16 min if ((Get-job -id $jnr1).state -eq 'Running') { Stop-job -id $jnr1 } Receive-Job -id $jnr1 $state1 = (Get-job -id $jnr1).State $dauer1 = ((get-date) - $SQLJobStartTime).Totalseconds "$(Get-date -f d) $(Get-date -f t) Release $release($build) Einspielen auf SQl-Server 2008 $state1 $dauer1 secs" '...Waiting for job SQL Server 2005' Wait-job -id $jnr2 -timeout ( Get-WaitTime $SQLJobStartTime 960 ) # max 16 min if ((Get-job -id $jnr2).state -eq 'Running') { Stop-job -id $jnr2 } Receive-Job -id $jnr2 $state2 = (Get-job -id $jnr2).State $dauer2 = ((get-date) - $SQLJobStartTime).Totalseconds "$(Get-date -f d) $(Get-date -f t) Release $release($build) Einspielen auf SQl-Server 2005 $state2 $dauer2 secs"
# adapt the following 4 lines to your environment Add-Type -Path "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Client.dll" $tfsuri = 'your tfs server' $project = "your prpject" $buildDefinition = 'Main' function Get-BuildInfo { param( $buildDefinition, $MaxBuildsPerDefinition = 5, [switch]$DontBreakOnSuccess, [switch]$Starttime ) #Write-Host "Using MaxBuildsPerDefinition $MaxBuildsPerDefinition" $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsuri) $buildServerType = [Microsoft.TeamFoundation.Build.Client.IBuildServer] $buildServer = $tfs.GetService($buildServerType); $buildDetailSpec = $buildServer.CreateBuildDetailSpec($project, $buildDefinition); if ($Starttime) { $buildDetailSpec.QueryOrder = [Microsoft.TeamFoundation.Build.Client.BuildQueryOrder]::StartTimeDescending } else { $buildDetailSpec.QueryOrder = [Microsoft.TeamFoundation.Build.Client.BuildQueryOrder]::FinishTimeDescending } $buildDetailSpec.MaxBuildsPerDefinition = $MaxBuildsPerDefinition try { $buildQueryResult = $buildServer.QueryBuilds($buildDetailSpec) foreach ($build in $buildQueryResult.builds) { # [Microsoft.TeamFoundation.Build.Client.IBuildDetail]$buildDetail = $buildQueryResult.builds[0] $tfsBuildNumber = $build.BuildNumber $shortBuildNumber = $tfsBuildNumber.Substring($tfsBuildNumber.LastIndexOf('_')+1) # #$shortBuildNumber # $buildQueryResult.failures.length # $buildQueryResult.builds.length # $buildQueryResult.builds[0].BuildNumber # #$buildQueryResult.builds[0].Status # #$buildQueryResult.builds[0].BuildDefinitionUri.AbsolutePath # $buildQueryResult.builds[0].StartTime # $buildQueryResult.builds[0].FinishTime $buildAgent = '??' $build.Information | % { $_.nodes | % { try { $buildAgent = $_.children.nodes[2].fields['ReservedAgentName'] } catch { } } } New-Object PSObject -Property @{ Buildnumber = $build.BuildNumber; Status = $build.Status; CompilationStatus = $build.CompilationStatus; Start = $build.StartTime; End = $build.FinishTime; DropLocation = $build.DropLocation; SourceGetVersion = $build.SourceGetVersion; BuildAgent = $buildAgent } if ($build.Status -eq 'Succeeded' -and ! $DontBreakOnSuccess) { break } } } catch [Exception] { throw "TFS nicht erreichbar." } } $a = @(Get-BuildInfo $buildDefinition) $a | % { if ($_.Status -eq 'InProgress') { $end = Get-Date } else { $end = $_.End } "{0,-32} {1,18} {2:ddd} {3} - {4:T} {5,5:0} {6} {7,6} {8}" -f $_.Buildnumber, $_.Status, $_.Start, $_.Start, $End, ($End -$_.Start).Totalminutes, $_.SourceGetVersion, $_.buildAgent, $Droplocation }
ipmo WPK if (! $sqlitedll) { $sqlitedll = [System.Reflection.Assembly]::LoadFrom("C:\Program Files\System.Data.SQLite\bin\System.Data.SQLite.dll") } $ConnectionString = "Data Source=C:\Var\sqlite_ff4\places.sqlite" $conn = new-object System.Data.SQLite.SQLiteConnection $conn.ConnectionString = $ConnectionString $conn.Open() function Invoke-sqlite { param( [string]$sql, [System.Data.SQLite.SQLiteConnection]$connection ) $cmd = new-object System.Data.SQLite.SQLiteCommand($sql,$connection) $ds = New-Object system.Data.DataSet $da = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) $da.fill($ds) | Out-Null return $ds.tables[0] } function Show-Bockmarks ($resource) { New-Grid -Rows 2 -Columns 1 -width 1400 -hight 1000 { New-StackPanel -Orientation horizontal -column 0 -row 0 -Children { New-Label '1. Keyword' New-TextBox -Name tag1 -width 200 New-Label '2. Keyword' New-TextBox -Name tag2 -width 200 New-Label '3. Keyword' New-TextBox -Name tag3 -width 200 New-Button -Name Search "search" -On_Click { $text1 = $window | Get-ChildControl Tag1 $tag1 = $text1.Text $text2 = $window | Get-ChildControl Tag2 $tag2 = $text2.Text $text3 = $window | Get-ChildControl Tag3 $tag3 = $text3.Text if ( $tag2 -ne '') { $clause2 = @" join moz_bookmarks l2 on b.fk = l2.fk and b.id <> l2.id join moz_bookmarks t2 on l2.parent = t2.id and t2.parent = 4 and upper(t2.title) = upper('$tag2') "@ } else { $clause2 = '' } if ( $tag3 -ne '') { $clause3 = @" join moz_bookmarks l3 on b.fk = l3.fk and b.id <> l3.id join moz_bookmarks t3 on l3.parent = t3.id and t3.parent = 4 and upper(t3.title) = upper('$tag3') "@ } else { $clause3 = '' } $ff_sql = @" SELECT b.title, datetime (b.dateAdded / 1000000, 'unixepoch', 'localtime') dateAdded , p.url from moz_bookmarks b join moz_bookmarks l1 on b.fk = l1.fk and b.id <> l1.id join moz_bookmarks t1 on l1.parent = t1.id and t1.parent = 4 and upper(t1.title) = upper('$tag1') join moz_places p on b.fk = p.id $clause2 $clause3 where b.title is not null and b.type = 1 "@ $conn = $resource.conn $window.Title = "$($conn.database) Database Browser" $TableView = $window | Get-ChildControl TableView $TableView.ItemsSource = @(Invoke-sqlite -sql $ff_sql -connection $conn) } New-Button -Name Cancel "Close" -On_Click {$window.Close()} } New-ListView -Column 0 -Row 1 -Name TableView -View { New-GridView -AllowsColumnReorder -Columns { New-GridViewColumn "title" New-GridViewColumn "dateAdded" New-GridViewColumn "url" } } -On_SelectionChanged { start $this.selecteditem.url } #} } -asjob -Resource $resource } Show-Bockmarks -resource @{conn = $conn}
function Eval-Selection { # Bernd Kriszio 2010-11-21 # http://pauerschell.blogspot.com/ # twitter @bernd_k $editor = $psise.CurrentFile.Editor if ($editor.SelectedText) { $inputScript = $editor.SelectedText $editor.InsertText('') $editor.InsertText($inputScript) $result = Invoke-expression $inputScript | out-String $editor.insertText("`r`n") $editor.InsertText($result) } else { $inputScript = $editor.Text $EndLine = $editor.LineCount $EndColumn = $editor.GetLineLength($EndLine) + 1 $editor.SetCaretPosition($EndLine, $EndColumn) $result = Invoke-expression $inputScript | out-String $editor.insertText("`r`n") $editor.InsertText($result) } } $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Eval-Selection", {Eval-Selection} , 'f7')
function Eval-Selection { # Bernd Kriszio 2010-11-21 # http://pauerschell.blogspot.com/ # twitter @bernd_k $editor = $psise.CurrentFile.Editor # if nothing is selected just use the line the caret is in if (!$editor.SelectedText) { $caretLine = $editor.CaretLine $caretLineEnd = $editor.GetLineLength($caretLine) + 1 $editor.Select($caretLine, 1, $caretLine, $caretLineEnd) } # if something is selected use it. if ($editor.SelectedText) { $inputScript = $editor.SelectedText $editor.InsertText('') $editor.InsertText($inputScript) $result = Invoke-expression $inputScript | out-String -width 1000 if ($editor.CaretColumn -ne 1) { $editor.insertText("`r`n") } $editor.InsertText($result) } }