September 2, 2010
To run a Powershell script at action 'Run a command' in vSphere alarm configuration, use the following syntax example:

c:\windows\system32\cmd.exe /c %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\vcalarm.ps1
August 23, 2010
An example how to use Write-Progress if you don't have a loop:


001
002
003
004
005
006
007
008
009
010
011
012
013
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "1" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "20" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "40" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "60" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "80" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Wait..." -PercentComplete "100" -Id 1
Start-Sleep -s 3
Write-Progress -Activity "Collecting Information" -status "Completed" -Completed -Id 1
With this script you send the credentials in clear text. But with this someone else can use it, like helpdesk. With password in encrypted file; no one else can use it.

 
$user = "adminuser"            
$password = ConvertTo-SecureString 'adminpassword' -AsPlainText -Force            
$cred = new-object System.Management.Automation.PSCredential $user,$password
 
 
August 20, 2010
#            
#Ping computer and just give result of action.            
#            
$strComputer = "localhost"            
$ping = New-Object System.Net.NetworkInformation.Ping            
$result = $ping.Send($strComputer)            
$result.Status
 
 
August 17, 2010
Output text and buttons are in swedish.


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 2010-08-16 08:43
# Generated By: dei3299
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button3Close = New-Object System.Windows.Forms.Button
$button2Clear = New-Object System.Windows.Forms.Button
$button1Get = New-Object System.Windows.Forms.Button
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$textBox1 = New-Object System.Windows.Forms.TextBox
$pictureBox1 = New-Object System.Windows.Forms.PictureBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1Get_OnClick= 
{
#Credentials local admin
$user = "admin"
$pass = cat K:\Övervakning\securepass.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass

$strComputer = $textBox1.text

$csystem = Get-WmiObject win32_computersystem -computername $strComputer -credential $cred
$richTextBox1.text+="Datornamn: " + $csystem.name

$os = Get-WmiObject win32_Operatingsystem -computername $strComputer -credential $cred
$sp = $os.ServicePackMajorVersion
$os = ($os.name).Split("|")
if ($sp -eq "0") {$richTextBox1.text+="`n" + $os[0]}
else {$richTextBox1.text+="`n" + $os[0]+" SP "+$sp}

$richTextBox1.text+="`nModel: " + $csystem.model
$totalminne = ($csystem.TotalPhysicalMemory)/1gb
$richTextBox1.text+="`nTotalt minne: " + "{0:N0}" -f $totalminne +" GB"
$richTextBox1.text+="`nAntal proc: " + $csystem.NumberOfProcessors

$cdisk = Get-WmiObject -class Win32_logicaldisk -computername $strComputer -credential $cred | where-object {$_.DeviceID -eq "C:"}
$richTextBox1.text+="`nLedigt utrymme på C: " + "{0:N0}" -f ($cdisk.freespace/1mb) + " MB"

$networkcard = Get-WmiObject win32_Networkadapter computername $strComputer -credential $cred | where-object {$_.AdapterType -eq "Ethernet 802.3"} | ForEach-Object {$_.name}
$richTextBox1.text+="`n`nNätverkskort: " + $networkcard


$richTextBox1.text+="`n`n**** Finished ****`n"
}

$button2Clear_OnClick= 
{
       $textBox1.text = $null

       $richTextBox1.text = $null

}

