Functions and Subs
PowerShell – Multiple (-and) (-or) within a single If statement
I had a hard time finding examples on the web on how or if you could code multiple If’s into a single If statement, so I thought I would just quick post up an example once I figured out how to do it. Perhaps I wasn’t looking in the right places but all the examples I saw were just a single (-and) or (-or) in the code.
So based on being able to code an If statement in either of the following fashions:
If ($someAttribute -eq $false -or $someAttribute -eq $Null){
}
or the following:
If (($someAttribute -eq $false) -or ($someAttribute -eq $Null)){
}
Then we should be able to do a combined effort:
If (($someAttribute -eq $false -or $someAttribute -eq $Null) -and ($someUser -eq “UserID123″)){
Write-Host “Switching to True”
Set-QADUser $someUser -Service “MyDomain.org” -ObjectAttributes @{$someAttribute = $True}
If (!$?) {Write-Host “Error: $($error[0])”}
}
So if $someAttribute is either $false or $Null AND #someUser equals “UserID123″ then do something… in this case I switch the Attribute someAttribute to $true and do some error checking.
~ITNotes
PowerShell – ProperCase Function
Change any case to Proper Case (‘ITNOTES DoT net’ to ‘Itnotes Dot Net’):
This is very useful for us during scripted user creations when the usernames and such from the source were in random cases. This script could also be useful with auto user provisioning with ILM/FIM 2010 from another datasource, we were using a VBScript that is much longer.
This script basically converts everything to lowercase then capitalizes the first letter of each word.
function ToProperCase ([String]$in)
{
$in = $in.Tolower()
$textInfo = [System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo
return $textInfo.ToTitleCase($in)
}
Example of use:
$Title = “ITNOTE DOT NET”
ToProperCase($Title)
Returns:
Itnotes Dot Net
Complete Script
function ToProperCase ([String]$in){
$in = $in.Tolower()
$textInfo = [System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo
return $textInfo.ToTitleCase($in)
}
$Title = “ITNOTE DOT NET”
ToProperCase($Title)
As noted by Lee, here is a oneliner contributed by Jonathan Noble at http://www.jonoble.com/. Thanks Lee.
(Get-Culture).TextInfo.ToTitleCase(“THIS IS MY STRING”.ToLower())
I am always in favor of shorter scripts nevertheless I also favor functions, so here is a much shorter function:
function ToProperCase ([String]$in){
return (Get-Culture).TextInfo.ToTitleCase($in.ToLower())
}
Re-Usable VBScript Functions – Part I
In this Multi-Part Series I will post lists on VBScript Functions and Subs that I have either found or written. I am a big fan of turning any useful code into a functions and adding it to my default VBScript Template, it saves a ton of time on the next scripting task. By no means am I claiming I wrote all of these, most were just re-purposed into reusable functions. I hope you find these useful. In the later part of the series I will include my template I use to begin all my scripts.
Determine if the computer is a laptop:
Function IsLaptop(strComputer) On Error Resume Next Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" ) Set colItems = objWMIService.ExecQuery( "Select * from Win32_Battery", , 48 ) IsLaptop = False For Each objItem in colItems IsLaptop = True Next If Err Then Err.Clear On Error GoTo 0 End Function |
Example of use
If IsLaptop( "." ) Then WScript.Echo "Laptop Else WScript.Echo "Desktop or server End If |
Shell out a command line with switches:
Sub RunSwch(ByVal strRunCmd, strRunSwitch)
Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run Chr(34) & strRunCmd & Chr(34)& " " & strRunSwitch, 1, True
Set objWshShell = Nothing
End Sub |
Example of use
RunSwch "C:\Program Files\ApplicationX\Start.exe","/S /X /Log C:\log.log" |
Shell out a command line without switches:
Sub Run(ByVal strRunCmd)
Dim objWshShell
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run Chr(34) & strRunCmd & Chr(34), 1, True
Set objWshShell = Nothing
End Sub |
Example of use
Run "C:\Program Files\ApplicationX\Start.exe" |
