Wednesday, September 5, 2012

get rid of parts in your string in MSSQL


this little snippet will help you to get rid of parts in strings.  in this case I wanted to get rid of the domain in the following string: 'domain\user'
 


declare @UserNameWithDomain varchar(32)
set @UserNameWithDomain = 'domain\user'

select replace(@UserNameWithDomain, left(@UserNameWithDomain, PATINDEX('%\%',@UserNameWithDomain)),'') as UsernameWithoutDomain

so, the result should be 'user' 

short explanation:


  • PATINDEX will find the '\'
  • LEFT will use the string from the beginning till the '\' found by PATINDEX
  • REPLACE will replace the defined left-string in the variable @UserNameWithDomain with nothing ('')


Tuesday, April 3, 2012

Download a file from an ftp server in powershell


Define the file you’d like to download and where to save:

$ftpfile = "ftp://ftp.adobe.com/pub/adobe/reader/win/10.x/10.1.2/en_US/AdbeRdr1012_en_US.exe"
$localfile = "C:\temp\AdbeRdr1012_en_US.exe"

Create new-objects. Click on the corresponding links to get information about “system.net.webclient” and “system.uri

$ftpclient = New-Object system.Net.WebClient
$uri = New-Object System.Uri($ftpfile)

And finally, download the file:

$ftpclient.DownloadFile($uri,$localfile)

Monday, April 2, 2012

Create a message box in powershell


Today I’d like to show how to create a message box in a very simple way:

What we need is some text, a title, the definition of our buttons and an icon for our window.

$text = "This is just a test"
$title = "Testbox"
$Buttons = [System.Windows.Forms.MessageBoxButtons]::OK
$Icon = [System.Windows.Forms.MessageBoxIcon]::Information

So now we got everything we need to pop this window up and we do it with this little command:

[System.Windows.Forms.MessageBox]::Show($text,$title,$Buttons,$Icon)

And that’s the result:


Tuesday, March 27, 2012

start or create a process in powershell

These are three possibilities to start a process in Powershell. The goal in this particular example is to open a text file called test.txt under “C:\temp” with notepad.

First solution -  cmdlet:

start-process -FilePath "$($env:SystemRoot)\notepad.exe" -ArgumentList 'C:\temp\test.txt'

Second solution – dot net:

[System.Diagnostics.Process]::Start("$($env:SystemRoot)\notepad.exe",'C:\temp\test.txt')

Third solution - wmi:

$process = [wmiclass]'Win32_Process'
$process.Create("$($env:SystemRoot)\notepad.exe C:\temp\test.txt")