powershell

How can I run initialization code each time my snap-in is loaded?

I have a PowerShell snapin, and I would like to run a bit of initialization code (hooking some AppDomain events) each time my snapin is loaded (i.e. once for each powershell.exe process that is started). How can this be accomplished? ...

Redirect write-host to a textbox in a form?

I suppose my question is somewhat similar to this: http://stackoverflow.com/questions/2607865/redirecting-the-standard-output-input-error-into-from-a-textbox but it is powershell-centric. I'm working on an GUI created using PrimalForms that will act as a frontend to my website deployment scripts. Up until now, I've been using start-tra...

Is Powershell's stack trace broken?

I am writing a simple unit test harness in powershell I designed the harness such that its assert functions takes a script-block as a parameter to allow the harness to run the code from within the assert function and treat any exceptions are thrown as a test failure. If the tests fail I want to return line in the unit test in which the...

Use Powershell to create access 2007 Queries?

I have been following Richard Siddaway's Awesome Series on Powershell+Access2007. Unfortunately it ends before discussing creating/running/modifying access 2007 queries in powershell. How could this be done? ...

How to use Powershell to Monitor a specific inbox for Commands?

I would like to set up functionality where a powershell script would access the email in a specific mailbox. It would then parse each email to engage in dialog with a specific set of users. To serve as a system that could be interacted with through email. How could this be pulled off? ...

Powershell Using Backtick for new lines - weirdness

What exaxctly is happening in example 1? How is this parsed? # doesnt split on , [String]::Join(",",("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa," + ` "aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa".Split(',') ` | foreach { ('"' + $_ + '"') })) # adding ( ) does work [String]::Join(",",(("aaaaa,aaaaa,aaaaa,aaaaa,aaa...

How to make C# Powershell Invoke member thread safe

I have this service that, when request is received, runs a powershell command and returns result. Here is the invoker class code: public class PowerShellScript { public PowerShellScript() { } public Object[] Invoke( String strScriptName, NameValueCollection nvcParams ) { Boolean bResult = true; int n = 0; ...

SQL Server 2008, Powershell and Web Services

Hi, Powershell provides the New-WebServiceProxy cmdlet which allows for a web service proxy object to be created. However the Powershell in SQL Server 2008 doesn't appear to support this. Is there a way to get SQL Server's Powershell to support the New-WebServiceProxy cmdlet? Many thanks. ...

Getting Current Item of Higher Scope While Loop

I have a nested switch statement inside a switch statement. I am wondering if it is possible to grab the un-nested current item within the scope of the nested switch statement. I know I can accomplish this with a for loop with moderate difficulty but I am hoping there is some elegance I am missing to accomplishing this with switch statem...

Can the Invoke-Item cmdlet launch an executable with parameters?

I'm trying to install some custom Windows services using PowerShell, and I've been unable to run InstallUtil without getting the following error: A positional parameter cannot be found that accepts argument '' Here's what I've run that causes the error above: Invoke-Command -ComputerName <remote machine> -ScriptBlock {Invoke-Item ...

Running remote GUI app in Powershell

We have a custom comonent that wraps some of the functionality of powershell so it can be used frim BizTalk 2006. For most operations (checking a file path, copy or move a file) this works fine. However we have the need to fire up a GUI app remotely to do some processing. The component itself handles the connection to the remote box, ...

Import CSV contacts to Public Folders

I am trying to import a CSV file containing many contacts to a public folder in exchange 2007 installed on SBS2008 for use by outlook users. I have this script that I am trying to run through powershell 1.0 (there seems to be no powershell 2.0 installable for SBS2008). I have created a folder in the Public Folder area called pub_contac...

"PS c:\> New-WebSite -Blah:" throws Index was outside the bounds of the array

On one of my servers the command New-WebSite stopped working today (it was working fine yesterday), throwing the exception "Index was outside the bounds of the array". PS C:\Windows\system32> Import-Module WebAdministration PS C:\Windows\system32> New-WebSite -Blah New-Item : Index was outside the bounds of the array. + CategoryInf...

Call REST API from PowerShell Script

How can I call a rest based API from a PowerShell script and process the Json answer? ...

Getting all virtual directories for a IIS6 web site using WMI

I want to list all virtual directories that belong to a web site with a certain name using WMI and PowerShell. I know I can list all virtual directories on a server using the code below, but how can I filter out only those that belong to a specific site? Get-WmiObject IIsWebVirtualDir -namespace "ROOT\MicrosoftIISv2" ...

How do I install the Exchange Management Shell Module to run Powershell Exchange Scripts in code

I am struggling getting Exchange mManagement Shell commands to run in my C# code. Here' the problem. I have access to a test Exchange Server running Exchange 2010. I have run my Exchange Mgmt Shell commands directly on the server inside the Exchange Mgmt Shell Simple Example: get-mailbox I created a console app to run my PS c...

Powershell - Check if file is in use

Is there a Powershell command to check if a file is in use by another user? If not, what would it take to write a script that can? ...

How to re-write powershell code in bash

I've been tasked with re-writing this in bash. But whilst most powershell is easy to read, i just dont get what this block is actually doing!!? Any ideas? It takes a file which is sorted on a key first, maybe thats relevant! Thanks for any insights! foreach ($line in $sfile) { $row = $line.split('|'); if (-not $ops[$row[1]]) { ...

Can you implement a dynamically created PSObject such that equality and comparison work?

I have been implementing makeshift types by dynamically adding methods to PSObjects I want to be able to compare two instances of my objects using the "-eq" "-lt" and "-gt" operators (I assume this would require me to implement interfaces like IComparible, and IEquatible) Is this sort of thing possible? (I'm thinking perhaps not as the...

Does powershell have a method_missing()?

I have been playing around with the dynamic abilities of powershell and I was wondering something Is there is anything in powershell analogous to Ruby's method_missing() where you can set up a 'catch all method' to dynamically handle calls to non-existant methods on your objects? ...