Posts RSS Comments RSS 249 Posts and 391 Comments till now

Archive for May, 2009

XenApp cmdlets are HERE!!!

This is still a CTP of sorts, but it is open to the public. These are high quality and I suggest you give them a try.

You can download here: XenApp cmdlets

I hope to have some demos up soon, but until then my old demos should still work.

Setting SNMP configuration in VMWare using Powershell

I have a ton of respect for the VMWare guys and the incredibly useful set of cmdlets they provide (FOR FREE) found HERE, but man have I been banging my head against the wall trying to simply set the SNMP configuration on my ESX hosts.

To help ease the pain for the REST of the world here are some thoughts/gotchas.

- You must have the object return from Get-VMHostSNMP to use Set-VMHostSNMP or Test-VMHostSNMP. Simply providing the name will not work
- You cannot use the name with Get-VMHostSNMP. It appears you must connect to the actual ESX host (using connect-viserver) and then run Get-VMHostSNMP
- Once you “get” the VMHostSNMP object you can reuse without worrying about the connect-viserver command.
- You need to add a ReadOnlyCommunity to each host
- You need to Enable traps with the -enable:$true parameter
- TargetCommunity, TargetPort, TargetHost parameters require that you also pass -addtarget
- You need to enable SNMP on the ESX firewall

You may find the script below useful for getting a VMHostSNMP object for each ESXHost on your Virtual Center

Using the script below you can then do things like
$SNMPHostObjects = .\Get-SNMPHost.ps1 $VirtualCenter

To Add a Community
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -ReadOnlyCommunity “YourCommunity”}

To Enable
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -Enabled:$true }

To Add a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1″ -TargetCommunity “YourCommunity” -AddTarget }

To Test
$SNMPHostObjects | %{ Test-VMHostSnmp $_ }

To Remove a Target
$SNMPHostObjects | %{ Set-VMHostSnmp -HostSnmp $_ -TargetHost “1.1.1.1″ -removeTarget}

blog: Sick of WMI timeouts stopping your script? Try Test-Host!

I manage a large number of servers and this management involves a ton of WMI queries. A long time back I got sick of waiting and waiting for my WMI queries so I developed a little script called Test-Port that would do a WMI ping or would test a specific port (for firewalls) and pass the object on if connectivity passed.

You can see more details about that script here: Test-Port (kinda like portqry without verbose output)

With v2 just on the horizon I wanted to see what I could improve using Powershell v2 and decided to re-write the code (found below.)

Some of the new features are:
- Rename to Test-Host
- Built in Help and examples (biggy)
- Parameter Binding
- Added more logging with -verbose switch

This can be very useful if you ever have the need to do a task on a number of Computers (specifically WMI.) For example, If I needed to get all the machines running x64 in my domain I could something like this:

  1. $computers = Get-QADComputer -search $OU | %{$_.dnshostname} | Test-Host -tcp 135
  2. $query = "SELECT * FROM win32_processor WHERE addresswidth='64'"
  3. Get-WMIObject -query $query -comp $computers -asJob