$button3Close_OnClick= 
{
$form1.close()

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon('K:\Övervakning\Tools\Images\Icons\server_Vista.ico')
$form1.Text = "System Information"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 284
$System_Drawing_Size.Height = 410
$form1.ClientSize = $System_Drawing_Size
$form1.AcceptButton = $button1Get

$button3Close.TabIndex = 5
$button3Close.Name = "button3Close"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button3Close.Size = $System_Drawing_Size
$button3Close.UseVisualStyleBackColor = $True

$button3Close.Text = "Avsluta"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 168
$System_Drawing_Point.Y = 364
$button3Close.Location = $System_Drawing_Point
$button3Close.DataBindings.DefaultDataSourceUpdateMode = 0
$button3Close.add_Click($button3Close_OnClick)

$form1.Controls.Add($button3Close)

$button2Clear.TabIndex = 4
$button2Clear.Name = "button2Clear"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button2Clear.Size = $System_Drawing_Size
$button2Clear.UseVisualStyleBackColor = $True

$button2Clear.Text = "Rensa"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 86
$System_Drawing_Point.Y = 364
$button2Clear.Location = $System_Drawing_Point
$button2Clear.DataBindings.DefaultDataSourceUpdateMode = 0
$button2Clear.add_Click($button2Clear_OnClick)

$form1.Controls.Add($button2Clear)

$button1Get.TabIndex = 3
$button1Get.Name = "button1Get"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1Get.Size = $System_Drawing_Size
$button1Get.UseVisualStyleBackColor = $True

$button1Get.Text = "Hämta"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 364
$button1Get.Location = $System_Drawing_Point
$button1Get.DataBindings.DefaultDataSourceUpdateMode = 0
$button1Get.add_Click($button1Get_OnClick)

$form1.Controls.Add($button1Get)

$richTextBox1.Name = "richTextBox1"
$richTextBox1.Text = ""
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 61
$richTextBox1.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 268
$System_Drawing_Size.Height = 296
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 2

$form1.Controls.Add($richTextBox1)

$groupBox1.Name = "groupBox1"

$groupBox1.Text = "IP-adress"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 214
$System_Drawing_Size.Height = 50
$groupBox1.Size = $System_Drawing_Size
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 58
$System_Drawing_Point.Y = 4
$groupBox1.Location = $System_Drawing_Point
$groupBox1.TabStop = $False
$groupBox1.TabIndex = 1
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0

$form1.Controls.Add($groupBox1)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 201
$System_Drawing_Size.Height = 20
$textBox1.Size = $System_Drawing_Size
$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox1.Name = "textBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 20
$textBox1.Location = $System_Drawing_Point
$textBox1.TabIndex = 0

$groupBox1.Controls.Add($textBox1)


$pictureBox1.TabIndex = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 47
$System_Drawing_Size.Height = 50
$pictureBox1.Size = $System_Drawing_Size

$pictureBox1.Image = [System.Drawing.Image]::FromFile('K:\Övervakning\Tools\Images\Icons\hardware_icons\PNG\png48\server.png')
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 4
$System_Drawing_Point.Y = 4
$pictureBox1.Location = $System_Drawing_Point


$pictureBox1.TabStop = $False
$pictureBox1.Name = "pictureBox1"
$pictureBox1.DataBindings.DefaultDataSourceUpdateMode = 0

$form1.Controls.Add($pictureBox1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
March 25, 2010
I have this script:

001
002
003
004
005
006
007
008
function isWithin([int]$days, [datetime]$Date)
{[DateTime]::Now.AddDays($days).Date -le $Date.Date}

$list = get-childitem "X:\Program Files\vizioncore\esxRanger Professional\Logs\Backup\" -recurse | where {!$_.PSIsContainer} | where {isWithin -1 $_.lastwritetime}
$list
foreach ($file in $list)
{$file | Select-String -pattern "Failure"
}

It checks for the text pattern Failure in files dated today or yesterday in a specific catalog.
I get the ouput:

    Directory: X:\Program Files\vizioncore\esxRanger Professional\Logs\Backup


Mode                LastWriteTime     Length Name                                                                                                                         
----                -------------     ------ ----                                                                                                                         
-a---        2010-03-25     12:25       1527 NECert02SuSE_20100325_232528.txt                                                                                     
-a---        2010-03-25     12:23        792 ResourcePool_resgroup-726_20100325_230002.txt                                                                        
-a---        2010-03-25     12:26       8880 testfcswin_20100325_134707.txt                                                                                       

IgnoreCase : True
LineNumber : 13
Line       : Logon failure: unknown user name or bad password.
Filename   : NECert02SuSE_20100325_232528 - Kopia.txt
Path       : X:\Program Files\vizioncore\esxRanger Professional\Logs\Backup\NECert02SuSE_20100325_232528 - Kopia.txt
Pattern    : Failure
Context    :
Matches    : {failure}

IgnoreCase : True
LineNumber : 19
Line       : Error: Logon failure: unknown user name or bad password.
Filename   : NECert02SuSE_20100325_232528 - Kopia.txt
Path       : X:\Program Files\vizioncore\esxRanger Professional\Logs\Backup\NECert02SuSE_20100325_232528 - Kopia.txt
Pattern    : Failure
Context    :
Matches    : {failure}

IgnoreCase : True
LineNumber : 22
Line       : Error In Backup Operations! Error: An error occurred during backup operations. Logon failure: unknown user name or bad password.
Filename   : NECert02SuSE_20100325_232528 - Kopia.txt
Path       : X:\Program Files\vizioncore\esxRanger Professional\Logs\Backup\NECert02SuSE_20100325_232528 - Kopia.txt
Pattern    : Failure
Context    :
Matches    : {failure}





If I only want the filename and the Line with error in it in the output. How do I do it??
March 23, 2010
Today I made my first powershell script and put it in use.
It deletes files older than 30 days in a tree structure, count number of  files removed and total file size removed.


001
002
003
004
005
006
007
008
009
010
$date = [DateTime]::Now.AddDays(-30)
$list = get-childitem L:\scan\ -recurse | where {!$_.PSIsContainer} | where {$_.LastWriteTime -le $date}
$sizetotal = 0
$files =0
foreach ($file in $list)
{$sizetotal = $sizetotal + $file.Length/1MB
 $files++
 Remove-Item $file.fullname -force}
"No of files removed: " + $files
"Total file size in MB: " + "{0:N0}" -f $sizetotal
March 11, 2010
Here comes some scripts later on....