Archive | April, 2012

PowerShell: Remotely Launching Processes

Unfortunately, the Start-Process cmdlet has no -ComputerName parameter so you cannot use it to launch processes on remote machines. Use WMI instead. This line will run calc.exe on your local machine: PS> (Invoke-WmiMethod Win32_Process Create calc.exe).ReturnValue -eq 0 True And this slight adaption will run calc.exe on a computer named Server1 with Administrator credentials (adjust […]

Continue Reading →

PowerShell: Logging Off

Stop-Computer and Restart-Computer can shutdown and restart a machine, but there are things they cannot do, for example logging off the current user. Here’s a simple function wrapper that utilizes a standard console application and wraps it into a PowerShell function: function Invoke-Logoff { shutdown.exe /L }

Continue Reading →

PowerShell: Locking Workstation

If you ever feel the need to lock your interactive session via PowerShell, here’s a function that can do this (and also illustrates how to use low-level API functionality): function Lock-WorkStation { $signature = @” [DllImport(“user32.dll”, SetLastError = true)] public static extern bool LockWorkStation(); “@ $LockWorkStation = Add-Type -MemberDefinition $signature -Name Win32LockWorkStation -Namespace Win32Functions -PassThru […]

Continue Reading →

PowerShell: Listing Domains in Forest

Here is a function that lists all the domains in your forest: function Get-Domain{ $Root = [ADSI]”LDAP://RootDSE” try { $oForestConfig = $Root.Get(“configurationNamingContext”) } catch { Write-Warning ‘You are currently not logged on to a domain’ break } $oSearchRoot = [ADSI](“LDAP://CN=Partitions,” + $oForestConfig) $AdSearcher = [adsisearcher]”(&(objectcategory=crossref)(netbiosname=*))” $AdSearcher.SearchRoot = $oSearchRoot $AdSearcher.FindAll() | ForEach-Object { if ($_.Path -match […]

Continue Reading →

PowerShell: Finding Disk Controller Errors

This line will analyse your system event log for disk controller errors: PS> Get-EventLog -LogName System -InstanceId 3221487627 -ea 0 | ForEach-Object { $_.ReplacementStrings[0] } | Group-Object -NoElement | Sort-Object Count -Descending Count Name —– —- 23 \Device\Harddisk2\DR4 21 \Device\Harddisk2\DR2 7 \Device\Harddisk2\DR3 Such errors can indicate disk failure but most often they result from USB […]

Continue Reading →

PowerShell: Showing MsgBox

Ever wanted to display a dialog box from PowerShell rather than spitting out console text? Then try this function: function Show-MsgBox { param ( [Parameter(Mandatory=$true)] [String]$Text, [String]$Title = ‘Message’, [String] $Icon = ‘YesNo,Information’ ) Add-Type -AssemblyName ‘Microsoft.VisualBasic’ [Microsoft.VisualBasic.Interaction]::MsgBox($text, $icon, $title) } It is really easy to use: simply tell it what to display: Show-MsgBox -Text […]

Continue Reading →

PowerShell: Executing PowerShell on Computer Lock

PowerShell can respond to system events such as locking or unlocking a session. Here is a fun sample. Provided you have your sound card turned on, your computer will say good-bye when you press WIN+L and welcome you back when you log on again: function Start-Fun { $null = Register-ObjectEvent -InputObject ([Microsoft.Win32.SystemEvents]) -EventName “SessionSwitch” -Action […]

Continue Reading →
revia in alcoholism