├── .gitattributes ├── .vscode └── settings.json ├── LICENSE.md ├── README.md ├── archives ├── Globals.ps1 ├── LazyTS.psproj ├── LazyTS.psproj.psbuild ├── LazyTS.psprojs ├── MainForm.psf ├── Startup.pss └── images │ ├── 1412236864_20737.ico │ ├── exit.png │ ├── message.png │ ├── running_process.png │ └── terminal-icon.png ├── media ├── Cassia.dll_required.png ├── LazyTS-Highlight.png ├── LazyTS-RDP_Shadow.png ├── LazyTS-Sending_Message-Confirmation.png ├── LazyTS-Sending_Message-Custom.png ├── LazyTS-Sending_Message.png ├── LazyTS-Shadow_Control.png ├── LazyTS-Shadow_Deny.png ├── LazyTS-Stopping_Processes.png ├── LazyTS-Stopping_Processes_Confirmation.png ├── LazyTS.png ├── LazyTS_Closing_sessions.png ├── LazyTS_Closing_sessions_Confirmation.png ├── LazyTS_DisconnectedUser_message.png ├── LazyTS_Disconnecting_sessions.png ├── LazyTS_Query_processes.png ├── LazyTS_Query_sessions.png └── User_message.png └── source ├── Cassia.dll ├── LazyTS.ps1 ├── config.psd1 └── functions ├── Append-RichtextboxStatus.ps1 ├── Clear-DataGridViewSelection.ps1 ├── ConvertTo-DataTable.ps1 ├── Disable-Button.ps1 ├── Disconnect-TSSession.ps1 ├── Enable-Button.ps1 ├── Find-DataGridViewValue.ps1 ├── Get-TSCurrentSession.ps1 ├── Get-TSGlobalServerName.ps1 ├── Get-TSProcess.ps1 ├── Get-TSServers.ps1 ├── Get-TSSession.ps1 ├── Load-DataGridView.ps1 ├── New-MessageBox.ps1 ├── Reset-DataGridViewFormat.ps1 ├── Reset-TextBox.ps1 ├── Send-TSMessage.ps1 ├── Set-DataGridView.ps1 ├── Set-DataGridViewFilter.ps1 ├── Set-TSGlobalServerName.ps1 ├── Set-TextBox.ps1 ├── Stop-TSProcess.ps1 └── Stop-TSSession.ps1 /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.trimTrailingWhitespace": true, 3 | "editor.renderWhitespace": "all", 4 | "editor.tabSize": 4 5 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Francois-Xavier Cat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | LazyTS 2 | ====== 3 | LazyTS is a PowerShell script to manage Sessions and Processes on local or remote machines. It allows you to Query/Disconnect/Stop session(s), Query/Stop process(es) and Send Interactive message to one or more sessions. 4 | 5 | This tool is using the module PSTerminalService which relies on the Cassia .NET Library.I used Sapien PowerShell Studio 2014 to which make life easier if you want to start building WinForms tools and add PowerShell code. 6 | 7 | ![alt text](http://2.bp.blogspot.com/-nx_UZoTQ3a8/VC4cumIZM3I/AAAAAAABoeM/xF4Z9M-dNgc/s1600/LazyTS.png "LazyTS") 8 | 9 | ## Requirements 10 | 11 | * PowerShell 3.0 12 | * Cassia Library (included in the download) 13 | 14 | LazyTS is relying on the Cassia .NET Library (A DLL file). This file need to be present in the same directory as the script (Either when you use the PS1 or EXE file) 15 | 16 | Of course you also need the appropriate permission on the remote machine to be able to manage it. 17 | If the Cassia.dll is not present, the script won't start: 18 | 19 | ![Alt text](media/Cassia.dll_required.png?raw=true "Cassia") 20 | 21 | ## Installation 22 | 23 | * Download the last version: https://github.com/lazywinadmin/LazyTS/releases 24 | * Unblock the zip file 25 | * Extract all the files 26 | * Launch LazyTS.ps1 27 | 28 | 29 | ## Usage 30 | 31 | * Launch a PowerShell console with the account that will perform the action 32 | * Execute the powershell script LazyTS.ps1 33 | 34 | ![Alt text](media/LazyTS.png "LazyTS") 35 | 36 | 37 | ## Main features 38 | 39 | ### Querying session on local or remote machines 40 | 41 | After entering the ComputerName, click on the button Sessions 42 | 43 | ![Alt text](media/LazyTS_Query_sessions.png?raw=true "Querying session on local or remote machines") 44 | 45 | 46 | ### Disconnecting Session(s) 47 | 48 | Select one session or multiple sessions and right click, click on Disconnect Session 49 | This will only disconnect the user, its session State will become "disconnected", but will remain opened on the host. 50 | 51 | ![Alt text](media/LazyTS_Disconnecting_sessions.png?raw=true "Disconnecting Session(s)") 52 | 53 | When getting disconnected, the user will get the following window 54 | 55 | ![Alt text](media/LazyTS_DisconnectedUser_message.png?raw=true "Your Remote Desktop Services session had ended") 56 | Your Remote Desktop Services session had ended. 57 | Your network administrator might have ended the connection. Try connection again, or contact technical support for assistance 58 | 59 | ### Stopping a Session(s) 60 | 61 | Select one session or multiple sessions and right click, click on Stop Session 62 | This will close the session of the user without warning. Any current work in progress will be lost. 63 | 64 | ![Alt text](media/LazyTS_Closing_sessions.png?raw=true "Stopping a Session(s)") 65 | 66 | Here is the confirmation window after you select "Stop Session" 67 | ![Alt text](media/LazyTS_Closing_sessions_Confirmation.png?raw=true "confirmation window for Stopping a Session") 68 | 69 | ### Querying Processes 70 | 71 | After entering the ComputerName, click on the button Process. 72 | This will list all the running processes (for all sessions) 73 | ![Alt text](media/LazyTS_Query_processes.png?raw=true "Querying Processes") 74 | 75 | ### Stopping Process(es) 76 | 77 | Select one process or multiple processes and right click, click on Stop Process 78 | This will Stop the process(es) selected. 79 | 80 | ![Alt text](media/LazyTS-Stopping_Processes.png?raw=true "Stopping Process(es)") 81 | 82 | ![Alt text](media/LazyTS-Stopping_Processes_Confirmation.png?raw=true "Stopping Process(es)/Confirmation") 83 | 84 | ### Sending Message(s) 85 | 86 | After entering the ComputerName, click on the button Message. 87 | Note that you must select one session or one process in order to use this option (the script need to know which session(s) it should send the message to. 88 | 89 | This will Send a message to the selected session. If multiple sessions are selected only one message will be sent to each session. 90 | 91 | ![Alt text](media/LazyTS-Sending_Message.png?raw=true "Sending Message(s)") 92 | 93 | 94 | Confirmation windows: 95 | 96 | ![Alt text](media/LazyTS-Sending_Message-Confirmation.png?raw=true "Sending Message(s)/confirmation") 97 | 98 | Default message: "IMPORTANT: The Server is going down for maintenance in 10 minutes. Please save your work and logoff." 99 | 100 | Message to send 101 | 102 | ![Alt text](media/LazyTS-Sending_Message-Custom.png?raw=true "Sending Message(s)/Message to send") 103 | 104 | 105 | #### Example of message received by each session selected 106 | 107 | ![Alt text](media/User_message.png?raw=true "Example") 108 | 109 | ### Remote Desktop: Shadow (since v1.1) 110 | 111 | When right clicking on a row you'll have the possibility to open a Shadow RDP on a selected Session ID. After you request to View or Control the session, the user will get this message 112 | 113 | ![Alt text](media/LazyTS-Shadow_Control.png?raw=true "Remote Desktop: Shadow") 114 | "\ is requesting to control your session remotely. Do you accept the request" 115 | Message showed to the user when trying to control his session id. 116 | 117 | If the user deny the request, you'll get the following message 118 | 119 | ![Alt text](media/LazyTS-Shadow_Deny.png?raw=true "Deny") 120 | "The operator or administrator has refused the request" 121 | -------------------------------------------------------------------------------- /archives/Globals.ps1: -------------------------------------------------------------------------------- 1 | #requires -version 3 2 | Write-Verbose -Message "[$ScriptName] Globals.ps1 processing..." 3 | #-------------------------------------------- 4 | # Declare Global Variables and Functions here 5 | #-------------------------------------------- 6 | 7 | #Location of the script 8 | function Get-ScriptDirectory 9 | { 10 | if($null -ne $hostinvocation) 11 | { 12 | Split-Path -path $hostinvocation.MyCommand.path 13 | } 14 | else 15 | { 16 | Split-Path -path $script:MyInvocation.MyCommand.Path 17 | } 18 | } 19 | 20 | [System.string]$ScriptDirectory = Get-ScriptDirectory 21 | 22 | 23 | # Load Configuration 24 | $configurationpath = Join-Path -Path $ScriptDirectory -ChildPath 'config.psd1' 25 | if(-not(Test-Path $configurationpath)){Write-Warning -message "can't retrieve the configuration file 'config.psd1'"} 26 | $Configuration= Import-LocalizedData -BaseDirectory (Get-ScriptDirectory) -FileName "config.psd1" 27 | if (-not($Configuration.settings.computer)){ $Configuration.settings.computer = $env:computername} 28 | 29 | 30 | # PSTerminalServices Module Requirements 31 | # Get the path of the DLL file: Cassia.dll (.net library) 32 | $CassiaPath = Join-Path -Path (Get-ScriptDirectory) -ChildPath "Cassia.dll" 33 | # Load the DLL 34 | if (-not(Test-Path $CassiaPath)){Write-Warning -Message "The file Cassia.dll is missing. The Script can't continue without this file";exit} 35 | [Reflection.Assembly]::LoadFile($CassiaPath) | Out-Null 36 | 37 | # Import all the helper functions 38 | $functionpath = Join-Path -Path $ScriptDirectory -child "functions" 39 | $script:Server = 'localhost' 40 | foreach ($file in (Get-ChildItem -path $functionpath)) 41 | { 42 | . $file.fullname 43 | } 44 | 45 | 46 | 47 | # 48 | ## From WinFormPS 49 | #function Append-RichtextboxStatus{ 50 | # PARAM( 51 | # [Parameter(Mandatory=$true)] 52 | # [string]$Message, 53 | # [string]$MessageColor = "DarkGreen", 54 | # [string]$DateTimeColor="Black", 55 | # [string]$Source, 56 | # [string]$SourceColor="Gray", 57 | # [string]$ComputerName, 58 | # [String]$ComputerNameColor= "Blue") 59 | # 60 | # $SortableTime = get-date -Format "yyyy-MM-dd HH:mm:ss" 61 | # $richtextboxStatus.SelectionColor = $DateTimeColor 62 | # $richtextboxStatus.AppendText("[$SortableTime] ") 63 | # 64 | # IF ($PSBoundParameters['ComputerName']){ 65 | # $richtextboxStatus.SelectionColor = $ComputerNameColor 66 | # $richtextboxStatus.AppendText(("$ComputerName ").ToUpper()) 67 | # } 68 | # 69 | # IF ($PSBoundParameters['Source']){ 70 | # $richtextboxStatus.SelectionColor = $SourceColor 71 | # $richtextboxStatus.AppendText("$Source ") 72 | # } 73 | # 74 | # $richtextboxStatus.SelectionColor = $MessageColor 75 | # $richtextboxStatus.AppendText("$Message`r") 76 | # $richtextboxStatus.Refresh() 77 | # $richtextboxStatus.ScrollToCaret() 78 | # 79 | # Write-Verbose -Message "$SortableTime $Message" 80 | #} 81 | # 82 | #function Set-DataGridView 83 | #{ 84 | # <# 85 | # .SYNOPSIS 86 | # This function helps you edit the datagridview control 87 | # 88 | # .DESCRIPTION 89 | # This function helps you edit the datagridview control 90 | # 91 | # .EXAMPLE 92 | # Set-DataGridView -DataGridView $datagridview1 -ProperFormat -FontFamily $listboxFontFamily.Text -FontSize $listboxFontSize.Text 93 | # 94 | # .EXAMPLE 95 | # Set-DataGridView -DataGridView $datagridview1 -AlternativeRowColor -BackColor 'AliceBlue' -ForeColor 'Black' 96 | # 97 | # .EXAMPLE 98 | # Set-DataGridViewRowHeader -DataGridView $datagridview1 -HideRowHeader 99 | # 100 | # .EXAMPLE 101 | # Set-DataGridViewRowHeader -DataGridView $datagridview1 -ShowRowHeader 102 | # 103 | # .NOTES 104 | # Author: Francois-Xavier Cat 105 | # Twitter:@LazyWinAdm 106 | # WWW: lazywinadmin.com 107 | # #> 108 | # 109 | # [CmdletBinding()] 110 | # PARAM ( 111 | # [ValidateNotNull()] 112 | # [Parameter(Mandatory = $true)] 113 | # [System.Windows.Forms.DataGridView]$DataGridView, 114 | # 115 | # [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 116 | # [Switch]$AlternativeRowColor, 117 | # 118 | # [Parameter(ParameterSetName = "DefaultRowColor")] 119 | # [Switch]$DefaultRowColor, 120 | # 121 | # [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 122 | # [Parameter(ParameterSetName = "DefaultRowColor")] 123 | # [System.Drawing.Color]$ForeColor, 124 | # 125 | # [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 126 | # [Parameter(ParameterSetName = "DefaultRowColor")] 127 | # [System.Drawing.Color]$BackColor, 128 | # 129 | # [Parameter(Mandatory = $true, ParameterSetName = "Proper")] 130 | # [Switch]$ProperFormat, 131 | # 132 | # [Parameter(ParameterSetName = "Proper")] 133 | # [String]$FontFamily = "Consolas", 134 | # 135 | # [Parameter(ParameterSetName = "Proper")] 136 | # [Int]$FontSize = 10, 137 | # 138 | # [Parameter(ParameterSetName = "HideRowHeader")] 139 | # [Switch]$HideRowHeader, 140 | # [Parameter(ParameterSetName = "ShowRowHeader")] 141 | # [Switch]$ShowRowHeader 142 | # ) 143 | # PROCESS 144 | # { 145 | # if ($psboundparameters['AlternativeRowColor']) 146 | # { 147 | # $DataGridView.AlternatingRowsDefaultCellStyle.ForeColor = $ForeColor 148 | # $DataGridView.AlternatingRowsDefaultCellStyle.BackColor = $BackColor 149 | # } 150 | # 151 | # if ($psboundparameters['DefaultRowColor']) 152 | # { 153 | # $DataGridView.RowsDefaultCellStyle.ForeColor = $ForeColor 154 | # $DataGridView.RowsDefaultCellStyle.BackColor = $BackColor 155 | # } 156 | # 157 | # 158 | # if ($psboundparameters['ProperFormat']) 159 | # { 160 | # #$Font = New-Object -TypeName System.Drawing.Font -ArgumentList "Segoi UI", 10 161 | # $Font = New-Object -TypeName System.Drawing.Font -ArgumentList $FontFamily, $FontSize 162 | # 163 | # #[System.Drawing.FontStyle]::Bold 164 | # 165 | # $DataGridView.ColumnHeadersBorderStyle = 'Raised' 166 | # $DataGridView.BorderStyle = 'Fixed3D' 167 | # $DataGridView.SelectionMode = 'FullRowSelect' 168 | # $DataGridView.AllowUserToResizeRows = $false 169 | # $datagridview.DefaultCellStyle.font = $Font 170 | # } 171 | # 172 | # if ($psboundparameters['HideRowHeader']) 173 | # { 174 | # $DataGridView.RowHeadersVisible = $false 175 | # } 176 | # if ($psboundparameters['ShowRowHeader']) 177 | # { 178 | # $DataGridView.RowHeadersVisible = $true 179 | # } 180 | # } 181 | # 182 | #}#Set-DataGridView 183 | # 184 | #function Reset-DataGridViewFormat 185 | #{ 186 | #<# 187 | # .SYNOPSIS 188 | # The Reset-DataGridViewFormat function will reset the format of a datagridview control 189 | # 190 | # .DESCRIPTION 191 | # The Reset-DataGridViewFormat function will reset the format of a datagridview control 192 | # 193 | # .PARAMETER DataGridView 194 | # Specifies the DataGridView Control. 195 | # 196 | # .EXAMPLE 197 | # PS C:\> Reset-DataGridViewFormat -DataGridView $DataGridViewObj 198 | # 199 | # .NOTES 200 | # Author: Francois-Xavier Cat 201 | # Twitter:@LazyWinAdm 202 | # WWW: lazywinadmin.com 203 | ##> 204 | # [CmdletBinding()] 205 | # PARAM ( 206 | # [Parameter(Mandatory = $true)] 207 | # [System.Windows.Forms.DataGridView]$DataGridView) 208 | # PROCESS 209 | # { 210 | # $DataSource = $DataGridView.DataSource 211 | # $DataGridView.DataSource = $null 212 | # $DataGridView.DataSource = $DataSource 213 | # 214 | # #$DataGridView.RowsDefaultCellStyle.BackColor = 'White' 215 | # #$DataGridView.RowsDefaultCellStyle.ForeColor = 'Black' 216 | # $DataGridView.RowsDefaultCellStyle = $null 217 | # $DataGridView.AlternatingRowsDefaultCellStyle = $null 218 | # } 219 | #}#Reset-DataGridViewFormat 220 | # 221 | #function Find-DataGridViewValue 222 | #{ 223 | #<# 224 | # .SYNOPSIS 225 | # The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. 226 | # 227 | # .DESCRIPTION 228 | # The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. 229 | # 230 | # .PARAMETER DataGridView 231 | # Specifies the DataGridView Control to use 232 | # 233 | # .PARAMETER RowBackColor 234 | # Specifies the back color of the row to use 235 | # 236 | # .PARAMETER RowForeColor 237 | # Specifies the fore color of the row to use 238 | # 239 | # .PARAMETER SelectCell 240 | # Specifies to select only the cell when the value is found 241 | # 242 | # .PARAMETER SelectRow 243 | # Specifies to select the entire row when the value is found 244 | # 245 | # .PARAMETER Value 246 | # Specifies the value to search 247 | # 248 | # .EXAMPLE 249 | # PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text 250 | # 251 | # This will find the value and select the cell(s) 252 | # 253 | # .EXAMPLE 254 | # PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -RowForeColor 'Red' -RowBackColor 'Black' 255 | # 256 | # This will find the value and color the fore and back of the row 257 | # .EXAMPLE 258 | # PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -SelectRow 259 | # 260 | # This will find the value and select the entire row 261 | # 262 | # .NOTES 263 | # Francois-Xavier Cat 264 | # @lazywinadm 265 | # www.lazywinadmin.com 266 | ##> 267 | # [CmdletBinding(DefaultParameterSetName = "Cell")] 268 | # PARAM ( 269 | # [Parameter(Mandatory = $true)] 270 | # [System.Windows.Forms.DataGridView]$DataGridView, 271 | # 272 | # [ValidateNotNull()] 273 | # [Parameter(Mandatory = $true)] 274 | # [String]$Value, 275 | # [Parameter(ParameterSetName = "Cell")] 276 | # [Switch]$SelectCell, 277 | # 278 | # [Parameter(ParameterSetName = "Row")] 279 | # [Switch]$SelectRow, 280 | # 281 | # #[Parameter(ParameterSetName = "Column")] 282 | # #[Switch]$SelectColumn, 283 | # [Parameter(ParameterSetName = "RowColor")] 284 | # [system.Drawing.Color]$RowForeColor, 285 | # [Parameter(ParameterSetName = "RowColor")] 286 | # [system.Drawing.Color]$RowBackColor 287 | # ) 288 | # 289 | # PROCESS 290 | # { 291 | # $DataGridView.ClearSelection() 292 | # 293 | # FOR ([int]$i = 0; $i -lt $DataGridView.RowCount; $i++) 294 | # { 295 | # FOR ([int] $j = 0; $j -lt $DataGridView.ColumnCount; $j++) 296 | # { 297 | # $CurrentCell = $dataGridView.Rows[$i].Cells[$j] 298 | # 299 | # #if ((-not $CurrentCell.Value.Equals([DBNull]::Value)) -and ($CurrentCell.Value.ToString() -like "*$Value*")) 300 | # if ($CurrentCell.Value.ToString() -match $Value) 301 | # { 302 | # 303 | # # Row Selection 304 | # IF ($PSBoundParameters['SelectRow']) 305 | # { 306 | # $dataGridView.Rows[$i].Selected = $true 307 | # } 308 | # 309 | # <# 310 | # # Column Selection 311 | # IF ($PSBoundParameters['SelectColumn']) 312 | # { 313 | # #$DataGridView.Columns[$($CurrentCell.ColumnIndex)].Selected = $true 314 | # #$DataGridView.Columns[$j].Selected = $true 315 | # #$CurrentCell.DataGridView.Columns[$j].Selected = $true 316 | # } 317 | # #> 318 | # 319 | # # Row Fore Color 320 | # IF ($PSBoundParameters['RowForeColor']) 321 | # { 322 | # $dataGridView.Rows[$i].DefaultCellStyle.ForeColor = $RowForeColor 323 | # } 324 | # # Row Back Color 325 | # IF ($PSBoundParameters['RowBackColor']) 326 | # { 327 | # $dataGridView.Rows[$i].DefaultCellStyle.BackColor = $RowBackColor 328 | # } 329 | # 330 | # # Cell Selection 331 | # ELSEIF (-not ($PSBoundParameters['SelectRow']) -and -not ($PSBoundParameters['SelectColumn'])) 332 | # { 333 | # $CurrentCell.Selected = $true 334 | # } 335 | # }#IF not empty and contains value 336 | # }#For Each column 337 | # }#For Each Row 338 | # }#PROCESS 339 | #}#Find-DataGridViewValue 340 | # 341 | #function Set-DataGridViewFilter 342 | #{ 343 | #<# 344 | # .SYNOPSIS 345 | # The function Set-DataGridViewFilter helps to only show specific entries with a specific value 346 | # 347 | # .DESCRIPTION 348 | # The function Set-DataGridViewFilter helps to only show specific entries with a specific value. 349 | # The data needs to be in a DataTable Object. You can use ConvertTo-DataTable to convert your 350 | # PowerShell object into a DataTable object. 351 | # 352 | # .PARAMETER AllColumns 353 | # Specifies to search all the column 354 | # 355 | # .PARAMETER ColumnName 356 | # Specifies to search in a specific column name 357 | # 358 | # .PARAMETER DataGridView 359 | # Specifies the DataGridView control where the data will be filtered 360 | # 361 | # .PARAMETER DataTable 362 | # Specifies the DataTable object that is most likely the original source of the DataGridView 363 | # 364 | # .PARAMETER Filter 365 | # Specifies the string to search 366 | # 367 | # .EXAMPLE 368 | # PS C:\> Set-DataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -AllColumns -Filter $textbox1.Text 369 | # 370 | # .EXAMPLE 371 | # PS C:\> Set-DataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -ColumnName "Name" -Filter $textbox1.Text 372 | # 373 | # .NOTES 374 | # Author: Francois-Xavier Cat 375 | # Twitter:@LazyWinAdm 376 | # WWW: lazywinadmin.com 377 | ##> 378 | # PARAM ( 379 | # [Parameter(Mandatory = $true)] 380 | # [System.Windows.Forms.DataGridView]$DataGridView, 381 | # [Parameter(Mandatory = $true)] 382 | # [System.Data.DataTable]$DataTable, 383 | # [Parameter(Mandatory = $true)] 384 | # [String]$Filter, 385 | # 386 | # [Parameter(Mandatory = $true, ParameterSetName = "OneColumn")] 387 | # [String]$ColumnName, 388 | # [Parameter(Mandatory = $true, ParameterSetName = "AllColumns")] 389 | # [Switch]$AllColumns 390 | # ) 391 | # PROCESS 392 | # { 393 | # $Filter = $Filter.ToString() 394 | # 395 | # IF ($PSBoundParameters['AllColumns']) 396 | # { 397 | # FOREACH ($Column in $DataTable.Columns) 398 | # { 399 | # #$RowFilter += "Convert("+$($Column.ColumnName)+",'system.string') Like '%"{1}%' OR " -f $Column.ColumnName, $Filter 400 | # $RowFilter += "Convert($($Column.ColumnName),'system.string') Like '%$Filter%' OR " 401 | # } 402 | # 403 | # # Remove the last 'OR' 404 | # $RowFilter = $RowFilter -replace " OR $", '' 405 | # 406 | # #Append-RichtextboxStatus -Message $RowFilter 407 | # } 408 | # IF ($PSBoundParameters['ColumnName']) 409 | # { 410 | # $RowFilter = "$ColumnName LIKE '%$Filter%'" 411 | # } 412 | # 413 | # $DataTable.defaultview.rowfilter = $RowFilter 414 | # Load-DataGridView -DataGridView $DataGridView -Item $DataTable 415 | # } 416 | # END { Remove-Variable -Name $RowFilter -ErrorAction 'SilentlyContinue' | Out-Null } 417 | #}#Set-DataGridViewFilter 418 | # 419 | #function ConvertTo-DataTable 420 | #{ 421 | # <# 422 | # .SYNOPSIS 423 | # Converts objects into a DataTable. 424 | # 425 | # .DESCRIPTION 426 | # Converts objects into a DataTable, which are used for DataBinding. 427 | # 428 | # .PARAMETER InputObject 429 | # The input to convert into a DataTable. 430 | # 431 | # .PARAMETER Table 432 | # The DataTable you wish to load the input into. 433 | # 434 | # .PARAMETER RetainColumns 435 | # This switch tells the function to keep the DataTable's existing columns. 436 | # 437 | # .PARAMETER FilterWMIProperties 438 | # This switch removes WMI properties that start with an underline. 439 | # 440 | # .EXAMPLE 441 | # $DataTable = ConvertTo-DataTable -InputObject (Get-Process) 442 | # 443 | # .NOTES 444 | # SAPIEN Technologies, Inc. 445 | # http://www.sapien.com/ 446 | # 447 | # VERSION HISTORY 448 | # 1.0 ????/??/?? From Sapien.com Version 449 | # 2.0 2014/12/03 Francois-Xavier Cat - In the rows workk, I added a 450 | # small piece of code to handle the $null value with [DBNull]::Value 451 | # 452 | # #> 453 | # [CmdletBinding()] 454 | # [OutputType([System.Data.DataTable])] 455 | # param ( 456 | # [ValidateNotNull()] 457 | # $InputObject, 458 | # [ValidateNotNull()] 459 | # [System.Data.DataTable]$Table, 460 | # [switch]$RetainColumns, 461 | # [switch]$FilterWMIProperties 462 | # ) 463 | # 464 | # if ($Table -eq $null) 465 | # { 466 | # $Table = New-Object System.Data.DataTable 467 | # } 468 | # 469 | # if ($InputObject -is [System.Data.DataTable]) 470 | # { 471 | # $Table = $InputObject 472 | # } 473 | # else 474 | # { 475 | # if (-not $RetainColumns -or $Table.Columns.Count -eq 0) 476 | # { 477 | # #Clear out the Table Contents 478 | # $Table.Clear() 479 | # 480 | # if ($InputObject -eq $null) { return } #Empty Data 481 | # 482 | # $object = $null 483 | # 484 | # #find the first non null value 485 | # foreach ($item in $InputObject) 486 | # { 487 | # if ($item -ne $null) 488 | # { 489 | # $object = $item 490 | # break 491 | # } 492 | # } 493 | # 494 | # if ($object -eq $null) { return } #All null then empty 495 | # 496 | # #COLUMN 497 | # #Get all the properties in order to create the columns 498 | # foreach ($prop in $object.PSObject.Get_Properties()) 499 | # { 500 | # if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__'))#filter out WMI properties 501 | # { 502 | # #Get the type from the Definition string 503 | # $type = $null 504 | # 505 | # if ($prop.Value -ne $null) 506 | # { 507 | # try { $type = $prop.Value.GetType() } 508 | # catch { Write-Verbose -Message "Can't find type of $prop" } 509 | # } 510 | # 511 | # if ($type -ne $null) # -and [System.Type]::GetTypeCode($type) -ne 'Object') 512 | # { 513 | # Write-Verbose -Message "Creating Column: $($Prop.name) (Type: $type)" 514 | # [void]$table.Columns.Add($prop.Name, $type) 515 | # } 516 | # else #Type info not found 517 | # { 518 | # #if ($prop.name -eq "" -or $prop.name -eq $null) { [void]$table.Columns.Add([DBNull]::Value) } 519 | # [void]$table.Columns.Add($prop.Name) 520 | # } 521 | # } 522 | # } 523 | # 524 | # if ($object -is [System.Data.DataRow]) 525 | # { 526 | # foreach ($item in $InputObject) 527 | # { 528 | # $Table.Rows.Add($item) 529 | # } 530 | # return @(, $Table) 531 | # } 532 | # } 533 | # else 534 | # { 535 | # $Table.Rows.Clear() 536 | # } 537 | # 538 | # #Rows Work 539 | # foreach ($item in $InputObject) 540 | # { 541 | # # Create a new row object 542 | # $row = $table.NewRow() 543 | # 544 | # if ($item) 545 | # { 546 | # foreach ($prop in $item.PSObject.Get_Properties()) 547 | # { 548 | # #Find the appropriate column to put the value 549 | # if ($table.Columns.Contains($prop.Name)) 550 | # { 551 | # if ($prop.value -eq $null) { $prop.value = [DBNull]::Value } 552 | # $row.Item($prop.Name) = $prop.Value 553 | # } 554 | # } 555 | # } 556 | # [void]$table.Rows.Add($row) 557 | # } 558 | # } 559 | # 560 | # return @(, $Table) 561 | #} 562 | # 563 | #function Load-DataGridView 564 | #{ 565 | # <# 566 | # .SYNOPSIS 567 | # This functions helps you load items into a DataGridView. 568 | # 569 | # .DESCRIPTION 570 | # Use this function to dynamically load items into the DataGridView control. 571 | # 572 | # .PARAMETER DataGridView 573 | # The ComboBox control you want to add items to. 574 | # 575 | # .PARAMETER Item 576 | # The object or objects you wish to load into the ComboBox's items collection. 577 | # 578 | # .PARAMETER DataMember 579 | # Sets the name of the list or table in the data source for which the DataGridView is displaying data. 580 | # #> 581 | # 582 | # [CmdletBinding()] 583 | # Param ( 584 | # [ValidateNotNull()] 585 | # [Parameter(Mandatory=$true)] 586 | # [System.Windows.Forms.DataGridView]$DataGridView, 587 | # [ValidateNotNull()] 588 | # [Parameter(Mandatory=$true)] 589 | # $Item, 590 | # [Parameter(Mandatory=$false)] 591 | # [string]$DataMember 592 | # ) 593 | # $DataGridView.SuspendLayout() 594 | # $DataGridView.DataMember = $DataMember 595 | # 596 | # if ($Item -is [System.ComponentModel.IListSource]` 597 | # -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] ) 598 | # { 599 | # $DataGridView.DataSource = $Item 600 | # } 601 | # else 602 | # { 603 | # $array = New-Object System.Collections.ArrayList 604 | # 605 | # if ($Item -is [System.Collections.IList]) 606 | # { 607 | # $array.AddRange($Item) 608 | # } 609 | # else 610 | # { 611 | # $array.Add($Item) 612 | # } 613 | # $DataGridView.DataSource = $array 614 | # } 615 | # 616 | # $DataGridView.ResumeLayout() 617 | #} 618 | # 619 | #function Set-TextBox 620 | #{ 621 | # [CmdletBinding()] 622 | # PARAM ( 623 | # [System.Windows.Forms.TextBox]$TextBox, 624 | # [System.Drawing.Color]$BackColor 625 | # ) 626 | # BEGIN { } 627 | # PROCESS 628 | # { 629 | # TRY 630 | # { 631 | # $TextBox.BackColor = $BackColor 632 | # } 633 | # CATCH { } 634 | # } 635 | #} 636 | # 637 | #function Disable-Button 638 | #{ 639 | #<# 640 | #.SYNOPSIS 641 | # This function will disable a button control 642 | #.EXAMPLE 643 | # Disable-Button -Button $Button 644 | #.NOTES 645 | # Francois-Xavier Cat 646 | # @lazywinadm 647 | # www.lazywinadmin.com 648 | ##> 649 | # [CmdletBinding()] 650 | # PARAM ( 651 | # [ValidateNotNull()] 652 | # [Parameter(Mandatory = $true)] 653 | # [System.Windows.Forms.Button[]]$Button 654 | # ) 655 | # BEGIN 656 | # { 657 | # Add-Type -AssemblyName System.Windows.Forms 658 | # } 659 | # PROCESS 660 | # { 661 | # foreach ($ButtonObject in $Button) 662 | # { 663 | # $ButtonObject.Enabled = $false 664 | # } 665 | # 666 | # } 667 | #}#Disable-Button 668 | # 669 | #function Reset-TextBox 670 | #{ 671 | # [CmdletBinding()] 672 | # PARAM ( 673 | # [System.Windows.Forms.TextBox]$TextBox, 674 | # [System.Drawing.Color]$BackColor = "White", 675 | # [System.Drawing.Color]$ForeColor = "Black" 676 | # ) 677 | # BEGIN { } 678 | # PROCESS 679 | # { 680 | # TRY 681 | # { 682 | # $TextBox.Text = "" 683 | # $TextBox.BackColor = $BackColor 684 | # $TextBox.ForeColor = $ForeColor 685 | # } 686 | # CATCH { } 687 | # } 688 | #} 689 | # 690 | #function Enable-Button 691 | #{ 692 | #<# 693 | #.SYNOPSIS 694 | # This function will enable a button control 695 | #.EXAMPLE 696 | # Enable-Button -Button $Button 697 | #.NOTES 698 | # Francois-Xavier Cat 699 | # @lazywinadm 700 | # www.lazywinadmin.com 701 | ##> 702 | # [CmdletBinding()] 703 | # PARAM ( 704 | # [ValidateNotNull()] 705 | # [Parameter(Mandatory = $true)] 706 | # [System.Windows.Forms.Button[]]$Button 707 | # ) 708 | # BEGIN 709 | # { 710 | # Add-Type -AssemblyName System.Windows.Forms 711 | # } 712 | # PROCESS 713 | # { 714 | # foreach ($ButtonObject in $Button) 715 | # { 716 | # $ButtonObject.Enabled = $true 717 | # } 718 | # } 719 | #}#Enable-Button 720 | # 721 | #function Clear-DataGridViewSelection 722 | #{ 723 | # PARAM ( 724 | # [Parameter(Mandatory = $true)] 725 | # [System.Windows.Forms.DataGridView]$DataGridView 726 | # ) 727 | # $DataGridView.ClearSelection() 728 | #} 729 | # 730 | #function New-MessageBox 731 | #{ 732 | #<# 733 | # .SYNOPSIS 734 | # The New-MessageBox functio will show a message box to the user 735 | # 736 | # .DESCRIPTION 737 | # The New-MessageBox functio will show a message box to the user 738 | # 739 | # .PARAMETER Message 740 | # Specifies the message to show 741 | # 742 | # .PARAMETER Title 743 | # Specifies the title of the message box 744 | # 745 | # .PARAMETER Buttons 746 | # Specifies which button to add. Just press tab to see the choices 747 | # 748 | # .PARAMETER Icon 749 | # Specifies the icon to show. Just press tab to see the choices 750 | # 751 | # .EXAMPLE 752 | # PS C:\> New-MessageBox -Message "Hello World" -Title "First Message" -Buttons "RetryCancel" -Icon "Asterix" 753 | # 754 | # .NOTES 755 | # Author: Francois-Xavier Cat 756 | # Twitter:@LazyWinAdm 757 | # WWW: lazywinadmin.com 758 | ##> 759 | # [CmdletBinding()] 760 | # PARAM ( 761 | # 762 | # [String]$Message, 763 | # [String]$Title, 764 | # [System.Windows.Forms.MessageBoxButtons]$Buttons = "OK", 765 | # [System.Windows.Forms.MessageBoxIcon]$Icon = "None" 766 | # ) 767 | # BEGIN 768 | # { 769 | # Add-Type -AssemblyName System.Windows.Forms 770 | # } 771 | # PROCESS 772 | # { 773 | # [System.Windows.Forms.MessageBox]::Show($Message, $Title, $Buttons, $Icon) 774 | # } 775 | #}#New-MessageBox 776 | # 777 | ## PSTerminalServices Module by Shay Levy 778 | #$script:Server='localhost' 779 | #function Get-TSSession 780 | #{ 781 | # <# 782 | # .SYNOPSIS 783 | # Lists the sessions on a given terminal server. 784 | # 785 | # .DESCRIPTION 786 | # Use Get-TSSession to get a list of sessions from a local or remote computers. 787 | # Note that Get-TSSession is using Aliased properties to display the output on the console (IPAddress and State), these attributes 788 | # are not the same as the original attributes (ClientIPAddress and ConnectionState). 789 | # This is important when you want to use the -Filter parameter which requires the latter. 790 | # To see all aliassed properties and their corresponding properties (Definition column), pipe the result to Get-Member: 791 | # 792 | # PS > Get-TSSession | Get-Member -MemberType AliasProperty 793 | # 794 | # TypeName: Cassia.Impl.TerminalServicesSession 795 | # 796 | # Name MemberType Definition 797 | # ---- ---------- ---------- 798 | # (...) 799 | # IPAddress AliasProperty IPAddress = ClientIPAddress 800 | # State AliasProperty State = ConnectionState 801 | # 802 | # 803 | # .PARAMETER ComputerName 804 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 805 | # 806 | # .PARAMETER Id 807 | # Specifies the session Id number. 808 | # 809 | # .PARAMETER InputObject 810 | # Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 811 | # 812 | # .PARAMETER Filter 813 | # Specifies a filter based on the session properties. The syntax of the filter, including the use of 814 | # wildcards and depends on the properties of the session. Internally, The Filter parameter uses client side 815 | # filtering using the Where-Object cmdlet, objects are filtered after they are retrieved. 816 | # 817 | # .PARAMETER State 818 | # The connection state of the session. Use this parameter to get sessions of a specific state. Valid values are: 819 | # 820 | # Value Description 821 | # ----- ----------- 822 | # Active A user is logged on to the session. 823 | # ConnectQuery The session is in the process of connecting to a client. 824 | # Connected A client is connected to the session). 825 | # Disconnected The session is active, but the client has disconnected from it. 826 | # Down The session is down due to an error. 827 | # Idle The session is waiting for a client to connect. 828 | # Initializing The session is initializing. 829 | # Listening The session is listening for connections. 830 | # Reset The session is being reset. 831 | # Shadowing This session is shadowing another session. 832 | # 833 | # .PARAMETER ClientName 834 | # The name of the machine last connected to a session. 835 | # Use this parameter to get sessions made from a specific computer name. Wildcrads are permitted. 836 | # 837 | # .PARAMETER UserName 838 | # Use this parameter to get sessions made by a specific user name. Wildcrads are permitted. 839 | # 840 | # .EXAMPLE 841 | # Get-TSSession 842 | # 843 | # Description 844 | # ----------- 845 | # Gets all the sessions from the local computer. 846 | # 847 | # .EXAMPLE 848 | # Get-TSSession -ComputerName comp1 -State Disconnected 849 | # 850 | # Description 851 | # ----------- 852 | # Gets all the disconnected sessions from the remote computer 'comp1'. 853 | # 854 | # .EXAMPLE 855 | # Get-TSSession -ComputerName comp1 -Filter {$_.ClientIPAddress -like '10*' -AND $_.ConnectionState -eq 'Active'} 856 | # 857 | # Description 858 | # ----------- 859 | # Gets all Active sessions from remote computer 'comp1', made from ip addresses that starts with '10'. 860 | # 861 | # .EXAMPLE 862 | # Get-TSSession -ComputerName comp1 -UserName a* 863 | # 864 | # Description 865 | # ----------- 866 | # Gets all sessions from remote computer 'comp1' made by users with name starts with the letter 'a'. 867 | # 868 | # .EXAMPLE 869 | # Get-TSSession -ComputerName comp1 -ClientName s* 870 | # 871 | # Description 872 | # ----------- 873 | # Gets all sessions from remote computer 'comp1' made from a computers names that starts with the letter 's'. 874 | # 875 | # .OUTPUTS 876 | # Cassia.Impl.TerminalServicesSession 877 | # 878 | # .COMPONENT 879 | # TerminalServer 880 | # 881 | # .NOTES 882 | # Author: Shay Levy 883 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 884 | # 885 | # .LINK 886 | # http://code.msdn.microsoft.com/PSTerminalServices 887 | # 888 | # .LINK 889 | # http://code.google.com/p/cassia/ 890 | # 891 | # .LINK 892 | # Stop-TSSession 893 | # Disconnect-TSSession 894 | # Send-TSMessage 895 | # #> 896 | # 897 | # 898 | # [OutputType('Cassia.Impl.TerminalServicesSession')] 899 | # [CmdletBinding(DefaultParameterSetName='Session')] 900 | # 901 | # Param( 902 | # 903 | # [Parameter()] 904 | # [Alias('CN','IPAddress')] 905 | # [System.String]$ComputerName, 906 | # 907 | # [Parameter( 908 | # Position=0, 909 | # ValueFromPipelineByPropertyName=$true, 910 | # ParameterSetName='Session' 911 | # )] 912 | # [Alias('SessionID')] 913 | # [ValidateRange(0,65536)] 914 | # [System.Int32]$Id=-1, 915 | # 916 | # [Parameter( 917 | # Position=0, 918 | # Mandatory=$true, 919 | # ValueFromPipeline=$true, 920 | # ParameterSetName='InputObject' 921 | # )] 922 | # [Cassia.Impl.TerminalServicesSession]$InputObject, 923 | # 924 | # [Parameter( 925 | # Mandatory=$true, 926 | # ParameterSetName='Filter' 927 | # )] 928 | # [ScriptBlock]$Filter, 929 | # 930 | # [Parameter()] 931 | # [ValidateSet('Active','Connected','ConnectQuery','Shadowing','Disconnected','Idle','Listening','Reset','Down','Initializing')] 932 | # [Alias('ConnectionState')] 933 | # [System.String]$State='*', 934 | # 935 | # [Parameter()] 936 | # [System.String]$ClientName='*', 937 | # 938 | # [Parameter()] 939 | # [System.String]$UserName='*' 940 | # ) 941 | # 942 | # 943 | # begin 944 | # { 945 | # try 946 | # { 947 | # $FuncName = $MyInvocation.MyCommand 948 | # Write-Verbose "[$funcName] Entering Begin block." 949 | # 950 | # if(!$ComputerName) 951 | # { 952 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 953 | # $ComputerName = Get-TSGlobalServerName 954 | # } 955 | # else 956 | # { 957 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 958 | # } 959 | # 960 | # 961 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 962 | # $TSManager = New-Object Cassia.TerminalServicesManager 963 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 964 | # $TSRemoteServer.Open() 965 | # 966 | # if(!$TSRemoteServer.IsOpen) 967 | # { 968 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 969 | # } 970 | # 971 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 972 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 973 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 974 | # } 975 | # catch 976 | # { 977 | # Throw 978 | # } 979 | # } 980 | # 981 | # 982 | # Process 983 | # { 984 | # 985 | # Write-Verbose "[$funcName] Entering Process block." 986 | # 987 | # try 988 | # { 989 | # if($PSCmdlet.ParameterSetName -eq 'Session') 990 | # { 991 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 992 | # if($Id -lt 0) 993 | # { 994 | # $session = $TSRemoteServer.GetSessions() 995 | # } 996 | # else 997 | # { 998 | # $session = $TSRemoteServer.GetSession($Id) 999 | # } 1000 | # } 1001 | # 1002 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 1003 | # { 1004 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1005 | # $session = $InputObject 1006 | # } 1007 | # 1008 | # if($PSCmdlet.ParameterSetName -eq 'Filter') 1009 | # { 1010 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1011 | # 1012 | # $TSRemoteServer.GetSessions() | Where-Object $Filter 1013 | # } 1014 | # 1015 | # if($session) 1016 | # { 1017 | # $session | Where-Object {$_.ConnectionState -like $State -AND $_.UserName -like $UserName -AND $_.ClientName -like $ClientName } | ` 1018 | # Add-Member -MemberType AliasProperty -Name IPAddress -Value ClientIPAddress -PassThru | ` 1019 | # Add-Member -MemberType AliasProperty -Name State -Value ConnectionState -PassThru 1020 | # } 1021 | # } 1022 | # catch 1023 | # { 1024 | # Throw 1025 | # } 1026 | # } 1027 | # 1028 | # end 1029 | # { 1030 | # try 1031 | # { 1032 | # Write-Verbose "[$funcName] Entering End block." 1033 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 1034 | # $TSRemoteServer.Close() 1035 | # $TSRemoteServer.Dispose() 1036 | # } 1037 | # catch 1038 | # { 1039 | # Throw 1040 | # } 1041 | # } 1042 | #} 1043 | # 1044 | #function Disconnect-TSSession 1045 | #{ 1046 | # 1047 | # <# 1048 | # .SYNOPSIS 1049 | # Disconnects any connected user from the session. 1050 | # 1051 | # .DESCRIPTION 1052 | # Disconnect-TSSession disconnects any connected user from a session on local or remote computers. 1053 | # 1054 | # .PARAMETER ComputerName 1055 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 1056 | # 1057 | # .PARAMETER Id 1058 | # Specifies the session Id number. 1059 | # 1060 | # .PARAMETER InputObject 1061 | # Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 1062 | # 1063 | # .PARAMETER Synchronous 1064 | # When the Synchronous parameter is present the command waits until the session is fully disconnected otherwise it returns 1065 | # immediately, even though the session may not be completely disconnected yet. 1066 | # 1067 | # .PARAMETER Force 1068 | # Overrides any confirmations made by the command. 1069 | # 1070 | # .EXAMPLE 1071 | # Get-TSSession -ComputerName comp1 | Disconnect-TSSession 1072 | # 1073 | # Description 1074 | # ----------- 1075 | # Disconnects all connected users from Active sessions on remote computer 'comp1'. The caller is prompted to 1076 | # By default, the caller is prompted to confirm each action. 1077 | # 1078 | # .EXAMPLE 1079 | # Get-TSSession -ComputerName comp1 -State Active | Disconnect-TSSession -Force 1080 | # 1081 | # Description 1082 | # ----------- 1083 | # Disconnects any connected user from Active sessions on remote computer 'comp1'. 1084 | # By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 1085 | # 1086 | # .EXAMPLE 1087 | # Get-TSSession -ComputerName comp1 -State Active -Synchronous | Disconnect-TSSession -Force 1088 | # 1089 | # Description 1090 | # ----------- 1091 | # Disconnects any connected user from Active sessions on remote computer 'comp1'. The Synchronous parameter tells the command to 1092 | # wait until the session is fully disconnected and only tghen it proceeds to the next session object. 1093 | # By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 1094 | # 1095 | # .OUTPUTS 1096 | # 1097 | # .COMPONENT 1098 | # TerminalServer 1099 | # 1100 | # .NOTES 1101 | # Author: Shay Levy 1102 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 1103 | # 1104 | # .LINK 1105 | # http://code.msdn.microsoft.com/PSTerminalServices 1106 | # 1107 | # .LINK 1108 | # http://code.google.com/p/cassia/ 1109 | # 1110 | # .LINK 1111 | # Get-TSSession 1112 | # Stop-TSSession 1113 | # Send-TSMessage 1114 | # #> 1115 | # 1116 | # [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High',DefaultParameterSetName='Id')] 1117 | # 1118 | # Param( 1119 | # 1120 | # [Parameter()] 1121 | # [Alias('CN','IPAddress')] 1122 | # [System.String]$ComputerName=$script:server, 1123 | # 1124 | # [Parameter( 1125 | # Position=0, 1126 | # Mandatory=$true, 1127 | # ParameterSetName='Id', 1128 | # ValueFromPipelineByPropertyName=$true 1129 | # )] 1130 | # [Alias('SessionId')] 1131 | # [System.Int32]$Id, 1132 | # 1133 | # [Parameter( 1134 | # Mandatory=$true, 1135 | # ValueFromPipeline=$true, 1136 | # ParameterSetName='InputObject' 1137 | # )] 1138 | # [Cassia.Impl.TerminalServicesSession]$InputObject, 1139 | # 1140 | # [switch]$Synchronous, 1141 | # 1142 | # [switch]$Force 1143 | # ) 1144 | # 1145 | # begin 1146 | # { 1147 | # try 1148 | # { 1149 | # $FuncName = $MyInvocation.MyCommand 1150 | # Write-Verbose "[$funcName] Entering Begin block." 1151 | # 1152 | # if(!$ComputerName) 1153 | # { 1154 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 1155 | # $ComputerName = Get-TSGlobalServerName 1156 | # } 1157 | # else 1158 | # { 1159 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 1160 | # } 1161 | # 1162 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 1163 | # $TSManager = New-Object Cassia.TerminalServicesManager 1164 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 1165 | # $TSRemoteServer.Open() 1166 | # 1167 | # if(!$TSRemoteServer.IsOpen) 1168 | # { 1169 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 1170 | # } 1171 | # 1172 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 1173 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 1174 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 1175 | # } 1176 | # catch 1177 | # { 1178 | # Throw 1179 | # } 1180 | # } 1181 | # 1182 | # 1183 | # Process 1184 | # { 1185 | # 1186 | # Write-Verbose "[$funcName] Entering Process block." 1187 | # 1188 | # try 1189 | # { 1190 | # if($PSCmdlet.ParameterSetName -eq 'Id') 1191 | # { 1192 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1193 | # $session = $TSRemoteServer.GetSession($Id) 1194 | # } 1195 | # 1196 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 1197 | # { 1198 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1199 | # $session = $InputObject 1200 | # } 1201 | # 1202 | # 1203 | # if($session -ne $null) 1204 | # { 1205 | # if($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName,"Disconnecting session id '$($session.sessionId)'")) 1206 | # { 1207 | # if($session.ConnectionState -ne 'Disconnected') 1208 | # { 1209 | # $session.Disconnect($Synchronous) 1210 | # } 1211 | # else 1212 | # { 1213 | # Write-Verbose 'Session is already in Disconnected mode.' 1214 | # } 1215 | # } 1216 | # } 1217 | # } 1218 | # catch 1219 | # { 1220 | # Throw 1221 | # } 1222 | # } 1223 | # 1224 | # end 1225 | # { 1226 | # try 1227 | # { 1228 | # Write-Verbose "[$funcName] Entering End block." 1229 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 1230 | # $TSRemoteServer.Close() 1231 | # $TSRemoteServer.Dispose() 1232 | # } 1233 | # catch 1234 | # { 1235 | # Throw 1236 | # } 1237 | # } 1238 | #} 1239 | # 1240 | #function Stop-TSSession 1241 | #{ 1242 | # 1243 | # <# 1244 | # .SYNOPSIS 1245 | # Logs the session off, disconnecting any user that might be connected. 1246 | # 1247 | # .DESCRIPTION 1248 | # Use Stop-TSSession to logoff the session and disconnect any user that might be connected. 1249 | # 1250 | # .PARAMETER ComputerName 1251 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 1252 | # 1253 | # .PARAMETER Id 1254 | # Specifies the session Id number. 1255 | # 1256 | # .PARAMETER InputObject 1257 | # Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 1258 | # 1259 | # .PARAMETER Synchronous 1260 | # When the Synchronous parameter is present the command waits until the session is fully disconnected otherwise it returns 1261 | # immediately, even though the session may not be completely disconnected yet. 1262 | # 1263 | # .PARAMETER Force 1264 | # Overrides any confirmations made by the command. 1265 | # 1266 | # .EXAMPLE 1267 | # Get-TSSession -ComputerName comp1 | Stop-TSSession 1268 | # 1269 | # Description 1270 | # ----------- 1271 | # logs off all connected users from Active sessions on remote computer 'comp1'. The caller is prompted to 1272 | # By default, the caller is prompted to confirm each action. 1273 | # 1274 | # .EXAMPLE 1275 | # Get-TSSession -ComputerName comp1 -State Active | Stop-TSSession -Force 1276 | # 1277 | # Description 1278 | # ----------- 1279 | # logs off any connected user from Active sessions on remote computer 'comp1'. 1280 | # By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 1281 | # 1282 | # .EXAMPLE 1283 | # Get-TSSession -ComputerName comp1 -State Active -Synchronous | Stop-TSSession -Force 1284 | # 1285 | # Description 1286 | # ----------- 1287 | # logs off any connected user from Active sessions on remote computer 'comp1'. The Synchronous parameter tells the command to 1288 | # wait until the session is fully disconnected and only tghen it proceeds to the next session object. 1289 | # By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 1290 | # 1291 | # .OUTPUTS 1292 | # 1293 | # .COMPONENT 1294 | # TerminalServer 1295 | # 1296 | # .NOTES 1297 | # Author: Shay Levy 1298 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 1299 | # 1300 | # .LINK 1301 | # http://code.msdn.microsoft.com/PSTerminalServices 1302 | # 1303 | # .LINK 1304 | # http://code.google.com/p/cassia/ 1305 | # 1306 | # .LINK 1307 | # Get-TSSession 1308 | # Disconnect-TSSession 1309 | # Send-TSMessage 1310 | # #> 1311 | # 1312 | # [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High',DefaultParameterSetName='Id')] 1313 | # 1314 | # Param( 1315 | # 1316 | # [Parameter()] 1317 | # [Alias('CN','IPAddress')] 1318 | # [System.String]$ComputerName=$script:server, 1319 | # 1320 | # [Parameter( 1321 | # Position=0, 1322 | # Mandatory=$true, 1323 | # ParameterSetName='Id', 1324 | # ValueFromPipelineByPropertyName=$true 1325 | # )] 1326 | # [Alias('SessionId')] 1327 | # [System.Int32]$Id, 1328 | # 1329 | # [Parameter( 1330 | # Mandatory=$true, 1331 | # ValueFromPipeline=$true, 1332 | # ParameterSetName='InputObject' 1333 | # )] 1334 | # [Cassia.Impl.TerminalServicesSession]$InputObject, 1335 | # 1336 | # [switch]$Synchronous, 1337 | # 1338 | # [switch]$Force 1339 | # ) 1340 | # 1341 | # begin 1342 | # { 1343 | # try 1344 | # { 1345 | # $FuncName = $MyInvocation.MyCommand 1346 | # Write-Verbose "[$funcName] Entering Begin block." 1347 | # 1348 | # if(!$ComputerName) 1349 | # { 1350 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 1351 | # $ComputerName = Get-TSGlobalServerName 1352 | # } 1353 | # else 1354 | # { 1355 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 1356 | # } 1357 | # 1358 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 1359 | # $TSManager = New-Object Cassia.TerminalServicesManager 1360 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 1361 | # $TSRemoteServer.Open() 1362 | # 1363 | # if(!$TSRemoteServer.IsOpen) 1364 | # { 1365 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 1366 | # } 1367 | # 1368 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 1369 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 1370 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 1371 | # } 1372 | # catch 1373 | # { 1374 | # Throw 1375 | # } 1376 | # } 1377 | # 1378 | # 1379 | # 1380 | # Process 1381 | # { 1382 | # 1383 | # Write-Verbose "[$funcName] Entering Process block." 1384 | # 1385 | # try 1386 | # { 1387 | # 1388 | # if($PSCmdlet.ParameterSetName -eq 'Id') 1389 | # { 1390 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1391 | # $session = $TSRemoteServer.GetSession($Id) 1392 | # } 1393 | # 1394 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 1395 | # { 1396 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1397 | # $session = $InputObject 1398 | # } 1399 | # 1400 | # if($session -ne $null) 1401 | # { 1402 | # if($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName,"Logging off session id '$($session.sessionId)'")) 1403 | # { 1404 | # Write-Verbose "[$FuncName] Logging off session '$($session.SessionId)'" 1405 | # $session.Logoff($Synchronous) 1406 | # } 1407 | # } 1408 | # } 1409 | # catch 1410 | # { 1411 | # Throw 1412 | # } 1413 | # } 1414 | # 1415 | # 1416 | # end 1417 | # { 1418 | # try 1419 | # { 1420 | # Write-Verbose "[$funcName] Entering End block." 1421 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 1422 | # $TSRemoteServer.Close() 1423 | # $TSRemoteServer.Dispose() 1424 | # } 1425 | # catch 1426 | # { 1427 | # Throw 1428 | # } 1429 | # } 1430 | #} 1431 | # 1432 | #function Get-TSProcess 1433 | #{ 1434 | # 1435 | # <# 1436 | # .SYNOPSIS 1437 | # Gets a list of processes running in a specific session or in all sessions. 1438 | # 1439 | # .DESCRIPTION 1440 | # Use Get-TSProcess to get a list of session processes from a local or remote computers. 1441 | # 1442 | # .PARAMETER ComputerName 1443 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 1444 | # 1445 | # .PARAMETER Id 1446 | # Specifies the process Id number. 1447 | # 1448 | # .PARAMETER InputObject 1449 | # Specifies a process object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 1450 | # 1451 | # .PARAMETER Name 1452 | # Specifies the process name. Wildcards are permitted. 1453 | # 1454 | # .PARAMETER Session 1455 | # Specifies the session Id number. 1456 | # 1457 | # .EXAMPLE 1458 | # Get-TSProcess 1459 | # 1460 | # Description 1461 | # ----------- 1462 | # Gets all the sessions processes from the local computer. 1463 | # 1464 | # .EXAMPLE 1465 | # Get-TSSession -Id 0 -ComputerName comp1 | Get-TSProcess 1466 | # 1467 | # Description 1468 | # ----------- 1469 | # Gets all processes connected to session id 0 from remote computer 'comp1'. 1470 | # 1471 | # .EXAMPLE 1472 | # Get-TSProcess -Name s* -ComputerName comp1 1473 | # 1474 | # Description 1475 | # ----------- 1476 | # Gets all the processes with name starts with the letter 's' from remote computer 'comp1'. 1477 | # 1478 | # .OUTPUTS 1479 | # Cassia.Impl.TerminalServicesProcess 1480 | # 1481 | # .COMPONENT 1482 | # TerminalServer 1483 | # 1484 | # .NOTES 1485 | # Author: Shay Levy 1486 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 1487 | # 1488 | # .LINK 1489 | # http://code.msdn.microsoft.com/PSTerminalServices 1490 | # 1491 | # .LINK 1492 | # http://code.google.com/p/cassia/ 1493 | # 1494 | # .LINK 1495 | # Get-TSSession 1496 | # Stop-TSProcess 1497 | # #> 1498 | # 1499 | # 1500 | # [OutputType('Cassia.Impl.TerminalServicesProcess')] 1501 | # [CmdletBinding(DefaultParameterSetName='Name')] 1502 | # 1503 | # Param( 1504 | # 1505 | # [Parameter()] 1506 | # [Alias('CN','IPAddress')] 1507 | # [System.String]$ComputerName=$script:server, 1508 | # 1509 | # [Parameter( 1510 | # Position=0, 1511 | # ValueFromPipelineByPropertyName=$true, 1512 | # ParameterSetName='Name' 1513 | # )] 1514 | # [Alias('ProcessName')] 1515 | # [System.String]$Name='*', 1516 | # 1517 | # [Parameter( 1518 | # Mandatory=$true, 1519 | # ValueFromPipeline=$true, 1520 | # ValueFromPipelineByPropertyName=$true, 1521 | # ParameterSetName='Id' 1522 | # )] 1523 | # [Alias('ProcessID')] 1524 | # [ValidateRange(0,65536)] 1525 | # [System.Int32]$Id=-1, 1526 | # 1527 | # 1528 | # [Parameter( 1529 | # Position=0, 1530 | # Mandatory=$true, 1531 | # ValueFromPipeline=$true, 1532 | # ParameterSetName='InputObject' 1533 | # )] 1534 | # [Cassia.Impl.TerminalServicesProcess]$InputObject, 1535 | # 1536 | # 1537 | # [Parameter( 1538 | # Position=0, 1539 | # Mandatory=$true, 1540 | # ValueFromPipeline=$true, 1541 | # ParameterSetName='Session' 1542 | # )] 1543 | # [Alias('SessionId')] 1544 | # [Cassia.Impl.TerminalServicesSession]$Session 1545 | # ) 1546 | # 1547 | # 1548 | # 1549 | # begin 1550 | # { 1551 | # $FuncName = $MyInvocation.MyCommand 1552 | # Write-Verbose "[$funcName] Entering Begin block." 1553 | # 1554 | # if(!$ComputerName) 1555 | # { 1556 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 1557 | # $ComputerName = Get-TSGlobalServerName 1558 | # } 1559 | # else 1560 | # { 1561 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 1562 | # } 1563 | # 1564 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 1565 | # $TSManager = New-Object Cassia.TerminalServicesManager 1566 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 1567 | # $TSRemoteServer.Open() 1568 | # 1569 | # if(!$TSRemoteServer.IsOpen) 1570 | # { 1571 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 1572 | # } 1573 | # 1574 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 1575 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 1576 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 1577 | # } 1578 | # 1579 | # 1580 | # 1581 | # Process 1582 | # { 1583 | # 1584 | # Write-Verbose "[$funcName] Entering Process block." 1585 | # 1586 | # try 1587 | # { 1588 | # 1589 | # if($PSCmdlet.ParameterSetName -eq 'Name') 1590 | # { 1591 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1592 | # if($Name -eq '*') 1593 | # { 1594 | # $proc = $TSRemoteServer.GetProcesses() 1595 | # } 1596 | # else 1597 | # { 1598 | # $proc = $TSRemoteServer.GetProcesses() | Where-Object {$_.ProcessName -like $Name} 1599 | # } 1600 | # } 1601 | # 1602 | # if($PSCmdlet.ParameterSetName -eq 'Id') 1603 | # { 1604 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1605 | # if($Id -lt 0) 1606 | # { 1607 | # $proc = $TSRemoteServer.GetProcesses() 1608 | # } 1609 | # else 1610 | # { 1611 | # $proc = $TSRemoteServer.GetProcess($Id) 1612 | # } 1613 | # } 1614 | # 1615 | # 1616 | # if($PSCmdlet.ParameterSetName -eq 'Session') 1617 | # { 1618 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1619 | # if($Session) 1620 | # { 1621 | # $proc = $Session.GetProcesses() 1622 | # } 1623 | # } 1624 | # 1625 | # 1626 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 1627 | # { 1628 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1629 | # $proc = $InputObject 1630 | # } 1631 | # 1632 | # 1633 | # if($proc) 1634 | # { 1635 | # $proc 1636 | # } 1637 | # } 1638 | # catch 1639 | # { 1640 | # Throw 1641 | # } 1642 | # } 1643 | # 1644 | # 1645 | # end 1646 | # { 1647 | # try 1648 | # { 1649 | # Write-Verbose "[$funcName] Entering End block." 1650 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 1651 | # $TSRemoteServer.Close() 1652 | # $TSRemoteServer.Dispose() 1653 | # } 1654 | # catch 1655 | # { 1656 | # Throw 1657 | # } 1658 | # } 1659 | #} 1660 | # 1661 | #function Stop-TSProcess 1662 | #{ 1663 | # 1664 | # <# 1665 | # .SYNOPSIS 1666 | # Terminates the process running in a specific session or in all sessions. 1667 | # 1668 | # .DESCRIPTION 1669 | # Use Stop-TSProcess to terminate one or more processes from a local or remote computers. 1670 | # 1671 | # .PARAMETER ComputerName 1672 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 1673 | # 1674 | # .PARAMETER Id 1675 | # Specifies the process Id number. 1676 | # 1677 | # .PARAMETER InputObject 1678 | # Specifies a process object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 1679 | # 1680 | # .PARAMETER Name 1681 | # Specifies the process name. 1682 | # 1683 | # .PARAMETER Session 1684 | # Specifies the session Id number. 1685 | # 1686 | # .PARAMETER Force 1687 | # Overrides any confirmations made by the command. 1688 | # 1689 | # .EXAMPLE 1690 | # Get-TSProcess -Id 6552 | Stop-TSProcess 1691 | # 1692 | # Description 1693 | # ----------- 1694 | # Gets process Id 6552 from the local computer and stop it. Confirmations needed. 1695 | # 1696 | # .EXAMPLE 1697 | # Get-TSSession -Id 3 -ComputerName comp1 | Stop-TSProcess -Force 1698 | # 1699 | # Description 1700 | # ----------- 1701 | # Terminats all processes connected to session id 3 from remote computer 'comp1', suppress confirmations. 1702 | # 1703 | # .OUTPUTS 1704 | # Cassia.Impl.TerminalServicesProcess 1705 | # 1706 | # .COMPONENT 1707 | # TerminalServer 1708 | # 1709 | # .NOTES 1710 | # Author: Shay Levy 1711 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 1712 | # 1713 | # .LINK 1714 | # http://code.msdn.microsoft.com/PSTerminalServices 1715 | # 1716 | # .LINK 1717 | # http://code.google.com/p/cassia/ 1718 | # 1719 | # .LINK 1720 | # Get-TSProcess 1721 | # Get-TSSession 1722 | # #> 1723 | # 1724 | # [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High',DefaultParameterSetName='Name')] 1725 | # 1726 | # Param( 1727 | # [Parameter()] 1728 | # [Alias('CN','IPAddress')] 1729 | # [System.String]$ComputerName=$script:server, 1730 | # 1731 | # [Parameter( 1732 | # Position=0, 1733 | # ValueFromPipelineByPropertyName=$true, 1734 | # ParameterSetName='Name' 1735 | # )] 1736 | # [Alias("ProcessName")] 1737 | # [System.String]$Name='*', 1738 | # 1739 | # [Parameter( 1740 | # Mandatory=$true, 1741 | # ValueFromPipeline=$true, 1742 | # ValueFromPipelineByPropertyName=$true, 1743 | # ParameterSetName='Id' 1744 | # )] 1745 | # [Alias('ProcessID')] 1746 | # [ValidateRange(0,65536)] 1747 | # [System.Int32]$Id=-1, 1748 | # 1749 | # [Parameter( 1750 | # Position=0, 1751 | # Mandatory=$true, 1752 | # ValueFromPipeline=$true, 1753 | # ParameterSetName='InputObject' 1754 | # )] 1755 | # [Cassia.Impl.TerminalServicesProcess]$InputObject, 1756 | # 1757 | # [Parameter( 1758 | # Position=0, 1759 | # Mandatory=$true, 1760 | # ValueFromPipeline=$true, 1761 | # ParameterSetName='Session' 1762 | # )] 1763 | # [Alias('SessionId')] 1764 | # [Cassia.Impl.TerminalServicesSession]$Session, 1765 | # 1766 | # [switch]$Force 1767 | # ) 1768 | # 1769 | # 1770 | # begin 1771 | # { 1772 | # try 1773 | # { 1774 | # $FuncName = $MyInvocation.MyCommand 1775 | # Write-Verbose "[$funcName] Entering Begin block." 1776 | # 1777 | # if(!$ComputerName) 1778 | # { 1779 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 1780 | # $ComputerName = Get-TSGlobalServerName 1781 | # } 1782 | # else 1783 | # { 1784 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 1785 | # } 1786 | # 1787 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 1788 | # $TSManager = New-Object Cassia.TerminalServicesManager 1789 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 1790 | # $TSRemoteServer.Open() 1791 | # 1792 | # if(!$TSRemoteServer.IsOpen) 1793 | # { 1794 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 1795 | # } 1796 | # 1797 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 1798 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 1799 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 1800 | # } 1801 | # catch 1802 | # { 1803 | # Throw 1804 | # } 1805 | # } 1806 | # 1807 | # 1808 | # Process 1809 | # { 1810 | # 1811 | # Write-Verbose "[$funcName] Entering Process block." 1812 | # 1813 | # try 1814 | # { 1815 | # 1816 | # if($PSCmdlet.ParameterSetName -eq 'Name') 1817 | # { 1818 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1819 | # if($Name -eq '*') 1820 | # { 1821 | # $proc = $TSRemoteServer.GetProcesses() 1822 | # } 1823 | # else 1824 | # { 1825 | # $proc = $TSRemoteServer.GetProcesses() | Where-Object {$_.ProcessName -like $Name} 1826 | # } 1827 | # } 1828 | # 1829 | # if($PSCmdlet.ParameterSetName -eq 'Id') 1830 | # { 1831 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1832 | # if($Id -lt 0) 1833 | # { 1834 | # $proc = $TSRemoteServer.GetProcesses() 1835 | # } 1836 | # else 1837 | # { 1838 | # $proc = $TSRemoteServer.GetProcess($Id) 1839 | # } 1840 | # } 1841 | # 1842 | # 1843 | # if($PSCmdlet.ParameterSetName -eq 'Session') 1844 | # { 1845 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1846 | # if($Session) 1847 | # { 1848 | # $proc = $Session.GetProcesses() 1849 | # } 1850 | # } 1851 | # 1852 | # 1853 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 1854 | # { 1855 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 1856 | # $proc = $InputObject 1857 | # } 1858 | # 1859 | # 1860 | # if($proc) 1861 | # { 1862 | # foreach($p in $proc) 1863 | # { 1864 | # if($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName,"Stop Process '$($p.ProcessName) ($($p.ProcessID))")) 1865 | # { 1866 | # Write-Verbose "[$FuncName] Killing process '$($p.ProcessName)' ($($p.ProcessId))" 1867 | # $p.Kill() 1868 | # } 1869 | # } 1870 | # } 1871 | # } 1872 | # catch 1873 | # { 1874 | # Throw 1875 | # } 1876 | # } 1877 | # 1878 | # 1879 | # end 1880 | # { 1881 | # try 1882 | # { 1883 | # Write-Verbose "[$funcName] Entering End block." 1884 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 1885 | # $TSRemoteServer.Close() 1886 | # $TSRemoteServer.Dispose() 1887 | # } 1888 | # catch 1889 | # { 1890 | # Throw 1891 | # } 1892 | # } 1893 | #} 1894 | # 1895 | #function Send-TSMessage 1896 | #{ 1897 | # 1898 | # <# 1899 | # .SYNOPSIS 1900 | # Displays a message box in the specified session Id. 1901 | # 1902 | # .DESCRIPTION 1903 | # Use Send-TSMessage display a message box in the specified session Id. 1904 | # 1905 | # .PARAMETER ComputerName 1906 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 1907 | # 1908 | # .PARAMETER Text 1909 | # The text to display in the message box. 1910 | # 1911 | # .PARAMETER SessionID 1912 | # The number of the session Id. 1913 | # 1914 | # .PARAMETER Caption 1915 | # The caption of the message box. The default caption is 'Alert'. 1916 | # 1917 | # .EXAMPLE 1918 | # $Message = "Importnat message`n, the server is going down for maintanace in 10 minutes. Please save your work and logoff." 1919 | # Get-TSSession -State Active -ComputerName comp1 | Send-TSMessage -Message $Message 1920 | # 1921 | # Description 1922 | # ----------- 1923 | # Displays a message box inside all active sessions of computer name 'comp1'. 1924 | # 1925 | # .OUTPUTS 1926 | # 1927 | # .COMPONENT 1928 | # TerminalServer 1929 | # 1930 | # .NOTES 1931 | # Author: Shay Levy 1932 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 1933 | # 1934 | # .LINK 1935 | # http://code.msdn.microsoft.com/PSTerminalServices 1936 | # 1937 | # .LINK 1938 | # http://code.google.com/p/cassia/ 1939 | # 1940 | # .LINK 1941 | # Get-TSSession 1942 | # #> 1943 | # 1944 | # 1945 | # [CmdletBinding(DefaultParameterSetName='Session')] 1946 | # 1947 | # Param( 1948 | # [Parameter()] 1949 | # [Alias('CN','IPAddress')] 1950 | # [System.String]$ComputerName=$script:server, 1951 | # 1952 | # [Parameter( 1953 | # Position=0, 1954 | # Mandatory=$true, 1955 | # HelpMessage='The text to display in the message box.' 1956 | # )] 1957 | # [System.String]$Text, 1958 | # 1959 | # [Parameter( 1960 | # HelpMessage='The caption of the message box.' 1961 | # )] 1962 | # [ValidateNotNullOrEmpty()] 1963 | # [System.String]$Caption='Alert', 1964 | # 1965 | # [Parameter( 1966 | # Position=0, 1967 | # ValueFromPipelineByPropertyName=$true, 1968 | # ParameterSetName='Session' 1969 | # )] 1970 | # [Alias('SessionID')] 1971 | # [ValidateRange(0,65536)] 1972 | # [System.Int32]$Id=-1, 1973 | # 1974 | # [Parameter( 1975 | # Position=0, 1976 | # Mandatory=$true, 1977 | # ValueFromPipeline=$true, 1978 | # ParameterSetName='InputObject' 1979 | # )] 1980 | # [Cassia.Impl.TerminalServicesSession]$InputObject 1981 | # ) 1982 | # 1983 | # begin 1984 | # { 1985 | # try 1986 | # { 1987 | # $FuncName = $MyInvocation.MyCommand 1988 | # Write-Verbose "[$funcName] Entering Begin block." 1989 | # 1990 | # if(!$ComputerName) 1991 | # { 1992 | # Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 1993 | # $ComputerName = Get-TSGlobalServerName 1994 | # } 1995 | # else 1996 | # { 1997 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 1998 | # } 1999 | # 2000 | # Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 2001 | # $TSManager = New-Object Cassia.TerminalServicesManager 2002 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 2003 | # $TSRemoteServer.Open() 2004 | # 2005 | # if(!$TSRemoteServer.IsOpen) 2006 | # { 2007 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 2008 | # } 2009 | # 2010 | # Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 2011 | # Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 2012 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 2013 | # } 2014 | # catch 2015 | # { 2016 | # Throw 2017 | # } 2018 | # } 2019 | # 2020 | # 2021 | # process 2022 | # { 2023 | # 2024 | # Write-Verbose "[$funcName] Entering Process block." 2025 | # 2026 | # try 2027 | # { 2028 | # 2029 | # if($PSCmdlet.ParameterSetName -eq 'Session') 2030 | # { 2031 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 2032 | # if($Id -ge 0) 2033 | # { 2034 | # $session = $TSRemoteServer.GetSession($Id) 2035 | # } 2036 | # } 2037 | # 2038 | # if($PSCmdlet.ParameterSetName -eq 'InputObject') 2039 | # { 2040 | # Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 2041 | # $session = $InputObject 2042 | # } 2043 | # 2044 | # if($session) 2045 | # { 2046 | # Write-Verbose "[$FuncName] Sending alert message to session id: '$($session.SessionId)' on '$ComputerName'" 2047 | # $session.MessageBox($Text,$Caption) 2048 | # } 2049 | # } 2050 | # catch 2051 | # { 2052 | # Throw 2053 | # } 2054 | # } 2055 | # 2056 | # 2057 | # end 2058 | # { 2059 | # try 2060 | # { 2061 | # Write-Verbose "[$funcName] Entering End block." 2062 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 2063 | # $TSRemoteServer.Close() 2064 | # $TSRemoteServer.Dispose() 2065 | # } 2066 | # catch 2067 | # { 2068 | # Throw 2069 | # } 2070 | # } 2071 | #} 2072 | # 2073 | #function Get-TSServers 2074 | #{ 2075 | # 2076 | # <# 2077 | # .SYNOPSIS 2078 | # Enumerates all terminal servers in a given domain. 2079 | # 2080 | # .DESCRIPTION 2081 | # Enumerates all terminal servers in a given domain. 2082 | # 2083 | # .PARAMETER ComputerName 2084 | # The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 2085 | # 2086 | # .PARAMETER DomainName 2087 | # The name of the domain. The default is the caller domain name ($env:USERDOMAIN). 2088 | # 2089 | # .EXAMPLE 2090 | # Get-TSDomainServers 2091 | # 2092 | # Description 2093 | # ----------- 2094 | # Get a list of all terminal servers of the caller default domain. 2095 | # 2096 | # .OUTPUTS 2097 | # 2098 | # .COMPONENT 2099 | # TerminalServer 2100 | # 2101 | # .NOTES 2102 | # Author: Shay Levy 2103 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 2104 | # 2105 | # .LINK 2106 | # http://code.msdn.microsoft.com/PSTerminalServices 2107 | # 2108 | # .LINK 2109 | # http://code.google.com/p/cassia/ 2110 | # 2111 | # .LINK 2112 | # Get-TSSession 2113 | # #> 2114 | # 2115 | # 2116 | # [OutputType('System.Management.Automation.PSCustomObject')] 2117 | # [CmdletBinding()] 2118 | # 2119 | # Param( 2120 | # [Parameter( 2121 | # Position=0, 2122 | # ParameterSetName='Name' 2123 | # )] 2124 | # [System.String]$DomainName=$env:USERDOMAIN 2125 | # ) 2126 | # 2127 | # 2128 | # try 2129 | # { 2130 | # $FuncName = $MyInvocation.MyCommand 2131 | # if(!$ComputerName) 2132 | # { 2133 | # Write-Verbose "[$funcName] ComputerName is not defined, loading global value '$script:Server'." 2134 | # $ComputerName = Get-TSGlobalServerName 2135 | # } 2136 | # else 2137 | # { 2138 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 2139 | # } 2140 | # 2141 | # Write-Verbose "[$funcName] Enumerating terminal servers for '$DomainName' domain." 2142 | # Write-Warning 'Depending on your environment the command may take a while to complete.' 2143 | # $TSManager = New-Object Cassia.TerminalServicesManager 2144 | # $TSManager.GetServers($DomainName) 2145 | # } 2146 | # catch 2147 | # { 2148 | # Throw 2149 | # } 2150 | # 2151 | #} 2152 | # 2153 | #function Get-TSCurrentSession 2154 | #{ 2155 | # 2156 | # <# 2157 | # .SYNOPSIS 2158 | # Provides information about the session in which the current process is running. 2159 | # 2160 | # .DESCRIPTION 2161 | # Provides information about the session in which the current process is running. 2162 | # 2163 | # .EXAMPLE 2164 | # Get-TSCurrentSession 2165 | # 2166 | # Description 2167 | # ----------- 2168 | # Displays the session in which the current process is running on the local computer. 2169 | # 2170 | # .OUTPUTS 2171 | # Cassia.Impl.TerminalServicesSession 2172 | # 2173 | # .COMPONENT 2174 | # TerminalServer 2175 | # 2176 | # .NOTES 2177 | # Author: Shay Levy 2178 | # Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 2179 | # 2180 | # .LINK 2181 | # http://code.msdn.microsoft.com/PSTerminalServices 2182 | # 2183 | # .LINK 2184 | # http://code.google.com/p/cassia/ 2185 | # 2186 | # .LINK 2187 | # Get-TSSession 2188 | # #> 2189 | # 2190 | # 2191 | # [OutputType('Cassia.Impl.TerminalServicesSession')] 2192 | # [CmdletBinding()] 2193 | # 2194 | # param( 2195 | # [Parameter()] 2196 | # [Alias('CN','IPAddress')] 2197 | # [System.String]$ComputerName=$script:server 2198 | # ) 2199 | # 2200 | # 2201 | # try 2202 | # { 2203 | # $FuncName = $MyInvocation.MyCommand 2204 | # 2205 | # if(!$ComputerName) 2206 | # { 2207 | # Write-Verbose "[$funcName] ComputerName is not defined, loading global value '$script:Server'." 2208 | # $ComputerName = Get-TSGlobalServerName 2209 | # } 2210 | # else 2211 | # { 2212 | # $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 2213 | # } 2214 | # 2215 | # Write-Verbose "[$funcName] Attempting remote connection to '$ComputerName'" 2216 | # $TSManager = New-Object Cassia.TerminalServicesManager 2217 | # $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 2218 | # $TSRemoteServer.Open() 2219 | # 2220 | # if(!$TSRemoteServer.IsOpen) 2221 | # { 2222 | # Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 2223 | # } 2224 | # 2225 | # Write-Verbose "[$funcName] Connection is open '$ComputerName'" 2226 | # Write-Verbose "[$funcName] Updating global Server name '$ComputerName'" 2227 | # $null = Set-TSGlobalServerName -ComputerName $ComputerName 2228 | # 2229 | # Write-Verbose "[$funcName] Get CurrentSession from '$ComputerName'" 2230 | # $TSManager.CurrentSession 2231 | # 2232 | # Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 2233 | # $TSRemoteServer.Close() 2234 | # $TSRemoteServer.Dispose() 2235 | # } 2236 | # catch 2237 | # { 2238 | # Throw 2239 | # } 2240 | #} 2241 | # 2242 | #function Set-TSGlobalServerName 2243 | #{ 2244 | # [CmdletBinding()] 2245 | # 2246 | # Param( 2247 | # [Parameter(Mandatory=$true)] 2248 | # [ValidateNotNullOrEmpty()] 2249 | # [System.String]$ComputerName 2250 | # ) 2251 | # 2252 | # if($ComputerName -eq "." -OR $ComputerName -eq $env:COMPUTERNAME) 2253 | # { 2254 | # $ComputerName='localhost' 2255 | # } 2256 | # 2257 | # $script:Server=$ComputerName 2258 | # $script:Server 2259 | #} 2260 | # 2261 | #function Get-TSGlobalServerName 2262 | #{ 2263 | # $script:Server 2264 | #} 2265 | # 2266 | # -------------------------------------------------------------------------------- /archives/LazyTS.psproj: -------------------------------------------------------------------------------- 1 |  2 | 2.0 3 | 8c368515-c779-4ebb-95db-cf067966c8e3 4 | 0 5 | 6 | functions 7 | 8 | 9 | Startup.pss 10 | MainForm.psf 11 | Globals.ps1 12 | Cassia.dll 13 | functions\Append-RichtextboxStatus.ps1 14 | functions\Clear-DataGridViewSelection.ps1 15 | functions\ConvertTo-DataTable.ps1 16 | functions\Disable-Button.ps1 17 | functions\Disconnect-TSSession.ps1 18 | functions\Enable-Button.ps1 19 | functions\Find-DataGridViewValue.ps1 20 | functions\Get-TSCurrentSession.ps1 21 | functions\Get-TSGlobalServerName.ps1 22 | functions\Get-TSProcess.ps1 23 | functions\Get-TSServers.ps1 24 | functions\Get-TSSession.ps1 25 | functions\Load-DataGridView.ps1 26 | functions\New-MessageBox.ps1 27 | functions\Reset-DataGridViewFormat.ps1 28 | functions\Reset-TextBox.ps1 29 | functions\Send-TSMessage.ps1 30 | functions\Set-DataGridView.ps1 31 | functions\Set-DataGridViewFilter.ps1 32 | functions\Set-TextBox.ps1 33 | functions\Set-TSGlobalServerName.ps1 34 | functions\Stop-TSProcess.ps1 35 | functions\Stop-TSSession.ps1 36 | 37 | -------------------------------------------------------------------------------- /archives/LazyTS.psproj.psbuild: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/LazyTS.psproj.psbuild -------------------------------------------------------------------------------- /archives/LazyTS.psprojs: -------------------------------------------------------------------------------- 1 |  2 | 1.0 3 | 8c368515-c779-4ebb-95db-cf067966c8e3 4 | 5 | functions 6 | 7 | 8 | -------------------------------------------------------------------------------- /archives/Startup.pss: -------------------------------------------------------------------------------- 1 |  2 | 16 | 17 | mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 18 | System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 19 | System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 20 | System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 21 | System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 22 | System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 23 | System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 24 | System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 25 | System.ServiceProcess, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 26 | 27 | -------------------------------------------------------------------------------- /archives/images/1412236864_20737.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/images/1412236864_20737.ico -------------------------------------------------------------------------------- /archives/images/exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/images/exit.png -------------------------------------------------------------------------------- /archives/images/message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/images/message.png -------------------------------------------------------------------------------- /archives/images/running_process.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/images/running_process.png -------------------------------------------------------------------------------- /archives/images/terminal-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/archives/images/terminal-icon.png -------------------------------------------------------------------------------- /media/Cassia.dll_required.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/Cassia.dll_required.png -------------------------------------------------------------------------------- /media/LazyTS-Highlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Highlight.png -------------------------------------------------------------------------------- /media/LazyTS-RDP_Shadow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-RDP_Shadow.png -------------------------------------------------------------------------------- /media/LazyTS-Sending_Message-Confirmation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Sending_Message-Confirmation.png -------------------------------------------------------------------------------- /media/LazyTS-Sending_Message-Custom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Sending_Message-Custom.png -------------------------------------------------------------------------------- /media/LazyTS-Sending_Message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Sending_Message.png -------------------------------------------------------------------------------- /media/LazyTS-Shadow_Control.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Shadow_Control.png -------------------------------------------------------------------------------- /media/LazyTS-Shadow_Deny.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Shadow_Deny.png -------------------------------------------------------------------------------- /media/LazyTS-Stopping_Processes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Stopping_Processes.png -------------------------------------------------------------------------------- /media/LazyTS-Stopping_Processes_Confirmation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS-Stopping_Processes_Confirmation.png -------------------------------------------------------------------------------- /media/LazyTS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS.png -------------------------------------------------------------------------------- /media/LazyTS_Closing_sessions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_Closing_sessions.png -------------------------------------------------------------------------------- /media/LazyTS_Closing_sessions_Confirmation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_Closing_sessions_Confirmation.png -------------------------------------------------------------------------------- /media/LazyTS_DisconnectedUser_message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_DisconnectedUser_message.png -------------------------------------------------------------------------------- /media/LazyTS_Disconnecting_sessions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_Disconnecting_sessions.png -------------------------------------------------------------------------------- /media/LazyTS_Query_processes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_Query_processes.png -------------------------------------------------------------------------------- /media/LazyTS_Query_sessions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/LazyTS_Query_sessions.png -------------------------------------------------------------------------------- /media/User_message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/media/User_message.png -------------------------------------------------------------------------------- /source/Cassia.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazywinadmin/LazyTS/59bdd4c5190fb639558c4363cee8bd4a93b20eee/source/Cassia.dll -------------------------------------------------------------------------------- /source/config.psd1: -------------------------------------------------------------------------------- 1 | @{ 2 | 3 | Name = 'LazyTS' 4 | Version = '1.3' 5 | ProjectURI = 'https://github.com/lazywinadmin/LazyTS' 6 | Author = @{ 7 | Name = 'Francois-Xavier Cat' 8 | Email = 'fxcat@lazywinadmin.com' 9 | Website = @{ 10 | name = 'LazyWinAdmin.com' 11 | uri = 'http://lazywinadmin.com' 12 | } 13 | } 14 | Settings = @{ 15 | computer = '' # if empty, Default Computer will be local computer 16 | } 17 | } -------------------------------------------------------------------------------- /source/functions/Append-RichtextboxStatus.ps1: -------------------------------------------------------------------------------- 1 | function Append-RichtextboxStatus 2 | { 3 | PARAM ( 4 | [Parameter(Mandatory = $true)] 5 | [string]$Message, 6 | [string]$MessageColor = "DarkGreen", 7 | [string]$DateTimeColor = "Black", 8 | [string]$Source, 9 | [string]$SourceColor = "Gray", 10 | [string]$ComputerName, 11 | [String]$ComputerNameColor = "Blue") 12 | 13 | $SortableTime = get-date -Format "yyyy-MM-dd HH:mm:ss" 14 | $richtextboxStatus.SelectionColor = $DateTimeColor 15 | $richtextboxStatus.AppendText("[$SortableTime] ") 16 | 17 | IF ($PSBoundParameters['ComputerName']) 18 | { 19 | $richtextboxStatus.SelectionColor = $ComputerNameColor 20 | $richtextboxStatus.AppendText(("$ComputerName ").ToUpper()) 21 | } 22 | 23 | IF ($PSBoundParameters['Source']) 24 | { 25 | $richtextboxStatus.SelectionColor = $SourceColor 26 | $richtextboxStatus.AppendText("$Source ") 27 | } 28 | 29 | $richtextboxStatus.SelectionColor = $MessageColor 30 | $richtextboxStatus.AppendText("$Message`r") 31 | $richtextboxStatus.Refresh() 32 | $richtextboxStatus.ScrollToCaret() 33 | 34 | Write-Verbose -Message "$SortableTime $Message" 35 | } -------------------------------------------------------------------------------- /source/functions/Clear-DataGridViewSelection.ps1: -------------------------------------------------------------------------------- 1 | function Clear-DataGridViewSelection 2 | { 3 | PARAM ( 4 | [Parameter(Mandatory = $true)] 5 | [System.Windows.Forms.DataGridView]$DataGridView 6 | ) 7 | $DataGridView.ClearSelection() 8 | } -------------------------------------------------------------------------------- /source/functions/ConvertTo-DataTable.ps1: -------------------------------------------------------------------------------- 1 | function ConvertTo-DataTable 2 | { 3 | <# 4 | .SYNOPSIS 5 | Converts objects into a DataTable. 6 | 7 | .DESCRIPTION 8 | Converts objects into a DataTable, which are used for DataBinding. 9 | 10 | .PARAMETER InputObject 11 | The input to convert into a DataTable. 12 | 13 | .PARAMETER Table 14 | The DataTable you wish to load the input into. 15 | 16 | .PARAMETER RetainColumns 17 | This switch tells the function to keep the DataTable's existing columns. 18 | 19 | .PARAMETER FilterWMIProperties 20 | This switch removes WMI properties that start with an underline. 21 | 22 | .EXAMPLE 23 | $DataTable = ConvertTo-DataTable -InputObject (Get-Process) 24 | 25 | .NOTES 26 | SAPIEN Technologies, Inc. 27 | http://www.sapien.com/ 28 | 29 | VERSION HISTORY 30 | 1.0 ????/??/?? From Sapien.com Version 31 | 2.0 2014/12/03 Francois-Xavier Cat - In the rows workk, I added a 32 | small piece of code to handle the $null value with [DBNull]::Value 33 | 34 | #> 35 | [CmdletBinding()] 36 | [OutputType([System.Data.DataTable])] 37 | param ( 38 | [ValidateNotNull()] 39 | $InputObject, 40 | [ValidateNotNull()] 41 | [System.Data.DataTable]$Table, 42 | [switch]$RetainColumns, 43 | [switch]$FilterWMIProperties 44 | ) 45 | 46 | if ($Table -eq $null) 47 | { 48 | $Table = New-Object System.Data.DataTable 49 | } 50 | 51 | if ($InputObject -is [System.Data.DataTable]) 52 | { 53 | $Table = $InputObject 54 | } 55 | else 56 | { 57 | if (-not $RetainColumns -or $Table.Columns.Count -eq 0) 58 | { 59 | #Clear out the Table Contents 60 | $Table.Clear() 61 | 62 | if ($InputObject -eq $null) { return } #Empty Data 63 | 64 | $object = $null 65 | 66 | #find the first non null value 67 | foreach ($item in $InputObject) 68 | { 69 | if ($item -ne $null) 70 | { 71 | $object = $item 72 | break 73 | } 74 | } 75 | 76 | if ($object -eq $null) { return } #All null then empty 77 | 78 | #COLUMN 79 | #Get all the properties in order to create the columns 80 | foreach ($prop in $object.PSObject.Get_Properties()) 81 | { 82 | if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties 83 | { 84 | #Get the type from the Definition string 85 | $type = $null 86 | 87 | if ($prop.Value -ne $null) 88 | { 89 | try { $type = $prop.Value.GetType() } 90 | catch { Write-Verbose -Message "Can't find type of $prop" } 91 | } 92 | 93 | if ($type -ne $null) # -and [System.Type]::GetTypeCode($type) -ne 'Object') 94 | { 95 | Write-Verbose -Message "Creating Column: $($Prop.name) (Type: $type)" 96 | [void]$table.Columns.Add($prop.Name, $type) 97 | } 98 | else #Type info not found 99 | { 100 | #if ($prop.name -eq "" -or $prop.name -eq $null) { [void]$table.Columns.Add([DBNull]::Value) } 101 | [void]$table.Columns.Add($prop.Name) 102 | } 103 | } 104 | } 105 | 106 | if ($object -is [System.Data.DataRow]) 107 | { 108 | foreach ($item in $InputObject) 109 | { 110 | $Table.Rows.Add($item) 111 | } 112 | return @( , $Table) 113 | } 114 | } 115 | else 116 | { 117 | $Table.Rows.Clear() 118 | } 119 | 120 | #Rows Work 121 | foreach ($item in $InputObject) 122 | { 123 | # Create a new row object 124 | $row = $table.NewRow() 125 | 126 | if ($item) 127 | { 128 | foreach ($prop in $item.PSObject.Get_Properties()) 129 | { 130 | #Find the appropriate column to put the value 131 | if ($table.Columns.Contains($prop.Name)) 132 | { 133 | if ($prop.value -eq $null) { $prop.value = [DBNull]::Value } 134 | $row.Item($prop.Name) = $prop.Value 135 | } 136 | } 137 | } 138 | [void]$table.Rows.Add($row) 139 | } 140 | } 141 | 142 | return @( , $Table) 143 | } -------------------------------------------------------------------------------- /source/functions/Disable-Button.ps1: -------------------------------------------------------------------------------- 1 | function Disable-Button 2 | { 3 | <# 4 | .SYNOPSIS 5 | This function will disable a button control 6 | .EXAMPLE 7 | Disable-Button -Button $Button 8 | .NOTES 9 | Francois-Xavier Cat 10 | @lazywinadm 11 | www.lazywinadmin.com 12 | #> 13 | [CmdletBinding()] 14 | PARAM ( 15 | [ValidateNotNull()] 16 | [Parameter(Mandatory = $true)] 17 | [System.Windows.Forms.Button[]]$Button 18 | ) 19 | BEGIN 20 | { 21 | Add-Type -AssemblyName System.Windows.Forms 22 | } 23 | PROCESS 24 | { 25 | foreach ($ButtonObject in $Button) 26 | { 27 | $ButtonObject.Enabled = $false 28 | } 29 | 30 | } 31 | } #Disable-Button -------------------------------------------------------------------------------- /source/functions/Disconnect-TSSession.ps1: -------------------------------------------------------------------------------- 1 | function Disconnect-TSSession 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Disconnects any connected user from the session. 7 | 8 | .DESCRIPTION 9 | Disconnect-TSSession disconnects any connected user from a session on local or remote computers. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER Id 15 | Specifies the session Id number. 16 | 17 | .PARAMETER InputObject 18 | Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 19 | 20 | .PARAMETER Synchronous 21 | When the Synchronous parameter is present the command waits until the session is fully disconnected otherwise it returns 22 | immediately, even though the session may not be completely disconnected yet. 23 | 24 | .PARAMETER Force 25 | Overrides any confirmations made by the command. 26 | 27 | .EXAMPLE 28 | Get-TSSession -ComputerName comp1 | Disconnect-TSSession 29 | 30 | Description 31 | ----------- 32 | Disconnects all connected users from Active sessions on remote computer 'comp1'. The caller is prompted to 33 | By default, the caller is prompted to confirm each action. 34 | 35 | .EXAMPLE 36 | Get-TSSession -ComputerName comp1 -State Active | Disconnect-TSSession -Force 37 | 38 | Description 39 | ----------- 40 | Disconnects any connected user from Active sessions on remote computer 'comp1'. 41 | By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 42 | 43 | .EXAMPLE 44 | Get-TSSession -ComputerName comp1 -State Active -Synchronous | Disconnect-TSSession -Force 45 | 46 | Description 47 | ----------- 48 | Disconnects any connected user from Active sessions on remote computer 'comp1'. The Synchronous parameter tells the command to 49 | wait until the session is fully disconnected and only tghen it proceeds to the next session object. 50 | By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 51 | 52 | .OUTPUTS 53 | 54 | .COMPONENT 55 | TerminalServer 56 | 57 | .NOTES 58 | Author: Shay Levy 59 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 60 | 61 | .LINK 62 | http://code.msdn.microsoft.com/PSTerminalServices 63 | 64 | .LINK 65 | http://code.google.com/p/cassia/ 66 | 67 | .LINK 68 | Get-TSSession 69 | Stop-TSSession 70 | Send-TSMessage 71 | #> 72 | 73 | [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High', DefaultParameterSetName = 'Id')] 74 | Param ( 75 | 76 | [Parameter()] 77 | [Alias('CN', 'IPAddress')] 78 | [System.String]$ComputerName = $script:server, 79 | [Parameter( 80 | Position = 0, 81 | Mandatory = $true, 82 | ParameterSetName = 'Id', 83 | ValueFromPipelineByPropertyName = $true 84 | )] 85 | [Alias('SessionId')] 86 | [System.Int32]$Id, 87 | [Parameter( 88 | Mandatory = $true, 89 | ValueFromPipeline = $true, 90 | ParameterSetName = 'InputObject' 91 | )] 92 | [Cassia.Impl.TerminalServicesSession]$InputObject, 93 | [switch]$Synchronous, 94 | [switch]$Force 95 | ) 96 | 97 | begin 98 | { 99 | try 100 | { 101 | $FuncName = $MyInvocation.MyCommand 102 | Write-Verbose "[$funcName] Entering Begin block." 103 | 104 | if (!$ComputerName) 105 | { 106 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 107 | $ComputerName = Get-TSGlobalServerName 108 | } 109 | else 110 | { 111 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 112 | } 113 | 114 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 115 | $TSManager = New-Object Cassia.TerminalServicesManager 116 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 117 | $TSRemoteServer.Open() 118 | 119 | if (!$TSRemoteServer.IsOpen) 120 | { 121 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 122 | } 123 | 124 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 125 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 126 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 127 | } 128 | catch 129 | { 130 | Throw 131 | } 132 | } 133 | 134 | 135 | Process 136 | { 137 | 138 | Write-Verbose "[$funcName] Entering Process block." 139 | 140 | try 141 | { 142 | if ($PSCmdlet.ParameterSetName -eq 'Id') 143 | { 144 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 145 | $session = $TSRemoteServer.GetSession($Id) 146 | } 147 | 148 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 149 | { 150 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 151 | $session = $InputObject 152 | } 153 | 154 | 155 | if ($session -ne $null) 156 | { 157 | if ($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName, "Disconnecting session id '$($session.sessionId)'")) 158 | { 159 | if ($session.ConnectionState -ne 'Disconnected') 160 | { 161 | $session.Disconnect($Synchronous) 162 | } 163 | else 164 | { 165 | Write-Verbose 'Session is already in Disconnected mode.' 166 | } 167 | } 168 | } 169 | } 170 | catch 171 | { 172 | Throw 173 | } 174 | } 175 | 176 | end 177 | { 178 | try 179 | { 180 | Write-Verbose "[$funcName] Entering End block." 181 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 182 | $TSRemoteServer.Close() 183 | $TSRemoteServer.Dispose() 184 | } 185 | catch 186 | { 187 | Throw 188 | } 189 | } 190 | } -------------------------------------------------------------------------------- /source/functions/Enable-Button.ps1: -------------------------------------------------------------------------------- 1 | function Enable-Button 2 | { 3 | <# 4 | .SYNOPSIS 5 | This function will enable a button control 6 | .EXAMPLE 7 | Enable-Button -Button $Button 8 | .NOTES 9 | Francois-Xavier Cat 10 | @lazywinadm 11 | www.lazywinadmin.com 12 | #> 13 | [CmdletBinding()] 14 | PARAM ( 15 | [ValidateNotNull()] 16 | [Parameter(Mandatory = $true)] 17 | [System.Windows.Forms.Button[]]$Button 18 | ) 19 | BEGIN 20 | { 21 | Add-Type -AssemblyName System.Windows.Forms 22 | } 23 | PROCESS 24 | { 25 | foreach ($ButtonObject in $Button) 26 | { 27 | $ButtonObject.Enabled = $true 28 | } 29 | } 30 | } #Enable-Button 31 | -------------------------------------------------------------------------------- /source/functions/Find-DataGridViewValue.ps1: -------------------------------------------------------------------------------- 1 | function Find-DataGridViewValue 2 | { 3 | <# 4 | .SYNOPSIS 5 | The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. 6 | 7 | .DESCRIPTION 8 | The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. 9 | 10 | .PARAMETER DataGridView 11 | Specifies the DataGridView Control to use 12 | 13 | .PARAMETER RowBackColor 14 | Specifies the back color of the row to use 15 | 16 | .PARAMETER RowForeColor 17 | Specifies the fore color of the row to use 18 | 19 | .PARAMETER SelectCell 20 | Specifies to select only the cell when the value is found 21 | 22 | .PARAMETER SelectRow 23 | Specifies to select the entire row when the value is found 24 | 25 | .PARAMETER Value 26 | Specifies the value to search 27 | 28 | .EXAMPLE 29 | PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text 30 | 31 | This will find the value and select the cell(s) 32 | 33 | .EXAMPLE 34 | PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -RowForeColor 'Red' -RowBackColor 'Black' 35 | 36 | This will find the value and color the fore and back of the row 37 | .EXAMPLE 38 | PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -SelectRow 39 | 40 | This will find the value and select the entire row 41 | 42 | .NOTES 43 | Francois-Xavier Cat 44 | @lazywinadm 45 | www.lazywinadmin.com 46 | #> 47 | [CmdletBinding(DefaultParameterSetName = "Cell")] 48 | PARAM ( 49 | [Parameter(Mandatory = $true)] 50 | [System.Windows.Forms.DataGridView]$DataGridView, 51 | [ValidateNotNull()] 52 | [Parameter(Mandatory = $true)] 53 | [String]$Value, 54 | [Parameter(ParameterSetName = "Cell")] 55 | [Switch]$SelectCell, 56 | [Parameter(ParameterSetName = "Row")] 57 | [Switch]$SelectRow, 58 | #[Parameter(ParameterSetName = "Column")] 59 | 60 | #[Switch]$SelectColumn, 61 | 62 | [Parameter(ParameterSetName = "RowColor")] 63 | [system.Drawing.Color]$RowForeColor, 64 | [Parameter(ParameterSetName = "RowColor")] 65 | [system.Drawing.Color]$RowBackColor 66 | ) 67 | 68 | PROCESS 69 | { 70 | $DataGridView.ClearSelection() 71 | 72 | FOR ([int]$i = 0; $i -lt $DataGridView.RowCount; $i++) 73 | { 74 | FOR ([int]$j = 0; $j -lt $DataGridView.ColumnCount; $j++) 75 | { 76 | $CurrentCell = $dataGridView.Rows[$i].Cells[$j] 77 | 78 | #if ((-not $CurrentCell.Value.Equals([DBNull]::Value)) -and ($CurrentCell.Value.ToString() -like "*$Value*")) 79 | if ($CurrentCell.Value.ToString() -match $Value) 80 | { 81 | 82 | # Row Selection 83 | IF ($PSBoundParameters['SelectRow']) 84 | { 85 | $dataGridView.Rows[$i].Selected = $true 86 | } 87 | 88 | <# 89 | # Column Selection 90 | IF ($PSBoundParameters['SelectColumn']) 91 | { 92 | #$DataGridView.Columns[$($CurrentCell.ColumnIndex)].Selected = $true 93 | #$DataGridView.Columns[$j].Selected = $true 94 | #$CurrentCell.DataGridView.Columns[$j].Selected = $true 95 | } 96 | #> 97 | 98 | # Row Fore Color 99 | IF ($PSBoundParameters['RowForeColor']) 100 | { 101 | $dataGridView.Rows[$i].DefaultCellStyle.ForeColor = $RowForeColor 102 | } 103 | # Row Back Color 104 | IF ($PSBoundParameters['RowBackColor']) 105 | { 106 | $dataGridView.Rows[$i].DefaultCellStyle.BackColor = $RowBackColor 107 | } 108 | 109 | # Cell Selection 110 | ELSEIF (-not ($PSBoundParameters['SelectRow']) -and -not ($PSBoundParameters['SelectColumn'])) 111 | { 112 | $CurrentCell.Selected = $true 113 | } 114 | } #IF not empty and contains value 115 | } #For Each column 116 | } #For Each Row 117 | } #PROCESS 118 | } #Find-DataGridViewValue -------------------------------------------------------------------------------- /source/functions/Get-TSCurrentSession.ps1: -------------------------------------------------------------------------------- 1 | function Get-TSCurrentSession 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Provides information about the session in which the current process is running. 7 | 8 | .DESCRIPTION 9 | Provides information about the session in which the current process is running. 10 | 11 | .EXAMPLE 12 | Get-TSCurrentSession 13 | 14 | Description 15 | ----------- 16 | Displays the session in which the current process is running on the local computer. 17 | 18 | .OUTPUTS 19 | Cassia.Impl.TerminalServicesSession 20 | 21 | .COMPONENT 22 | TerminalServer 23 | 24 | .NOTES 25 | Author: Shay Levy 26 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 27 | 28 | .LINK 29 | http://code.msdn.microsoft.com/PSTerminalServices 30 | 31 | .LINK 32 | http://code.google.com/p/cassia/ 33 | 34 | .LINK 35 | Get-TSSession 36 | #> 37 | 38 | 39 | [OutputType('Cassia.Impl.TerminalServicesSession')] 40 | [CmdletBinding()] 41 | param ( 42 | [Parameter()] 43 | [Alias('CN', 'IPAddress')] 44 | [System.String]$ComputerName = $script:server 45 | ) 46 | 47 | 48 | try 49 | { 50 | $FuncName = $MyInvocation.MyCommand 51 | 52 | if (!$ComputerName) 53 | { 54 | Write-Verbose "[$funcName] ComputerName is not defined, loading global value '$script:Server'." 55 | $ComputerName = Get-TSGlobalServerName 56 | } 57 | else 58 | { 59 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 60 | } 61 | 62 | Write-Verbose "[$funcName] Attempting remote connection to '$ComputerName'" 63 | $TSManager = New-Object Cassia.TerminalServicesManager 64 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 65 | $TSRemoteServer.Open() 66 | 67 | if (!$TSRemoteServer.IsOpen) 68 | { 69 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 70 | } 71 | 72 | Write-Verbose "[$funcName] Connection is open '$ComputerName'" 73 | Write-Verbose "[$funcName] Updating global Server name '$ComputerName'" 74 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 75 | 76 | Write-Verbose "[$funcName] Get CurrentSession from '$ComputerName'" 77 | $TSManager.CurrentSession 78 | 79 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 80 | $TSRemoteServer.Close() 81 | $TSRemoteServer.Dispose() 82 | } 83 | catch 84 | { 85 | Throw 86 | } 87 | } -------------------------------------------------------------------------------- /source/functions/Get-TSGlobalServerName.ps1: -------------------------------------------------------------------------------- 1 | function Get-TSGlobalServerName 2 | { 3 | $script:Server 4 | } -------------------------------------------------------------------------------- /source/functions/Get-TSProcess.ps1: -------------------------------------------------------------------------------- 1 | function Get-TSProcess 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Gets a list of processes running in a specific session or in all sessions. 7 | 8 | .DESCRIPTION 9 | Use Get-TSProcess to get a list of session processes from a local or remote computers. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER Id 15 | Specifies the process Id number. 16 | 17 | .PARAMETER InputObject 18 | Specifies a process object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 19 | 20 | .PARAMETER Name 21 | Specifies the process name. Wildcards are permitted. 22 | 23 | .PARAMETER Session 24 | Specifies the session Id number. 25 | 26 | .EXAMPLE 27 | Get-TSProcess 28 | 29 | Description 30 | ----------- 31 | Gets all the sessions processes from the local computer. 32 | 33 | .EXAMPLE 34 | Get-TSSession -Id 0 -ComputerName comp1 | Get-TSProcess 35 | 36 | Description 37 | ----------- 38 | Gets all processes connected to session id 0 from remote computer 'comp1'. 39 | 40 | .EXAMPLE 41 | Get-TSProcess -Name s* -ComputerName comp1 42 | 43 | Description 44 | ----------- 45 | Gets all the processes with name starts with the letter 's' from remote computer 'comp1'. 46 | 47 | .OUTPUTS 48 | Cassia.Impl.TerminalServicesProcess 49 | 50 | .COMPONENT 51 | TerminalServer 52 | 53 | .NOTES 54 | Author: Shay Levy 55 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 56 | 57 | .LINK 58 | http://code.msdn.microsoft.com/PSTerminalServices 59 | 60 | .LINK 61 | http://code.google.com/p/cassia/ 62 | 63 | .LINK 64 | Get-TSSession 65 | Stop-TSProcess 66 | #> 67 | 68 | 69 | [OutputType('Cassia.Impl.TerminalServicesProcess')] 70 | [CmdletBinding(DefaultParameterSetName = 'Name')] 71 | Param ( 72 | 73 | [Parameter()] 74 | [Alias('CN', 'IPAddress')] 75 | [System.String]$ComputerName = $script:server, 76 | [Parameter( 77 | Position = 0, 78 | ValueFromPipelineByPropertyName = $true, 79 | ParameterSetName = 'Name' 80 | )] 81 | [Alias('ProcessName')] 82 | [System.String]$Name = '*', 83 | [Parameter( 84 | Mandatory = $true, 85 | ValueFromPipeline = $true, 86 | ValueFromPipelineByPropertyName = $true, 87 | ParameterSetName = 'Id' 88 | )] 89 | [Alias('ProcessID')] 90 | [ValidateRange(0, 65536)] 91 | [System.Int32]$Id = -1, 92 | [Parameter( 93 | Position = 0, 94 | Mandatory = $true, 95 | ValueFromPipeline = $true, 96 | ParameterSetName = 'InputObject' 97 | )] 98 | [Cassia.Impl.TerminalServicesProcess]$InputObject, 99 | [Parameter( 100 | Position = 0, 101 | Mandatory = $true, 102 | ValueFromPipeline = $true, 103 | ParameterSetName = 'Session' 104 | )] 105 | [Alias('SessionId')] 106 | [Cassia.Impl.TerminalServicesSession]$Session 107 | ) 108 | 109 | 110 | 111 | begin 112 | { 113 | $FuncName = $MyInvocation.MyCommand 114 | Write-Verbose "[$funcName] Entering Begin block." 115 | 116 | if (!$ComputerName) 117 | { 118 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 119 | $ComputerName = Get-TSGlobalServerName 120 | } 121 | else 122 | { 123 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 124 | } 125 | 126 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 127 | $TSManager = New-Object Cassia.TerminalServicesManager 128 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 129 | $TSRemoteServer.Open() 130 | 131 | if (!$TSRemoteServer.IsOpen) 132 | { 133 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 134 | } 135 | 136 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 137 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 138 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 139 | } 140 | 141 | 142 | 143 | Process 144 | { 145 | 146 | Write-Verbose "[$funcName] Entering Process block." 147 | 148 | try 149 | { 150 | 151 | if ($PSCmdlet.ParameterSetName -eq 'Name') 152 | { 153 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 154 | if ($Name -eq '*') 155 | { 156 | $proc = $TSRemoteServer.GetProcesses() 157 | } 158 | else 159 | { 160 | $proc = $TSRemoteServer.GetProcesses() | Where-Object { $_.ProcessName -like $Name } 161 | } 162 | } 163 | 164 | if ($PSCmdlet.ParameterSetName -eq 'Id') 165 | { 166 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 167 | if ($Id -lt 0) 168 | { 169 | $proc = $TSRemoteServer.GetProcesses() 170 | } 171 | else 172 | { 173 | $proc = $TSRemoteServer.GetProcess($Id) 174 | } 175 | } 176 | 177 | 178 | if ($PSCmdlet.ParameterSetName -eq 'Session') 179 | { 180 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 181 | if ($Session) 182 | { 183 | $proc = $Session.GetProcesses() 184 | } 185 | } 186 | 187 | 188 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 189 | { 190 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 191 | $proc = $InputObject 192 | } 193 | 194 | 195 | if ($proc) 196 | { 197 | $proc 198 | } 199 | } 200 | catch 201 | { 202 | Throw 203 | } 204 | } 205 | 206 | 207 | end 208 | { 209 | try 210 | { 211 | Write-Verbose "[$funcName] Entering End block." 212 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 213 | $TSRemoteServer.Close() 214 | $TSRemoteServer.Dispose() 215 | } 216 | catch 217 | { 218 | Throw 219 | } 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /source/functions/Get-TSServers.ps1: -------------------------------------------------------------------------------- 1 | function Get-TSServers 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Enumerates all terminal servers in a given domain. 7 | 8 | .DESCRIPTION 9 | Enumerates all terminal servers in a given domain. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER DomainName 15 | The name of the domain. The default is the caller domain name ($env:USERDOMAIN). 16 | 17 | .EXAMPLE 18 | Get-TSDomainServers 19 | 20 | Description 21 | ----------- 22 | Get a list of all terminal servers of the caller default domain. 23 | 24 | .OUTPUTS 25 | 26 | .COMPONENT 27 | TerminalServer 28 | 29 | .NOTES 30 | Author: Shay Levy 31 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 32 | 33 | .LINK 34 | http://code.msdn.microsoft.com/PSTerminalServices 35 | 36 | .LINK 37 | http://code.google.com/p/cassia/ 38 | 39 | .LINK 40 | Get-TSSession 41 | #> 42 | 43 | 44 | [OutputType('System.Management.Automation.PSCustomObject')] 45 | [CmdletBinding()] 46 | Param ( 47 | [Parameter( 48 | Position = 0, 49 | ParameterSetName = 'Name' 50 | )] 51 | [System.String]$DomainName = $env:USERDOMAIN 52 | ) 53 | 54 | 55 | try 56 | { 57 | $FuncName = $MyInvocation.MyCommand 58 | if (!$ComputerName) 59 | { 60 | Write-Verbose "[$funcName] ComputerName is not defined, loading global value '$script:Server'." 61 | $ComputerName = Get-TSGlobalServerName 62 | } 63 | else 64 | { 65 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 66 | } 67 | 68 | Write-Verbose "[$funcName] Enumerating terminal servers for '$DomainName' domain." 69 | Write-Warning 'Depending on your environment the command may take a while to complete.' 70 | $TSManager = New-Object Cassia.TerminalServicesManager 71 | $TSManager.GetServers($DomainName) 72 | } 73 | catch 74 | { 75 | Throw 76 | } 77 | 78 | } -------------------------------------------------------------------------------- /source/functions/Get-TSSession.ps1: -------------------------------------------------------------------------------- 1 | function Get-TSSession 2 | { 3 | <# 4 | .SYNOPSIS 5 | Lists the sessions on a given terminal server. 6 | 7 | .DESCRIPTION 8 | Use Get-TSSession to get a list of sessions from a local or remote computers. 9 | Note that Get-TSSession is using Aliased properties to display the output on the console (IPAddress and State), these attributes 10 | are not the same as the original attributes (ClientIPAddress and ConnectionState). 11 | This is important when you want to use the -Filter parameter which requires the latter. 12 | To see all aliassed properties and their corresponding properties (Definition column), pipe the result to Get-Member: 13 | 14 | PS > Get-TSSession | Get-Member -MemberType AliasProperty 15 | 16 | TypeName: Cassia.Impl.TerminalServicesSession 17 | 18 | Name MemberType Definition 19 | ---- ---------- ---------- 20 | (...) 21 | IPAddress AliasProperty IPAddress = ClientIPAddress 22 | State AliasProperty State = ConnectionState 23 | 24 | 25 | .PARAMETER ComputerName 26 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 27 | 28 | .PARAMETER Id 29 | Specifies the session Id number. 30 | 31 | .PARAMETER InputObject 32 | Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 33 | 34 | .PARAMETER Filter 35 | Specifies a filter based on the session properties. The syntax of the filter, including the use of 36 | wildcards and depends on the properties of the session. Internally, The Filter parameter uses client side 37 | filtering using the Where-Object cmdlet, objects are filtered after they are retrieved. 38 | 39 | .PARAMETER State 40 | The connection state of the session. Use this parameter to get sessions of a specific state. Valid values are: 41 | 42 | Value Description 43 | ----- ----------- 44 | Active A user is logged on to the session. 45 | ConnectQuery The session is in the process of connecting to a client. 46 | Connected A client is connected to the session). 47 | Disconnected The session is active, but the client has disconnected from it. 48 | Down The session is down due to an error. 49 | Idle The session is waiting for a client to connect. 50 | Initializing The session is initializing. 51 | Listening The session is listening for connections. 52 | Reset The session is being reset. 53 | Shadowing This session is shadowing another session. 54 | 55 | .PARAMETER ClientName 56 | The name of the machine last connected to a session. 57 | Use this parameter to get sessions made from a specific computer name. Wildcrads are permitted. 58 | 59 | .PARAMETER UserName 60 | Use this parameter to get sessions made by a specific user name. Wildcrads are permitted. 61 | 62 | .EXAMPLE 63 | Get-TSSession 64 | 65 | Description 66 | ----------- 67 | Gets all the sessions from the local computer. 68 | 69 | .EXAMPLE 70 | Get-TSSession -ComputerName comp1 -State Disconnected 71 | 72 | Description 73 | ----------- 74 | Gets all the disconnected sessions from the remote computer 'comp1'. 75 | 76 | .EXAMPLE 77 | Get-TSSession -ComputerName comp1 -Filter {$_.ClientIPAddress -like '10*' -AND $_.ConnectionState -eq 'Active'} 78 | 79 | Description 80 | ----------- 81 | Gets all Active sessions from remote computer 'comp1', made from ip addresses that starts with '10'. 82 | 83 | .EXAMPLE 84 | Get-TSSession -ComputerName comp1 -UserName a* 85 | 86 | Description 87 | ----------- 88 | Gets all sessions from remote computer 'comp1' made by users with name starts with the letter 'a'. 89 | 90 | .EXAMPLE 91 | Get-TSSession -ComputerName comp1 -ClientName s* 92 | 93 | Description 94 | ----------- 95 | Gets all sessions from remote computer 'comp1' made from a computers names that starts with the letter 's'. 96 | 97 | .OUTPUTS 98 | Cassia.Impl.TerminalServicesSession 99 | 100 | .COMPONENT 101 | TerminalServer 102 | 103 | .NOTES 104 | Author: Shay Levy 105 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 106 | 107 | .LINK 108 | http://code.msdn.microsoft.com/PSTerminalServices 109 | 110 | .LINK 111 | http://code.google.com/p/cassia/ 112 | 113 | .LINK 114 | Stop-TSSession 115 | Disconnect-TSSession 116 | Send-TSMessage 117 | #> 118 | 119 | 120 | [OutputType('Cassia.Impl.TerminalServicesSession')] 121 | [CmdletBinding(DefaultParameterSetName = 'Session')] 122 | Param ( 123 | 124 | [Parameter()] 125 | [Alias('CN', 'IPAddress')] 126 | [System.String]$ComputerName, 127 | [Parameter( 128 | Position = 0, 129 | ValueFromPipelineByPropertyName = $true, 130 | ParameterSetName = 'Session' 131 | )] 132 | [Alias('SessionID')] 133 | [ValidateRange(0, 65536)] 134 | [System.Int32]$Id = -1, 135 | [Parameter( 136 | Position = 0, 137 | Mandatory = $true, 138 | ValueFromPipeline = $true, 139 | ParameterSetName = 'InputObject' 140 | )] 141 | [Cassia.Impl.TerminalServicesSession]$InputObject, 142 | [Parameter( 143 | Mandatory = $true, 144 | ParameterSetName = 'Filter' 145 | )] 146 | [ScriptBlock]$Filter, 147 | [Parameter()] 148 | [ValidateSet('Active', 'Connected', 'ConnectQuery', 'Shadowing', 'Disconnected', 'Idle', 'Listening', 'Reset', 'Down', 'Initializing')] 149 | [Alias('ConnectionState')] 150 | [System.String]$State = '*', 151 | [Parameter()] 152 | [System.String]$ClientName = '*', 153 | [Parameter()] 154 | [System.String]$UserName = '*' 155 | ) 156 | 157 | 158 | begin 159 | { 160 | try 161 | { 162 | $FuncName = $MyInvocation.MyCommand 163 | Write-Verbose "[$funcName] Entering Begin block." 164 | 165 | if (!$ComputerName) 166 | { 167 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 168 | $ComputerName = Get-TSGlobalServerName 169 | } 170 | else 171 | { 172 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 173 | } 174 | 175 | 176 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 177 | $TSManager = New-Object Cassia.TerminalServicesManager 178 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 179 | $TSRemoteServer.Open() 180 | 181 | if (!$TSRemoteServer.IsOpen) 182 | { 183 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 184 | } 185 | 186 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 187 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 188 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 189 | } 190 | catch 191 | { 192 | Throw 193 | } 194 | } 195 | 196 | 197 | Process 198 | { 199 | 200 | Write-Verbose "[$funcName] Entering Process block." 201 | 202 | try 203 | { 204 | if ($PSCmdlet.ParameterSetName -eq 'Session') 205 | { 206 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 207 | if ($Id -lt 0) 208 | { 209 | $session = $TSRemoteServer.GetSessions() 210 | } 211 | else 212 | { 213 | $session = $TSRemoteServer.GetSession($Id) 214 | } 215 | } 216 | 217 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 218 | { 219 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 220 | $session = $InputObject 221 | } 222 | 223 | if ($PSCmdlet.ParameterSetName -eq 'Filter') 224 | { 225 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 226 | 227 | $TSRemoteServer.GetSessions() | Where-Object $Filter 228 | } 229 | 230 | if ($session) 231 | { 232 | $session | Where-Object { $_.ConnectionState -like $State -AND $_.UserName -like $UserName -AND $_.ClientName -like $ClientName } | ` 233 | Add-Member -MemberType AliasProperty -Name IPAddress -Value ClientIPAddress -PassThru | ` 234 | Add-Member -MemberType AliasProperty -Name State -Value ConnectionState -PassThru 235 | } 236 | } 237 | catch 238 | { 239 | Throw 240 | } 241 | } 242 | 243 | end 244 | { 245 | try 246 | { 247 | Write-Verbose "[$funcName] Entering End block." 248 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 249 | $TSRemoteServer.Close() 250 | $TSRemoteServer.Dispose() 251 | } 252 | catch 253 | { 254 | Throw 255 | } 256 | } 257 | } -------------------------------------------------------------------------------- /source/functions/Load-DataGridView.ps1: -------------------------------------------------------------------------------- 1 | function Load-DataGridView 2 | { 3 | <# 4 | .SYNOPSIS 5 | This functions helps you load items into a DataGridView. 6 | 7 | .DESCRIPTION 8 | Use this function to dynamically load items into the DataGridView control. 9 | 10 | .PARAMETER DataGridView 11 | The ComboBox control you want to add items to. 12 | 13 | .PARAMETER Item 14 | The object or objects you wish to load into the ComboBox's items collection. 15 | 16 | .PARAMETER DataMember 17 | Sets the name of the list or table in the data source for which the DataGridView is displaying data. 18 | #> 19 | 20 | [CmdletBinding()] 21 | Param ( 22 | [ValidateNotNull()] 23 | [Parameter(Mandatory = $true)] 24 | [System.Windows.Forms.DataGridView]$DataGridView, 25 | [ValidateNotNull()] 26 | [Parameter(Mandatory = $true)] 27 | $Item, 28 | [Parameter(Mandatory = $false)] 29 | [string]$DataMember 30 | ) 31 | $DataGridView.SuspendLayout() 32 | $DataGridView.DataMember = $DataMember 33 | 34 | if ($Item -is [System.ComponentModel.IListSource]` 35 | -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView]) 36 | { 37 | $DataGridView.DataSource = $Item 38 | } 39 | else 40 | { 41 | $array = New-Object System.Collections.ArrayList 42 | 43 | if ($Item -is [System.Collections.IList]) 44 | { 45 | $array.AddRange($Item) 46 | } 47 | else 48 | { 49 | $array.Add($Item) 50 | } 51 | $DataGridView.DataSource = $array 52 | } 53 | 54 | $DataGridView.ResumeLayout() 55 | } -------------------------------------------------------------------------------- /source/functions/New-MessageBox.ps1: -------------------------------------------------------------------------------- 1 | function New-MessageBox 2 | { 3 | <# 4 | .SYNOPSIS 5 | The New-MessageBox functio will show a message box to the user 6 | 7 | .DESCRIPTION 8 | The New-MessageBox functio will show a message box to the user 9 | 10 | .PARAMETER Message 11 | Specifies the message to show 12 | 13 | .PARAMETER Title 14 | Specifies the title of the message box 15 | 16 | .PARAMETER Buttons 17 | Specifies which button to add. Just press tab to see the choices 18 | 19 | .PARAMETER Icon 20 | Specifies the icon to show. Just press tab to see the choices 21 | 22 | .EXAMPLE 23 | PS C:\> New-MessageBox -Message "Hello World" -Title "First Message" -Buttons "RetryCancel" -Icon "Asterix" 24 | 25 | .NOTES 26 | Author: Francois-Xavier Cat 27 | Twitter:@LazyWinAdm 28 | WWW: lazywinadmin.com 29 | #> 30 | [CmdletBinding()] 31 | PARAM ( 32 | 33 | [String]$Message, 34 | [String]$Title, 35 | [System.Windows.Forms.MessageBoxButtons]$Buttons = "OK", 36 | [System.Windows.Forms.MessageBoxIcon]$Icon = "None" 37 | ) 38 | BEGIN 39 | { 40 | Add-Type -AssemblyName System.Windows.Forms 41 | } 42 | PROCESS 43 | { 44 | [System.Windows.Forms.MessageBox]::Show($Message, $Title, $Buttons, $Icon) 45 | } 46 | } #New-MessageBox -------------------------------------------------------------------------------- /source/functions/Reset-DataGridViewFormat.ps1: -------------------------------------------------------------------------------- 1 | function Reset-DataGridViewFormat 2 | { 3 | <# 4 | .SYNOPSIS 5 | The Reset-DataGridViewFormat function will reset the format of a datagridview control 6 | 7 | .DESCRIPTION 8 | The Reset-DataGridViewFormat function will reset the format of a datagridview control 9 | 10 | .PARAMETER DataGridView 11 | Specifies the DataGridView Control. 12 | 13 | .EXAMPLE 14 | PS C:\> Reset-DataGridViewFormat -DataGridView $DataGridViewObj 15 | 16 | .NOTES 17 | Author: Francois-Xavier Cat 18 | Twitter:@LazyWinAdm 19 | WWW: lazywinadmin.com 20 | #> 21 | [CmdletBinding()] 22 | PARAM ( 23 | [Parameter(Mandatory = $true)] 24 | [System.Windows.Forms.DataGridView]$DataGridView) 25 | PROCESS 26 | { 27 | $DataSource = $DataGridView.DataSource 28 | $DataGridView.DataSource = $null 29 | $DataGridView.DataSource = $DataSource 30 | 31 | #$DataGridView.RowsDefaultCellStyle.BackColor = 'White' 32 | #$DataGridView.RowsDefaultCellStyle.ForeColor = 'Black' 33 | $DataGridView.RowsDefaultCellStyle = $null 34 | $DataGridView.AlternatingRowsDefaultCellStyle = $null 35 | } 36 | } #Reset-DataGridViewFormat -------------------------------------------------------------------------------- /source/functions/Reset-TextBox.ps1: -------------------------------------------------------------------------------- 1 | function Reset-TextBox 2 | { 3 | [CmdletBinding()] 4 | PARAM ( 5 | [System.Windows.Forms.TextBox]$TextBox, 6 | [System.Drawing.Color]$BackColor = "White", 7 | [System.Drawing.Color]$ForeColor = "Black" 8 | ) 9 | BEGIN { } 10 | PROCESS 11 | { 12 | TRY 13 | { 14 | $TextBox.Text = "" 15 | $TextBox.BackColor = $BackColor 16 | $TextBox.ForeColor = $ForeColor 17 | } 18 | CATCH { } 19 | } 20 | } -------------------------------------------------------------------------------- /source/functions/Send-TSMessage.ps1: -------------------------------------------------------------------------------- 1 | function Send-TSMessage 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Displays a message box in the specified session Id. 7 | 8 | .DESCRIPTION 9 | Use Send-TSMessage display a message box in the specified session Id. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER Text 15 | The text to display in the message box. 16 | 17 | .PARAMETER SessionID 18 | The number of the session Id. 19 | 20 | .PARAMETER Caption 21 | The caption of the message box. The default caption is 'Alert'. 22 | 23 | .EXAMPLE 24 | $Message = "Importnat message`n, the server is going down for maintanace in 10 minutes. Please save your work and logoff." 25 | Get-TSSession -State Active -ComputerName comp1 | Send-TSMessage -Message $Message 26 | 27 | Description 28 | ----------- 29 | Displays a message box inside all active sessions of computer name 'comp1'. 30 | 31 | .OUTPUTS 32 | 33 | .COMPONENT 34 | TerminalServer 35 | 36 | .NOTES 37 | Author: Shay Levy 38 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 39 | 40 | .LINK 41 | http://code.msdn.microsoft.com/PSTerminalServices 42 | 43 | .LINK 44 | http://code.google.com/p/cassia/ 45 | 46 | .LINK 47 | Get-TSSession 48 | #> 49 | 50 | 51 | [CmdletBinding(DefaultParameterSetName = 'Session')] 52 | Param ( 53 | [Parameter()] 54 | [Alias('CN', 'IPAddress')] 55 | [System.String]$ComputerName = $script:server, 56 | [Parameter( 57 | Position = 0, 58 | Mandatory = $true, 59 | HelpMessage = 'The text to display in the message box.' 60 | )] 61 | [System.String]$Text, 62 | [Parameter( 63 | HelpMessage = 'The caption of the message box.' 64 | )] 65 | [ValidateNotNullOrEmpty()] 66 | [System.String]$Caption = 'Alert', 67 | [Parameter( 68 | Position = 0, 69 | ValueFromPipelineByPropertyName = $true, 70 | ParameterSetName = 'Session' 71 | )] 72 | [Alias('SessionID')] 73 | [ValidateRange(0, 65536)] 74 | [System.Int32]$Id = -1, 75 | [Parameter( 76 | Position = 0, 77 | Mandatory = $true, 78 | ValueFromPipeline = $true, 79 | ParameterSetName = 'InputObject' 80 | )] 81 | [Cassia.Impl.TerminalServicesSession]$InputObject 82 | ) 83 | 84 | begin 85 | { 86 | try 87 | { 88 | $FuncName = $MyInvocation.MyCommand 89 | Write-Verbose "[$funcName] Entering Begin block." 90 | 91 | if (!$ComputerName) 92 | { 93 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 94 | $ComputerName = Get-TSGlobalServerName 95 | } 96 | else 97 | { 98 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 99 | } 100 | 101 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 102 | $TSManager = New-Object Cassia.TerminalServicesManager 103 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 104 | $TSRemoteServer.Open() 105 | 106 | if (!$TSRemoteServer.IsOpen) 107 | { 108 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 109 | } 110 | 111 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 112 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 113 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 114 | } 115 | catch 116 | { 117 | Throw 118 | } 119 | } 120 | 121 | 122 | process 123 | { 124 | 125 | Write-Verbose "[$funcName] Entering Process block." 126 | 127 | try 128 | { 129 | 130 | if ($PSCmdlet.ParameterSetName -eq 'Session') 131 | { 132 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 133 | if ($Id -ge 0) 134 | { 135 | $session = $TSRemoteServer.GetSession($Id) 136 | } 137 | } 138 | 139 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 140 | { 141 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 142 | $session = $InputObject 143 | } 144 | 145 | if ($session) 146 | { 147 | Write-Verbose "[$FuncName] Sending alert message to session id: '$($session.SessionId)' on '$ComputerName'" 148 | $session.MessageBox($Text, $Caption) 149 | } 150 | } 151 | catch 152 | { 153 | Throw 154 | } 155 | } 156 | 157 | 158 | end 159 | { 160 | try 161 | { 162 | Write-Verbose "[$funcName] Entering End block." 163 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 164 | $TSRemoteServer.Close() 165 | $TSRemoteServer.Dispose() 166 | } 167 | catch 168 | { 169 | Throw 170 | } 171 | } 172 | } -------------------------------------------------------------------------------- /source/functions/Set-DataGridView.ps1: -------------------------------------------------------------------------------- 1 | function Set-DataGridView 2 | { 3 | <# 4 | .SYNOPSIS 5 | This function helps you edit the datagridview control 6 | 7 | .DESCRIPTION 8 | This function helps you edit the datagridview control 9 | 10 | .EXAMPLE 11 | Set-DataGridView -DataGridView $datagridview1 -ProperFormat -FontFamily $listboxFontFamily.Text -FontSize $listboxFontSize.Text 12 | 13 | .EXAMPLE 14 | Set-DataGridView -DataGridView $datagridview1 -AlternativeRowColor -BackColor 'AliceBlue' -ForeColor 'Black' 15 | 16 | .EXAMPLE 17 | Set-DataGridViewRowHeader -DataGridView $datagridview1 -HideRowHeader 18 | 19 | .EXAMPLE 20 | Set-DataGridViewRowHeader -DataGridView $datagridview1 -ShowRowHeader 21 | 22 | .NOTES 23 | Author: Francois-Xavier Cat 24 | Twitter:@LazyWinAdm 25 | WWW: lazywinadmin.com 26 | #> 27 | 28 | [CmdletBinding()] 29 | PARAM ( 30 | [ValidateNotNull()] 31 | [Parameter(Mandatory = $true)] 32 | [System.Windows.Forms.DataGridView]$DataGridView, 33 | [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 34 | [Switch]$AlternativeRowColor, 35 | [Parameter(ParameterSetName = "DefaultRowColor")] 36 | [Switch]$DefaultRowColor, 37 | [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 38 | [Parameter(ParameterSetName = "DefaultRowColor")] 39 | [System.Drawing.Color]$ForeColor, 40 | [Parameter(Mandatory = $true, ParameterSetName = "AlternativeRowColor")] 41 | [Parameter(ParameterSetName = "DefaultRowColor")] 42 | [System.Drawing.Color]$BackColor, 43 | [Parameter(Mandatory = $true, ParameterSetName = "Proper")] 44 | [Switch]$ProperFormat, 45 | [Parameter(ParameterSetName = "Proper")] 46 | [String]$FontFamily = "Consolas", 47 | [Parameter(ParameterSetName = "Proper")] 48 | [Int]$FontSize = 10, 49 | [Parameter(ParameterSetName = "HideRowHeader")] 50 | [Switch]$HideRowHeader, 51 | [Parameter(ParameterSetName = "ShowRowHeader")] 52 | [Switch]$ShowRowHeader 53 | ) 54 | PROCESS 55 | { 56 | if ($psboundparameters['AlternativeRowColor']) 57 | { 58 | $DataGridView.AlternatingRowsDefaultCellStyle.ForeColor = $ForeColor 59 | $DataGridView.AlternatingRowsDefaultCellStyle.BackColor = $BackColor 60 | } 61 | 62 | if ($psboundparameters['DefaultRowColor']) 63 | { 64 | $DataGridView.RowsDefaultCellStyle.ForeColor = $ForeColor 65 | $DataGridView.RowsDefaultCellStyle.BackColor = $BackColor 66 | } 67 | 68 | 69 | if ($psboundparameters['ProperFormat']) 70 | { 71 | #$Font = New-Object -TypeName System.Drawing.Font -ArgumentList "Segoi UI", 10 72 | $Font = New-Object -TypeName System.Drawing.Font -ArgumentList $FontFamily, $FontSize 73 | 74 | #[System.Drawing.FontStyle]::Bold 75 | 76 | $DataGridView.ColumnHeadersBorderStyle = 'Raised' 77 | $DataGridView.BorderStyle = 'Fixed3D' 78 | $DataGridView.SelectionMode = 'FullRowSelect' 79 | $DataGridView.AllowUserToResizeRows = $false 80 | $datagridview.DefaultCellStyle.font = $Font 81 | } 82 | 83 | if ($psboundparameters['HideRowHeader']) 84 | { 85 | $DataGridView.RowHeadersVisible = $false 86 | } 87 | if ($psboundparameters['ShowRowHeader']) 88 | { 89 | $DataGridView.RowHeadersVisible = $true 90 | } 91 | } 92 | 93 | } #Set-DataGridView -------------------------------------------------------------------------------- /source/functions/Set-DataGridViewFilter.ps1: -------------------------------------------------------------------------------- 1 | function Set-DataGridViewFilter 2 | { 3 | <# 4 | .SYNOPSIS 5 | The function Set-DataGridViewFilter helps to only show specific entries with a specific value 6 | 7 | .DESCRIPTION 8 | The function Set-DataGridViewFilter helps to only show specific entries with a specific value. 9 | The data needs to be in a DataTable Object. You can use ConvertTo-DataTable to convert your 10 | PowerShell object into a DataTable object. 11 | 12 | .PARAMETER AllColumns 13 | Specifies to search all the column 14 | 15 | .PARAMETER ColumnName 16 | Specifies to search in a specific column name 17 | 18 | .PARAMETER DataGridView 19 | Specifies the DataGridView control where the data will be filtered 20 | 21 | .PARAMETER DataTable 22 | Specifies the DataTable object that is most likely the original source of the DataGridView 23 | 24 | .PARAMETER Filter 25 | Specifies the string to search 26 | 27 | .EXAMPLE 28 | PS C:\> Set-DataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -AllColumns -Filter $textbox1.Text 29 | 30 | .EXAMPLE 31 | PS C:\> Set-DataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -ColumnName "Name" -Filter $textbox1.Text 32 | 33 | .NOTES 34 | Author: Francois-Xavier Cat 35 | Twitter:@LazyWinAdm 36 | WWW: lazywinadmin.com 37 | #> 38 | PARAM ( 39 | [Parameter(Mandatory = $true)] 40 | [System.Windows.Forms.DataGridView]$DataGridView, 41 | [Parameter(Mandatory = $true)] 42 | [System.Data.DataTable]$DataTable, 43 | [Parameter(Mandatory = $true)] 44 | [String]$Filter, 45 | [Parameter(Mandatory = $true, ParameterSetName = "OneColumn")] 46 | [String]$ColumnName, 47 | [Parameter(Mandatory = $true, ParameterSetName = "AllColumns")] 48 | [Switch]$AllColumns 49 | ) 50 | PROCESS 51 | { 52 | $Filter = $Filter.ToString() 53 | 54 | IF ($PSBoundParameters['AllColumns']) 55 | { 56 | FOREACH ($Column in $DataTable.Columns) 57 | { 58 | #$RowFilter += "Convert("+$($Column.ColumnName)+",'system.string') Like '%"{1}%' OR " -f $Column.ColumnName, $Filter 59 | $RowFilter += "Convert($($Column.ColumnName),'system.string') Like '%$Filter%' OR " 60 | } 61 | 62 | # Remove the last 'OR' 63 | $RowFilter = $RowFilter -replace " OR $", '' 64 | 65 | #Append-RichtextboxStatus -Message $RowFilter 66 | } 67 | IF ($PSBoundParameters['ColumnName']) 68 | { 69 | $RowFilter = "$ColumnName LIKE '%$Filter%'" 70 | } 71 | 72 | $DataTable.defaultview.rowfilter = $RowFilter 73 | Load-DataGridView -DataGridView $DataGridView -Item $DataTable 74 | } 75 | END { Remove-Variable -Name $RowFilter -ErrorAction 'SilentlyContinue' | Out-Null } 76 | } #Set-DataGridViewFilter -------------------------------------------------------------------------------- /source/functions/Set-TSGlobalServerName.ps1: -------------------------------------------------------------------------------- 1 | function Set-TSGlobalServerName 2 | { 3 | [CmdletBinding()] 4 | Param ( 5 | [Parameter(Mandatory = $true)] 6 | [ValidateNotNullOrEmpty()] 7 | [System.String]$ComputerName 8 | ) 9 | 10 | if ($ComputerName -eq "." -OR $ComputerName -eq $env:COMPUTERNAME) 11 | { 12 | $ComputerName = 'localhost' 13 | } 14 | 15 | $script:Server = $ComputerName 16 | $script:Server 17 | } 18 | -------------------------------------------------------------------------------- /source/functions/Set-TextBox.ps1: -------------------------------------------------------------------------------- 1 | function Set-TextBox 2 | { 3 | [CmdletBinding()] 4 | PARAM ( 5 | [System.Windows.Forms.TextBox]$TextBox, 6 | [System.Drawing.Color]$BackColor 7 | ) 8 | BEGIN { } 9 | PROCESS 10 | { 11 | TRY 12 | { 13 | $TextBox.BackColor = $BackColor 14 | } 15 | CATCH { } 16 | } 17 | } -------------------------------------------------------------------------------- /source/functions/Stop-TSProcess.ps1: -------------------------------------------------------------------------------- 1 | function Stop-TSProcess 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Terminates the process running in a specific session or in all sessions. 7 | 8 | .DESCRIPTION 9 | Use Stop-TSProcess to terminate one or more processes from a local or remote computers. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER Id 15 | Specifies the process Id number. 16 | 17 | .PARAMETER InputObject 18 | Specifies a process object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 19 | 20 | .PARAMETER Name 21 | Specifies the process name. 22 | 23 | .PARAMETER Session 24 | Specifies the session Id number. 25 | 26 | .PARAMETER Force 27 | Overrides any confirmations made by the command. 28 | 29 | .EXAMPLE 30 | Get-TSProcess -Id 6552 | Stop-TSProcess 31 | 32 | Description 33 | ----------- 34 | Gets process Id 6552 from the local computer and stop it. Confirmations needed. 35 | 36 | .EXAMPLE 37 | Get-TSSession -Id 3 -ComputerName comp1 | Stop-TSProcess -Force 38 | 39 | Description 40 | ----------- 41 | Terminats all processes connected to session id 3 from remote computer 'comp1', suppress confirmations. 42 | 43 | .OUTPUTS 44 | Cassia.Impl.TerminalServicesProcess 45 | 46 | .COMPONENT 47 | TerminalServer 48 | 49 | .NOTES 50 | Author: Shay Levy 51 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 52 | 53 | .LINK 54 | http://code.msdn.microsoft.com/PSTerminalServices 55 | 56 | .LINK 57 | http://code.google.com/p/cassia/ 58 | 59 | .LINK 60 | Get-TSProcess 61 | Get-TSSession 62 | #> 63 | 64 | [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High', DefaultParameterSetName = 'Name')] 65 | Param ( 66 | [Parameter()] 67 | [Alias('CN', 'IPAddress')] 68 | [System.String]$ComputerName = $script:server, 69 | [Parameter( 70 | Position = 0, 71 | ValueFromPipelineByPropertyName = $true, 72 | ParameterSetName = 'Name' 73 | )] 74 | [Alias("ProcessName")] 75 | [System.String]$Name = '*', 76 | [Parameter( 77 | Mandatory = $true, 78 | ValueFromPipeline = $true, 79 | ValueFromPipelineByPropertyName = $true, 80 | ParameterSetName = 'Id' 81 | )] 82 | [Alias('ProcessID')] 83 | [ValidateRange(0, 65536)] 84 | [System.Int32]$Id = -1, 85 | [Parameter( 86 | Position = 0, 87 | Mandatory = $true, 88 | ValueFromPipeline = $true, 89 | ParameterSetName = 'InputObject' 90 | )] 91 | [Cassia.Impl.TerminalServicesProcess]$InputObject, 92 | [Parameter( 93 | Position = 0, 94 | Mandatory = $true, 95 | ValueFromPipeline = $true, 96 | ParameterSetName = 'Session' 97 | )] 98 | [Alias('SessionId')] 99 | [Cassia.Impl.TerminalServicesSession]$Session, 100 | [switch]$Force 101 | ) 102 | 103 | 104 | begin 105 | { 106 | try 107 | { 108 | $FuncName = $MyInvocation.MyCommand 109 | Write-Verbose "[$funcName] Entering Begin block." 110 | 111 | if (!$ComputerName) 112 | { 113 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 114 | $ComputerName = Get-TSGlobalServerName 115 | } 116 | else 117 | { 118 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 119 | } 120 | 121 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 122 | $TSManager = New-Object Cassia.TerminalServicesManager 123 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 124 | $TSRemoteServer.Open() 125 | 126 | if (!$TSRemoteServer.IsOpen) 127 | { 128 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 129 | } 130 | 131 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 132 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 133 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 134 | } 135 | catch 136 | { 137 | Throw 138 | } 139 | } 140 | 141 | 142 | Process 143 | { 144 | 145 | Write-Verbose "[$funcName] Entering Process block." 146 | 147 | try 148 | { 149 | 150 | if ($PSCmdlet.ParameterSetName -eq 'Name') 151 | { 152 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 153 | if ($Name -eq '*') 154 | { 155 | $proc = $TSRemoteServer.GetProcesses() 156 | } 157 | else 158 | { 159 | $proc = $TSRemoteServer.GetProcesses() | Where-Object { $_.ProcessName -like $Name } 160 | } 161 | } 162 | 163 | if ($PSCmdlet.ParameterSetName -eq 'Id') 164 | { 165 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 166 | if ($Id -lt 0) 167 | { 168 | $proc = $TSRemoteServer.GetProcesses() 169 | } 170 | else 171 | { 172 | $proc = $TSRemoteServer.GetProcess($Id) 173 | } 174 | } 175 | 176 | 177 | if ($PSCmdlet.ParameterSetName -eq 'Session') 178 | { 179 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 180 | if ($Session) 181 | { 182 | $proc = $Session.GetProcesses() 183 | } 184 | } 185 | 186 | 187 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 188 | { 189 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 190 | $proc = $InputObject 191 | } 192 | 193 | 194 | if ($proc) 195 | { 196 | foreach ($p in $proc) 197 | { 198 | if ($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName, "Stop Process '$($p.ProcessName) ($($p.ProcessID))")) 199 | { 200 | Write-Verbose "[$FuncName] Killing process '$($p.ProcessName)' ($($p.ProcessId))" 201 | $p.Kill() 202 | } 203 | } 204 | } 205 | } 206 | catch 207 | { 208 | Throw 209 | } 210 | } 211 | 212 | 213 | end 214 | { 215 | try 216 | { 217 | Write-Verbose "[$funcName] Entering End block." 218 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 219 | $TSRemoteServer.Close() 220 | $TSRemoteServer.Dispose() 221 | } 222 | catch 223 | { 224 | Throw 225 | } 226 | } 227 | } -------------------------------------------------------------------------------- /source/functions/Stop-TSSession.ps1: -------------------------------------------------------------------------------- 1 | function Stop-TSSession 2 | { 3 | 4 | <# 5 | .SYNOPSIS 6 | Logs the session off, disconnecting any user that might be connected. 7 | 8 | .DESCRIPTION 9 | Use Stop-TSSession to logoff the session and disconnect any user that might be connected. 10 | 11 | .PARAMETER ComputerName 12 | The name of the terminal server computer. The default is the local computer. Default value is the local computer (localhost). 13 | 14 | .PARAMETER Id 15 | Specifies the session Id number. 16 | 17 | .PARAMETER InputObject 18 | Specifies a session object. Enter a variable that contains the object, or type a command or expression that gets the sessions. 19 | 20 | .PARAMETER Synchronous 21 | When the Synchronous parameter is present the command waits until the session is fully disconnected otherwise it returns 22 | immediately, even though the session may not be completely disconnected yet. 23 | 24 | .PARAMETER Force 25 | Overrides any confirmations made by the command. 26 | 27 | .EXAMPLE 28 | Get-TSSession -ComputerName comp1 | Stop-TSSession 29 | 30 | Description 31 | ----------- 32 | logs off all connected users from Active sessions on remote computer 'comp1'. The caller is prompted to 33 | By default, the caller is prompted to confirm each action. 34 | 35 | .EXAMPLE 36 | Get-TSSession -ComputerName comp1 -State Active | Stop-TSSession -Force 37 | 38 | Description 39 | ----------- 40 | logs off any connected user from Active sessions on remote computer 'comp1'. 41 | By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 42 | 43 | .EXAMPLE 44 | Get-TSSession -ComputerName comp1 -State Active -Synchronous | Stop-TSSession -Force 45 | 46 | Description 47 | ----------- 48 | logs off any connected user from Active sessions on remote computer 'comp1'. The Synchronous parameter tells the command to 49 | wait until the session is fully disconnected and only tghen it proceeds to the next session object. 50 | By default, the caller is prompted to confirm each action. To override confirmations, the Force Switch parameter is specified. 51 | 52 | .OUTPUTS 53 | 54 | .COMPONENT 55 | TerminalServer 56 | 57 | .NOTES 58 | Author: Shay Levy 59 | Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/ 60 | 61 | .LINK 62 | http://code.msdn.microsoft.com/PSTerminalServices 63 | 64 | .LINK 65 | http://code.google.com/p/cassia/ 66 | 67 | .LINK 68 | Get-TSSession 69 | Disconnect-TSSession 70 | Send-TSMessage 71 | #> 72 | 73 | [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High', DefaultParameterSetName = 'Id')] 74 | Param ( 75 | 76 | [Parameter()] 77 | [Alias('CN', 'IPAddress')] 78 | [System.String]$ComputerName = $script:server, 79 | [Parameter( 80 | Position = 0, 81 | Mandatory = $true, 82 | ParameterSetName = 'Id', 83 | ValueFromPipelineByPropertyName = $true 84 | )] 85 | [Alias('SessionId')] 86 | [System.Int32]$Id, 87 | [Parameter( 88 | Mandatory = $true, 89 | ValueFromPipeline = $true, 90 | ParameterSetName = 'InputObject' 91 | )] 92 | [Cassia.Impl.TerminalServicesSession]$InputObject, 93 | [switch]$Synchronous, 94 | [switch]$Force 95 | ) 96 | 97 | begin 98 | { 99 | try 100 | { 101 | $FuncName = $MyInvocation.MyCommand 102 | Write-Verbose "[$funcName] Entering Begin block." 103 | 104 | if (!$ComputerName) 105 | { 106 | Write-Verbose "[$funcName] $ComputerName is not defined, loading global value '$script:Server'." 107 | $ComputerName = Get-TSGlobalServerName 108 | } 109 | else 110 | { 111 | $ComputerName = Set-TSGlobalServerName -ComputerName $ComputerName 112 | } 113 | 114 | Write-Verbose "[$FuncName] Attempting remote connection to '$ComputerName'" 115 | $TSManager = New-Object Cassia.TerminalServicesManager 116 | $TSRemoteServer = $TSManager.GetRemoteServer($ComputerName) 117 | $TSRemoteServer.Open() 118 | 119 | if (!$TSRemoteServer.IsOpen) 120 | { 121 | Throw 'Connection to remote server is not open. Use Connect-TSServer to connect first.' 122 | } 123 | 124 | Write-Verbose "[$FuncName] Connection is open '$ComputerName'" 125 | Write-Verbose "[$FuncName] Updating global Server name '$ComputerName'" 126 | $null = Set-TSGlobalServerName -ComputerName $ComputerName 127 | } 128 | catch 129 | { 130 | Throw 131 | } 132 | } 133 | 134 | 135 | 136 | Process 137 | { 138 | 139 | Write-Verbose "[$funcName] Entering Process block." 140 | 141 | try 142 | { 143 | 144 | if ($PSCmdlet.ParameterSetName -eq 'Id') 145 | { 146 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 147 | $session = $TSRemoteServer.GetSession($Id) 148 | } 149 | 150 | if ($PSCmdlet.ParameterSetName -eq 'InputObject') 151 | { 152 | Write-Verbose "[$FuncName] Binding to ParameterSetName '$($PSCmdlet.ParameterSetName)'" 153 | $session = $InputObject 154 | } 155 | 156 | if ($session -ne $null) 157 | { 158 | if ($Force -or $PSCmdlet.ShouldProcess($TSRemoteServer.ServerName, "Logging off session id '$($session.sessionId)'")) 159 | { 160 | Write-Verbose "[$FuncName] Logging off session '$($session.SessionId)'" 161 | $session.Logoff($Synchronous) 162 | } 163 | } 164 | } 165 | catch 166 | { 167 | Throw 168 | } 169 | } 170 | 171 | 172 | end 173 | { 174 | try 175 | { 176 | Write-Verbose "[$funcName] Entering End block." 177 | Write-Verbose "[$funcName] Disconnecting from remote server '$($TSRemoteServer.ServerName)'" 178 | $TSRemoteServer.Close() 179 | $TSRemoteServer.Dispose() 180 | } 181 | catch 182 | { 183 | Throw 184 | } 185 | } 186 | } --------------------------------------------------------------------------------