Author Posts

September 15, 2015 at 5:47 am

I've been having problems with this script, can you guys give me a hand here (i'm a total newbie)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName system.drawing
import-module ServerManager
$before = $true
$folderpath = ""
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Server Patching Checks'
$form.Size = New-Object system.drawing.size(300,300)
$form.StartPosition = "Centerscreen"
Function FNProcData {
$mydialog = New-Object System.Windows.Forms.OpenFileDialog
$mydialog.Title = "Please select a file"
$mydialog.ShowHelp = $true
$res = $mydialog.ShowDialog()
if($res -eq "ok"){
$textbox.text = $mydialog.FileName
$folderpath = Split-Path $textbox.text
Set-Location $folderpath
}#if15
}#function10

$groupbox = New-Object System.Windows.Forms.GroupBox
$groupbox.Location = New-Object System.Drawing.Size(10,100)
$groupbox.Size = New-Object System.Drawing.Size(200,70)
$groupbox.Text = "Before/after Action"
$form.Controls.Add($groupbox)

$RadioButton1 = New-Object System.Windows.Forms.RadioButton
$RadioButton1.Location = New-Object System.Drawing.Point(10,15)
$RadioButton1.size = New-Object System.Drawing.Size(180,20)
$RadioButton1.Checked = $true
$RadioButton1.text = "Before Patching"
$groupbox.Controls.Add($RadioButton1)

$RadioButton2 = New-Object System.Windows.Forms.RadioButton
$RadioButton2.Location = New-Object System.Drawing.Point(10,40)
$RadioButton2.size = New-Object System.Drawing.Size(180,20)
$RadioButton2.text = "After Patching"
$groupbox.Controls.Add($RadioButton2)

$browsebutton = New-Object System.Windows.Forms.Button
$browsebutton.Location = New-Object System.Drawing.Point(200,40)
$browsebutton.size = New-Object System.Drawing.Size(75,23)
$browsebutton.text = "Browse"
$browsebutton.add_click({FNProcData})
$form.Controls.Add($browsebutton)

$okbutton = New-Object System.Windows.Forms.Button
$okbutton.Location = New-Object System.Drawing.Point(75,220)
$okbutton.size = New-Object System.Drawing.Size(75,23)
$okbutton.text = "OK"
$okbutton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okbutton
$form.Controls.Add($okbutton)

$cancelbutton = New-Object System.Windows.Forms.Button
$cancelbutton.Location = New-Object System.Drawing.Point(150,220)
$cancelbutton.size = New-Object System.Drawing.Size(75,23)
$cancelbutton.text = "Cancel"
$cancelbutton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.AcceptButton = $cancelbutton
$form.Controls.Add($cancelbutton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.size = New-Object System.Drawing.Size(280,20)
$label.Text = "Please enter the server list file location"
$form.controls.Add($label)

$textbox = New-Object System.Windows.Forms.TextBox
$textbox.location = New-Object System.Drawing.Point(10,40)
$textbox.size = New-Object System.Drawing.Size(180,20)
$form.Controls.Add($textbox)

$form.TopMost = $true

$form.add_Shown({$textbox.Select()})
$result = $form.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK){
$servers = Get-Content $textbox.Text
if($radiobutton2.Checked -eq $true) {
$before = $false
$warning = @()
foreach ($server in $servers){
$badservices = @()

$services = Get-WmiObject win32_service -ComputerName $server | select name,displayname,startmode,state,Status

$SCCMaction = Invoke-WmiMethod -ComputerName $server -Namespace root\ccm -class sms_client -name triggerschedule "{00000000-0000-0000-0000-000000000113}"
foreach($service in $services){
if($service.startmode -match "Auto" -and $service.state -notlike "Running"){
$badservices+=$service.displayname
}#if96
}#foreach95

$x = Get-Content .\$($server + ".csv")

$x = $x[1..$x.count]
$x = $x | ForEach-Object {$_.replace('"',")}
$diff = Compare-Object $badservices $x | where {$_.sideindicator -eq '=>'}
$w = $diff | where {$_.Inputobject -ne $null} | measure
if($w.count -gt 0){
if(!(Test-Path -Path .\warning)){New-Item -ItemType directory -Path .\warning\}
$diff.inputobject | Out-File .\warning\$($server + ".csv")
$warning += $server
}else{
if((Test-Path -Path .\warning\$($server + ".csv"))){
Remove-Item -Path .\warning\$($server + ".csv") -Force
}#if114
}#else113
}#foreach89
if($warning.count -gt 0){$warning | Out-GridView -Title "Please check the associated output files"}
}else{
foreach ($server in $servers){
$badservices = @()
$services = Get-WmiObject win32_service -ComputerName $server | select name,displayname,startmode,state,Status
$SCCMaction = Invoke-WmiMethod -ComputerName $server -Namespace root\ccm -class sms_client -name triggerschedule "{00000000-0000-0000-0000-000000000108}"
foreach($service in $services){
if($service.startmode -match "Auto" -and $service.state -notlike "Running"){
$badservices+=$Item
$item =$null
}#if126
}#foreach125

$badservices | Export-Csv -NoTypeInformation .\$($server+".csv")
$SCCMaction = Invoke-WmiMethod nvoke-WmiMethod -ComputerName $server -Namespace root\ccm -class sms_client -name triggerschedule "{00000000-0000-0000-0000-000000000108}"
}#foreach121
}#else120
}#if84

September 15, 2015 at 6:45 am

What is not working ?
Did you get an error ?
How are you running it ?
What are you looking to achieve from this script ?

Thanks !