Thursday, October 29, 2009

Fix Citrix\XenApp WMI issues

Today I run into problem with WMI registrations on few servers. Even though namespace root\Citrix was available, there were no classes.

Below is simple batch script that should be able to solve most WMI-related issues:

:: Remove Repository
Net Stop winmgmt
RmDir /s /q "%WinDir%\System32\WBEM\Repository"
Net Start winmgmt

:: Register core libraries
Regsvr32 /n /I /s "%WinDir%\system32\userenv.dll"
regsvr32 /n /I /s "%WinDir%\system32\scecli.dll"

:: Compile Windows WMI
If Exist "%WinDir%\System32\WBEM" (
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%WinDir%\System32\WBEM\*.dll"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG RegSvr32.exe /s "%WinDir%\System32\Wbem\%%X"
    )
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%WinDir%\System32\WBEM\*.mof"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG MofComp.exe "%WinDir%\System32\Wbem\%%X"
    )
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%WinDir%\System32\WBEM\*.mfl"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG MofComp.exe "%WinDir%\System32\Wbem\%%X"
    )
)

:: Compile Citrix WMI
If Exist "%ProgramFiles%\Citrix\System32\Citrix\WMI" (
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%ProgramFiles%\Citrix\System32\Citrix\WMI\*.dll"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG RegSvr32.exe /s "%ProgramFiles%\Citrix\System32\Citrix\WMI\%%X"
    )
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%ProgramFiles%\Citrix\System32\Citrix\WMI\*.mof"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG MofComp.exe "%ProgramFiles%\Citrix\System32\Citrix\WMI\%%X"
    )
    For /f "usebackq tokens=*" %%X IN (`Dir /b "%ProgramFiles%\Citrix\System32\Citrix\WMI\*.fom"`) Do (
        Call %S4_Lib%\SCRIPT_RUNLOG MofComp.exe "%ProgramFiles%\Citrix\System32\Citrix\WMI\%%X"
    )
)

Wednesday, October 21, 2009

Powershell and argument issue

I was creating very simple function today, however got stuck there for a while…

As you can see below, function is very, very simple:
Function Global:Security\Encrypt-String ([string]$Input){Return [LoginConsultants.Crypto.Password]::EnCrypt($Input)}

It’s using our library to encrypt some text… To my surprise, it didn’t work however. After a little investigation, it turned out that I made rookie mistake – I used $Input, while it is reserved variable :) Very, very stupid mistake of course, I was just surprised that Posh doesn’t complain if you use such parameter.

Anyway, below is small function you can use to test if parameter you want to use is valid:
function Test-ParameterName ([string]$Name) {
           Return [boolean]$($(Test-Path Variable:$Name) -eq 0)
}

As you can see, it’s extremely primitive, only interesting part is Return [boolean]$($(Test-Path Variable:$Name) -eq 0) – meaning of this function is to revert boolean value. So if Test-Path is $True, function will return $False and if Test-Path is $False, it will return $True. I remember that some years ago I used [boolean]$Foo – 1 in .NET, however it is not supported in Powershell itself (operator – is not defined for [boolean] and [int]).

Martin

Tuesday, October 20, 2009

Windows 7 madness

I think that every geek is googling for phrases like “Windows 7 tips and tricks” or “Windows 7 secrets” once he thinks he knows enough about new product.

Windows 7 is new IT phenomenon – you can discuss it, disagree with it, hate it, but that’s about it. Let’s have a look at Google Trends:

Win7 vs Leopard

Blue represents Windows 7, red is main competitor Snow Leopard

Just for fun, below is similar graph comparing Windows 7, Windows XP and Windows Vista.

Windows family

Windows 7 vs Windows XP vs Windows Vista

First, let me give you my presentation from 1.10.2009 about Windows 7.

Second, I was reading through a lot of tips and tricks pages. Today I noticed article called 77 Windows 7 Tips. To make long story short, it’s obvious that number 77 was chose first and then they started to think about the content :( Otherwise I don’t see any explanation for some of those tips.

On the other hand, there are other articles that are discussing new features and tips\tricks and are great. To mention just a few of them, Tim Sneath got great article and I also liked a lot article from TechRadar.

So, I decided to write a series of articles about Windows 7. Currently I got 91 tips, but I started yesterday, so it shouldn’t be problem to get to 100. Some of the tricks are really simple and can be explained on 2-3 lines, some will require a lot more space. I don’t want to simply publish tricks, but also explain how some new technologies works and provide some additional details (for example performance comparison of older Robocopy and new one). I will call these articles Windows 7 Maddness and I will try to group tips together.

Hold on to see more :)