Here is the code for Test-Host
Can download here: Test-Host.ps1 (from Poshcode.org)
  1. function Test-Host
  2. {
  3.  
  4. [CmdletBinding()]
  5.  
  6. Param(
  7. [Parameter(ValueFromPipeline=$true,Mandatory=$True)]
  8. [string]$Server,
  9. [Parameter()]
  10. [int]$TCPPort,
  11. [Parameter()]
  12. [int]$timeout=1000,
  13. [Parameter()]
  14. [string]$property
  15. )
  16. Begin
  17. {
  18. function TestPort {
  19. Param($srv,$tport,$tmOut)
  20. Write-Verbose " [TestPort] :: Start"
  21. Write-Verbose " [TestPort] :: Setting Error state = 0"
  22. $Error\3ActionPreference = "SilentlyContinue"
  23.  
  24. Write-Verbose " [TestPort] :: Creating [system.Net.Sockets.TcpClient] instance"
  25. $tcpclient = New-Object system.Net.Sockets.TcpClient
  26.  
  27. Write-Verbose " [TestPort] :: Calling BeginConnect($srv,$tport,$null,$null)"
  28. $iar = $tcpclient.BeginConnect($srv,$tport,$null,$null)
  29.  
  30. Write-Verbose " [TestPort] :: Waiting for timeout [$timeout]"
  31. $wait = $iar.AsyncWaitHandle.WaitOne($tmOut,$false)
  32. # Traps
  33. trap
  34. {
  35. Write-Verbose " [TestPort] :: General Exception"
  36. Write-Verbose " [TestPort] :: End"
  37. return $false
  38. }
  39. trap [System.Net.Sockets.SocketException]
  40. {
  41. Write-Verbose " [TestPort] :: Exception: $($_.exception.message)"
  42. Write-Verbose " [TestPort] :: End"
  43. return $false
  44. }
  45. if(!$wait)
  46. {
  47. $tcpclient.Close()
  48. Write-Verbose " [TestPort] :: Connection Timeout"
  49. Write-Verbose " [TestPort] :: End"
  50. return $false
  51. }
  52. else
  53. {
  54. Write-Verbose " [TestPort] :: Closing TCP Sockett"
  55. $tcpclient.EndConnect($iar) | out-Null
  56. $tcpclient.Close()
  57. }
  58. if($?){Write-Verbose " [TestPort] :: End";return $true}
  59. }
  60. function PingServer {
  61. Param($MyHost)
  62. Write-Verbose " [PingServer] :: Pinging $MyHost"
  63. $pingresult = Get-WmiObject win32_pingstatus -f "address='$MyHost'"
  64. Write-Verbose " [PingServer] :: Ping returned $($pingresult.statuscode)"
  65. if($pingresult.statuscode -eq 0) {$true} else {$false}
  66. }
  67. }
  68. Process
  69. {
  70. Write-Verbose ""
  71. Write-Verbose " Server : $Server"
  72. if($TCPPort)
  73. {
  74. Write-Verbose " Timeout : $timeout"
  75. Write-Verbose " Port : $TCPPort"
  76. if($property)
  77. {
  78. Write-Verbose " Property : $Property"
  79. if(TestPort $Server.$property -tport $TCPPort -tmOut $timeout){$Server}
  80. }
  81. else
  82. {
  83. if(TestPort $Server -tport $TCPPort -tmOut $timeout){$Server}
  84. }
  85. }
  86. else
  87. {
  88. if($property)
  89. {
  90. Write-Verbose " Property : $Property"
  91. if(PingServer $Server.$property){$Server}
  92. }
  93. else
  94. {
  95. Write-Verbose " Simple Ping"
  96. if(PingServer $Server){$Server}
  97. }
  98. }
  99. Write-Verbose ""
  100. }
  101. }

Sorry for the slew of duplicate posts

I setup a mirror to my TheExpertsCommunity.com blog.

blog: Why use LDAP filters (Powershell)

A common problem when dealing with Active Directory is the end user trying to parse the results themselves.

Let take this example
  1. $selector = New-Object DirectoryServices.DirectorySearcher
  2. $selector.SearchRoot = [ADSI]""
  3. $selector.pagesize = 1000
  4. $adobj= $selector.findall() | where {$_.properties.objectcategory -match "CN=Person"}
  5. foreach ($person in $adobj) {
  6. $date120DaysAgo = [DateTime]::Now.AddDays(-120).ToFileTime()
  7. $LL1 = $person.properties.lastlogontimestamp
  8. if(($LL1 -le $date120DaysAgo) -and ($person.GetDirectoryEntry().psbase.invokeget('AccountDisabled'))){$person}
  9. }

Instead of doing the parsing on results side... we should let the server do the work. How do we do that?

With LDAP filters. Here is an example.
  1. $filter = "(&(objectcategory=user)(userAccountControl:1.2.840.113556.1.4.803:=2)(lastlogontimestamp>=$date))"
  2. $ds = New-Object DirectoryServices.DirectorySearcher([ADSI]"",$filter)
  3. $ds.PageSize = 1000
  4. $users = $ds.FindAll()
  5. $users

Or with Quest tools... even easier!
  1. $date = (Get-Date).AddDays(-120).ToFileTime()
  2. $filter = "(lastlogontimestamp>=$date)"
  3. Get-QADUser -LdapFilter $filter -disabled

I think you will find with an LDAP filter you can save a TON of time.

Here is the output of measure-command for the two examples above (this was a very small sample.)

Without Filter
--------------
Days : 0
Hours : 0
Minutes : 0
Seconds : 3
Milliseconds : 477
Ticks : 34776670

With Filter
-----------
Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 34
Ticks : 340740

If you were to do this on a large AD the difference in time would be HUGE! Here is an example with 600K users...

With Filter
------------
Days : 0
Hours : 0
Minutes : 0
Seconds : 17
Milliseconds : 353
Ticks : 173535605

I can't post one with out filter... because it has been hours and it is still not done :)

Incredible… simply incredible (free SAN)

Ever needed a SAN to play with? Ever wanted to play with MS Cluster services at home, but didn’t want to fork out the cash for shared storage?

Here is your FREE!!! solution

Leading Storage Virtualization Now Free With Unlimited Connections and Large 2TB Size

I have no clue how they can do this, but I am happy none-the-less.

NOTE: This product is super simple to use and great for testing VMWare/Xen/MS clusters.

The Annual Scripting Games are approaching

Get out your favorite IDE and get to coding :)

PowerShellCommunity.org Joins Forces with Microsoft Scripting Guys to Host 2009 Summer Scripting Games

Scripting Games, June 15–26, 2009.

The twitter debate lives on…

While recently attending Synergy (it was a blast) I had a discussion with Brian Madden about the usefulness/uselessness of Twitter. I have been back and forth on twitter and even gave it a go a while back, but I never really found a good use for it. Brian made some solid points and told me to give it a shot. It is now several weeks later and I get it… I actually get value from twitter.

The basic idea is to keep it professional and control who you follow. A more detailed guide is below.

Brian Madden’s guide to using twitter for real work purposes

I challenge you to give it a shot for a week.

You can follow me @bsonposh

blog: I know it was slow coming, but here it is! (PhillyCodeCamp)

I apologize for taking so long to post this, but better late than never

Click HERE to download zip file with slides and vids.

blog: New XenApp cmdlets demo from Synergy

I have to say… the XenApp guys at Citrix get it and I mean they really get it. We all know there are areas where Citrix has blew it in the Powershell space, but this is not one of them (good time to note that Citrix has taken the feedback from the community and is changing things.)

btw… You can catch a vid of me and Brian Madden discussing Powershell HERE. If I sound a tad distracted… I am. This was done in the Expo hall with 1000s of people walking around. I was doing my best not to yell out ";LOOK!… SHINY!";

I had the privilege of showing off the new XenApp cmdlets to the world at Synergy and thought I would share the demo's with you guys as well.

Ok… back to the blog at hand :)

NOTE: It requires 4.5 with HR3 and above for most functionality. XenApp5 for full functionality.

Here is the first demo that demo's Get-XAFarm, Get-XAFarmConfiguration, Get-XAServer, and Get-XAApplication

Download XenAppDemo

Here is the list of the cmdlets included, sorted by verb:

Verb: Add
Name
—-
Add-XAAdministratorPrivilege
Add-XAApplicationAccount
Add-XAApplicationFileType
Add-XAApplicationServer
Add-XALoadEvaluatorServer
Add-XASessionPrinter

Verb: Clear
Name
—-
Clear-XAConfigurationLog

Verb: Connect
Name
—-
Connect-XASession

Verb: Copy
Name
—-
Copy-XAApplication

Verb: Disable
Name
—-
Disable-XAAdministrator
Disable-XAApplication
Disable-XAPolicy
Disable-XAServerLogOn

