In This Episode
* Greg and Jean “The Scripting Guys” themselves are our special guests. Head over to the Script Center!
You can grab the show at powerscripting.net
WordPress database error: [Table 'halr_pmachine.wp_categories' doesn't exist]
SELECT cat_id, cat_name FROM wp_categories WHERE link_count > 0
In This Episode
* Greg and Jean “The Scripting Guys” themselves are our special guests. Head over to the Script Center!
You can grab the show at powerscripting.net
Just a side note here. I was in #PowerShell chatting with my peeps on random stuff, and somehow Altiris (now Symantec) Deployment Solution came up and I mentioned that I had used it back when it was a native app, but I heard the latest version was going to be only available as a web console and that I didn’t like the idea at all. Immediately three other guys are chipping in with the same comments. One even said he’s going to drop the product because of this.
Don’t these companies ask their users what they want? (Rhetorical question, unfortunately.)
Jaykul from huddledmasses.org has just released the first public version of his new XMPP utility for PowerShell called PoshXmpp. As I mentioned the other day, there is some interest lately in combining XMPP with PowerShell. With Jaykul’s tools, you now have a basic Jabber client for your Windows command-line. But more than that is the possibilities it raises for stuff like the example below. (In PowerShell terms, PoshXmpp is a “snapin” which is a collection of “cmdlets”.)
Connect-Chat — connect to MUC chats: try “PowerShell%irc.freenode.net@irc.im.flosoft.biz” to connect to the PowerShell IRC channel. NOTE: messages from these come the same way as regular chat messages… you have to Receive-Message to see anything. You could Receive-Message -Loop > log.txt ….
The homepage is here, and the code is here (requires PowerShell to be installed first).
A user at PowerShellCommunity.org asked how to send a Jabber message using PowerShell. There is a set of commercial cmdlets available from /n software that can do this (and a lot more), but they are a little pricey for the casual user. As luck would have it, PowerShell makes it super easy to work with .NET stuff and I happened to know there was a few .NET libraries already in existence. After reviewing what was available, I decided on Alex Gnauck’s excellent agsXMPP SDK.
It took a bit of work to translate the C# examples to PowerShell, but there was another guy at AG-Software forums who was working on this as well and he had an example I could work with. Also, Alex was a great help in some troubleshooting.
Next step–a Jabber bot.
So, without further ado, the code (script repository link):
1: # Download XMPP SDK from http://www.ag-software.de/index.php?page=agsxmpp-sdk
2: function Send-XmppMessage {
3: param (
4: $From = $( Throw “You must specify a Jabber ID for the sender.” ),
5: $Password, # Leave blank to be prompted for password
6: $To = $( Throw “You must specify a Jabber ID for the recipient.” ),
7: $Body = $( Throw “You must specify a body for the message.” )
8: )
9:
10: # This function reads a string from the host while masking with *’s.
11: function Read-HostMasked( [string]$prompt=“Password” ) {
12: $password = Read-Host -AsSecureString $prompt;
13: $BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($password);
14: $password = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR);
15: [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR);
16: return $password;
17: }
18: # Set path accordingly.
19: $assemblyPath = $(resolve-path $profiledir\Assemblies\agsXMPP.dll)
20: [void][reflection.assembly]::LoadFrom( $assemblyPath )
21: $jidSender = New-Object agsxmpp.jid( $From )
22: $jidReceiver = New-Object agsxmpp.jid ( $To )
23: $xmppClient = New-Object agsxmpp.XmppClientConnection( $jidSender.Server )
24: $Message = New-Object agsXMPP.protocol.client.Message( $jidReceiver, $Body )
25:
26: # The following switches may assist in troubleshooting connection issues.
27: # If SSL and StartTLS are disabled, then you can use a network sniffer to inspect the XML
28: #$xmppClient.UseSSL = $FALSE
29: #$xmppClient.UseStartTLS = $FALSE
30:
31: # Since this function is only used to send a message, we don’t care about doing the
32: # normal discovery and requesting a roster. Leave disabled to quicken the login period.
33: $xmppClient.AutoAgents = $FALSE
34: $xmppClient.AutoRoster = $FALSE
35:
36: # Use SRV lookups to determine correct XMPP server if different from the server
37: # portion of your JID. e.g. user@gmail.com, the server is really talk.google.com
38: $xmppClient.AutoResolveConnectServer = $TRUE
39: if ( !$password ) { $password = Read-HostMasked }
40:
41: # Open connection, then wait for it to be authenticated
42: $xmppClient.Open( $jidSender.User, $Password )
43: while ( !$xmppClient.Authenticated ) {
44: Write-Verbose $xmppClient.XmppConnectionState
45: Start-Sleep 1
46: }
47: # If server disconnects you, try enabling this
48: #$xmppClient.SendMyPresence()
49: $xmppClient.Send( $Message )
50: # Send is asynchronous, so we must wait a second before closing the connection
51: Start-Sleep 1
52: $xmppClient.Close()
53: }
A few days ago Shay Levy (from $cript fanatic) had replied to a thread on microsoft.public.windows.powershell detailing several methods of working with processes, some of which I had not seen before. The best thing about it was the simple fact that he had compiled all of them in one place. So I asked him to blog about it and then I helped him edit it a bit. The resulting article should be very helpful to admins who want to start or otherwise control processes on their machines using PowerShell. so if you have not checked it out yet, go read the article.