Friday, September 25, 2009

Sorting hashtable in Powershell

I am using hashtables (or dictionaries) more and more in Powershell – once I figured out that syntax $Hashtable.Key works, it turned out to be extremely useful as something similar to custom class.

However I run into an issue (maybe bug?) with sorting hashtables:

PS C:\> $X.Martin = "Zugec"
PS C:\> $X.Kamila = "Vlasakova"
PS C:\> $X.Filip = "Puntik"
PS C:\> $X.Tereza = "Liska"
PS C:\> $X | Sort Name

Name                           Value
----                           -----
Filip                          Puntik
Kamila                       Vlasakova
Tereza                      Liska
Martin                       Zugec

As you can see, output is not sorted. After a while I tried to use raw method that is used in .NET:

PS C:\> $X.psbase.GetEnumerator() | Sort Name

Name                           Value
----                           -----
Filip                          Puntik
Kamila                       Vlasakova
Martin                       Zugec
Tereza                      Liska

As you can see, everything is sorted out correctly in this case. Another scenario where you could use GetEnumerator is if you want to handle name (key) and value:

PS C:\> $X.psbase.GetEnumerator() | Where {$_.Name -eq "Martin"}

Name                           Value
----                           -----
Martin                         Zugec

Without using enumerator, you got access only to values.

Monday, September 7, 2009

How to display all performance counters in console

Just run into this today – I am writing some scripts for Citrix License server and wanted to see which performance counters are available…

Turned out to be pretty simple – just use “TypePerf” command with –q:

   1: C:\Users\Martin>typeperf -q | more



   2: \TBS counters\CurrentResources



   3: \TBS counters\CurrentContexts



   4: \WSMan Quota Statistics(*)\Active Users



   5: \WSMan Quota Statistics(*)\Active Operations



   6: \WSMan Quota Statistics(*)\Active Shells



   7: ...


Friday, August 28, 2009

Get or set file\directory attributes using Powershell

This is just “reminder post” for me if I will need to use it in future…

I wanted to create hidden folder using Powershell – in fact, it’s not very hard:

$(MkDir “Martin Zugec”).Attributes = ‘Hidden’

.Attributes is FileAttributes enumeration, so you use same syntax when you want to create files. Below are available values:

Archive Applications use this attribute to mark file\folder for backup or removal
Compressed Is compressed
Device N/A – in future maybe ;) Looks promising :)
Directory “File is directory” :)
Encrypted Encrypted
Hidden Hidden
Normal If no other attributes are applied
NotContentIndexed Skip for indexing
Offline File\Folder is offline
ReadOnly File\Folder is read-only
ReparsePoint Contains reparse point. Reparse point is user data associated with this entry – one example where reparse points are used is when you mount folder into another.
SparseFile Sparse files are usually large files whose data are mostly zeros. To be honest, I never saw sparse file as far as I know – even fake huge files (FSUtil File CreateNew) are not sparse files.
System System file
Temporary If file is marked as temporary, file system will try to keep all the data in memory for quicker access instead of flushing data back to harddisk. Of course temporary file should be deleted as soon as possible if not needed ;)

If you want to change attribute of existing folder\file, it’s also very easy:

$(Get-Item “Martin Zugec”).Attributes = ‘Hidden’

Wednesday, August 26, 2009

How to return multiple values from Powershell function

I used this trick few times when I had function that had to return more values…

As I wrote here, you can access dictionary object using syntax $Dictionary.Key… If your function returns dictionary, then you can use it to your advantage :)

   1: Function Get-MultiValue () { 



   2:  [hashtable]$Return = @{} 



   3:  



   4:  $Return.Success = $True 



   5:  $Return.PercentComplete = 100 



   6:  $Return.ReturnTime = $(Get-Date) 



   7:  $Return.Username = "Martin Zugec" 



   8:  



   9:  Return $Return 



  10: }




Usage is pretty obvious:





   1: $Var = Get-MultiValue 



   2:  



   3: If ($Var.Success) { 



   4:     $Var.UserName 



   5:     $Var.ReturnTime 



   6: }




Easy and useful sometimes…