Verb: Disconnect
Name
—-
Disconnect-XASession

Verb: Enable
Name
—-
Enable-XAAdministrator
Enable-XAApplication
Enable-XAPolicy
Enable-XAServerLogOn

Verb: Get
Name
—-
Get-XAAccount
Get-XAAccountAuthority
Get-XAAdministrator
Get-XAAdministratorFolder
Get-XAAdministratorPrivilege
Get-XAApplication
Get-XAAppliedPolicy
Get-XAAutoReplicatedPrinterDriver
Get-XAClientModule
Get-XAClientPrinter
Get-XAConfigurationLog
Get-XAFarm
Get-XAFarmConfiguration
Get-XAFileType
Get-XAFolder
Get-XAHmrTest
Get-XAIconStream
Get-XALoadEvaluator
Get-XAMemoryOptimization
Get-XAOfflineLicense
Get-XAPolicy
Get-XAPolicyConfiguration
Get-XAPolicyFilter
Get-XAPrinter
Get-XAPrinterDriver
Get-XAPrinterDriverCompatibility
Get-XAPrinterDriverMapping
Get-XAPrintServer
Get-XAProfileApplication
Get-XAResultantPolicy
Get-XAServer
Get-XAServerConfiguration
Get-XAServerLoad
Get-XASession
Get-XASessionPrinter
Get-XASessionProcess
Get-XAStreamingSession
Get-XAVirtualIPRange
Get-XAZone

Verb: Import
Name
—-
Import-XALegacyApplication
Import-XAPrintServer

Verb: Move
Name
—-
Move-XAApplication
Move-XAFolder
Move-XAServer

Verb: New
Name
—-
New-XAAdministrator
New-XAApplication
New-XAAutoReplicatedPrinterDriver
New-XAClientPrinter
New-XAFolder
New-XAHmrTest
New-XAIcaFile
New-XALoadEvaluator
New-XAPolicy
New-XAPrinterDriverCompatibility
New-XAPrinterDriverMapping
New-XAVirtualIPRange

Verb: Remove
Name
—-
Remove-XAAdministrator
Remove-XAAdministratorPrivilege
Remove-XAApplication
Remove-XAApplicationAccount
Remove-XAApplicationFileType
Remove-XAApplicationServer
Remove-XAAutoReplicatedPrinterDriver
Remove-XAClientPrinter
Remove-XAFolder
Remove-XAHmrTest
Remove-XALoadEvaluator
Remove-XALoadEvaluatorServer
Remove-XAPolicy
Remove-XAPrinterDriverCompatibility
Remove-XAPrinterDriverMapping
Remove-XAPrintServer
Remove-XAServer
Remove-XASessionPrinter
Remove-XAVirtualIPRange

Verb: Rename
Name
—-
Rename-XAApplication
Rename-XAFolder
Rename-XAHmrTest
Rename-XALoadEvaluator
Rename-XAPolicy
Rename-XAZone

Verb: Replicate
Name
—-
Replicate-XAPrinterDriver

Verb: Reset
Name
—-
Reset-XAClientPrinter

Verb: Send
Name
—-
Send-XASessionMessage

Verb: Set
Name
—-
Set-XAAdministrator
Set-XAAdministratorFolder
Set-XAApplication
Set-XAFarmConfiguration
Set-XAHmrTest
Set-XALoadEvaluator
Set-XAPolicy
Set-XAPolicyConfiguration
Set-XAPolicyFilter
Set-XAPrinterDriverCompatibility
Set-XAPrinterDriverMapping
Set-XAServerConfiguration
Set-XAServerEdition
Set-XAServerZone
Set-XASessionPrinter

Verb: Stop
Name
—-
Stop-XASession
Stop-XASessionProcess

Verb: Test
Name
—-
Test-XAConfigurationLog
Test-XALicenseServer

Verb: Update

Name
—-
Update-XAFileType
Update-XAPrinter

Next »