PowerShellRay

Archive for the ‘Generic Scripts’ Category

 

import-module activedirectory
$ADO = Search-ADAccount -AccountExpired
foreach($object in $ADO)
{
Remove-ADObject -Identity $object.DistinguishedName -Confirm:$false
function hotfixcompare ($computer1, $computer2)
{
$node1 = Get-HotFix -ComputerName $computer1
$node2 = Get-HotFix -ComputerName $computer2
Compare-Object -ReferenceObject $node1 -DifferenceObject $node2  -Property HotFixID 
}
hotfixcompare ex01 lyncse |sort -Property sideindicator

node1 = ex01

node2 = lyncse

Sideindicator == means that the 2 nodes have the same hotfixes

Sideindicator => means that the hotfix is installed on the second node “lyncse”

Sideindicator <= means that the hotfix is installed on the first node “ex01”

 

 

#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 4/15/2011 9:34 PM
# Author By: Raymond
########################################################################
function listprograms {  
  
Get-WmiObject win32_product -ComputerName $text1.Text | Out-GridView  
  
}  
  
function remoteuninstall {  
  
$app = Get-WmiObject win32_product -ComputerName $text1.Text | Where-Object {$_.name -match $text2.Text}  
$app.uninstall() | Select-Object -Property returnvalue  
}
  

#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
$label2 = New-Object System.Windows.Forms.Label
$label1 = New-Object System.Windows.Forms.Label
$Text2 = New-Object System.Windows.Forms.RichTextBox
$Text1 = New-Object System.Windows.Forms.RichTextBox
$Button2 = New-Object System.Windows.Forms.Button
$Button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$Button1_OnClick= 
{
#TODO: Place custom script here
remoteuninstall
}

$handler_label2_Click= 
{
#TODO: Place custom script here

}

$handler_EnterComputerName_TextChanged= 
{
#TODO: Place custom script here

}

$handler_label1_Click= 
{
#TODO: Place custom script here

}

$handler_button2_Click= 
{
#TODO: Place custom script here
listprograms

}

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

#----------------------------------------------
#region Generated Form Code
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200)
$form1.Text = "Uninstall program"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 490
$System_Drawing_Size.Height = 219
$form1.ClientSize = $System_Drawing_Size

$label2.TabIndex = 5
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 154
$System_Drawing_Size.Height = 23
$label2.Size = $System_Drawing_Size
$label2.Text = "Enter Application Name"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 271
$System_Drawing_Point.Y = 91
$label2.Location = $System_Drawing_Point
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Name = "label2"
$label2.add_Click($handler_label2_Click)

$form1.Controls.Add($label2)

$label1.TabIndex = 4
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 141
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = "Enter Computer Name"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 92
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = "label1"
$label1.add_Click($handler_label1_Click)

$form1.Controls.Add($label1)

$Text2.Name = "Text2"
$Text2.Text = ""
$Text2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 271
$System_Drawing_Point.Y = 36
$Text2.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 175
$System_Drawing_Size.Height = 29
$Text2.Size = $System_Drawing_Size
$Text2.TabIndex = 3

$form1.Controls.Add($Text2)

$Text1.Name = "Text1"
$Text1.Text = ""
$Text1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 36
$Text1.Location = $System_Drawing_Point
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 175
$System_Drawing_Size.Height = 29
$Text1.Size = $System_Drawing_Size
$Text1.TabIndex = 2
$Text1.add_TextChanged($handler_EnterComputerName_TextChanged)

$form1.Controls.Add($Text1)

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

$Button2.Text = "List installed programs"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 371
$System_Drawing_Point.Y = 162
$Button2.Location = $System_Drawing_Point
$Button2.DataBindings.DefaultDataSourceUpdateMode = 0
$Button2.add_Click($handler_button2_Click)

$form1.Controls.Add($Button2)

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

$Button1.Text = "Uninstall"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 24
$System_Drawing_Point.Y = 162
$Button1.Location = $System_Drawing_Point
$Button1.DataBindings.DefaultDataSourceUpdateMode = 0
$Button1.add_Click($Button1_OnClick)

$form1.Controls.Add($Button1)

#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

I would like to say thank you for Sheif Talaat “MVP PowerShell” for help me to learn PowerShell and for his effort in developing this script in less than 2 min.

This script will help you to pronounce words using PowerShell
$spokenText = “Hi my name is Raymond”

$voice = New-Object -com SAPI.SpVoice
$voiceList = $voice.GetVoices()
$voiceDescList = @()

for ($i=0; $i -lt $voiceList.Count; $i++)
{

$voiceDescList += $voiceList.Item($i).GetDescription()
}

if ($voiceDescList -contains “LH Michelle”)

{
$voiceMember = “Name=LH Michelle”
}

else

{
$voiceMember = “Name=” + $voiceDescList[0]
}

$voiceToUse = $voice.GetVoices($voiceMember)
$voice.Voice = $voiceToUse.Item(0)
[void] $voice.Speak($spokenText)

This script  will take affect after restart

$NewName="windowsserver"
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerInfo.Rename($NewName)
Restart-Computer

$dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$root = $dom.GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.filter = "(&(objectclass=computer)(name=windowsserver))"
$search.findall() | %{$_.GetDirectoryEntry() } | %{$_.DeleteObject(0)}

This script helps to join the machine to the domain local and if you want to do it remotely you can combined this script using invoke-command

$credential = New-Object System.Management.Automation.PsCredential("coex\administrator", (ConvertTo-SecureString "P@ssw0rd <mailto:P@ssw0rd>" -AsPlainText -Force))
Add-Computer -DomainName "coex.com" -Credential $credential -passthru
restart-computer


%d bloggers like this: