Hey guys, What I want to do is hopefully simple. It's 2 things actually that should be simple.
1. Under the Remote Desktop function I want to do a check to see if fDenyTSConnections is already set to 0 and if it is, report it to the text box you'll see at the right if you run the code. Maybe make it say "fDenyTSConnections is already set to 0" If it isn't, then of course, set it to 0. As you'll notice I already have it setting it to 0 and it does work. But I want it to be part of a check and only run if needed.
2. Under Restart computer, I would like to have the computer be sent the shutdown command, and then in the text box report to me the following;
1. <computer> will be restarted in 60 Seconds>
2. <computer> is now restarting
3. Lost Ping with <computer>
4. Regained ping with <computer>
I am pretty sure I can use test-connection to accomplish this but not sure how to implement it. So without further adue, here is my script, any help would be greatly apprecated. BTW it's my first script, so also any suggestions to improve the code would be great as well.
# Written by Tim Brown import-module activedirectory [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null $form1 = New-Object System.Windows.Forms.Form $button1 = New-Object System.Windows.Forms.Button $button2 = New-Object System.Windows.Forms.Button $listBox1 = New-Object System.Windows.Forms.ListBox $objTextBox = New-Object System.Windows.Forms.TextBox $objLabel = New-Object System.Windows.Forms.Label $checkBox2 = New-Object System.Windows.Forms.CheckBox $checkBox1 = New-Object System.Windows.Forms.CheckBox $checkBox3 = New-Object System.Windows.Forms.CheckBox $checkBox4 = New-Object System.Windows.Forms.CheckBox $checkBox5 = New-Object System.Windows.Forms.CheckBox $checkBox6 = New-Object System.Windows.Forms.CheckBox $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState $global:DD = "c:\users\public\documents\temp.txt" $b1= $false $b2= $false $b3= $false $b4= $False $b5= $False $b6= $False function FixWindowsUpdate { $Cmpter = Get-Content $DD $cmdline = "\\$Cmpter\c$\windows\SoftwareDistribution" $listBox1.Items.Add("Stopping Windows Update Service") Stop-Service -inputobject $(Get-Service -computername $Cmpter -name wuauserv) Start-Sleep -s 3 $listBox1.Items.add("Deleting $cmdline ...") Remove-Item $cmdline -recurse Start-Sleep -s 2 $ListBox1.Items.Add("Starting Windows Update Service") Start-Service -inputobject $(get-service -computername $Cmpter -name wuauserv) Start-Sleep -s 3 $ListBox1.Items.Add("Done") } function fixGroupPolicy { $Cmpter = Get-Content $DD $cmdline = "ren \\$Cmpter\c$\windows\System32\GroupPolicy\machine\registry.pol registry.bak" Invoke-Expression $cmdline $ListBox1.Items.Add("Renaming \\$Cmpter\c$\Windows\system32\GroupPolicy\machine\registry.pol to registry.bak") Start-Sleep -s 3 $cmdline = "ren \\$Cmpter\c$\windows\System32\GroupPolicy\user\registry.pol registry.bak" Invoke-Expression $cmdline $ListBox1.Items.Add("Renaming \\$Cmpter\c$\Windows\system32\GroupPolicy\user\registry.pol to registry.bak") Start-Sleep -s 3 remove-item -path "\\$Cmpter\c$\ProgramData\Microsoft\Group Policy\History" -Recurse -force $ListBox1.Items.Add("Deleting \\$Cmpter\c$\ProgramData\Microsoft\Group Policy\History") Start-Sleep -s 3 $ListBox1.Items.Add("Running GPUpdate /Force") $cmdline = "c:\windows\pstools\psexec.exe /acceptEula \\$Cmpter gpupdate.exe /force" Invoke-expression $cmdline $ListBox1.Items.Add("Done, Please run the restart computer function of this script") } function ResetComputerRepository { $Cmpter = Get-Content $DD $listBox1.Items.Add("Stopping WMI Service") Stop-Service -inputobject $(Get-Service -computername $Cmpter -name winmgmt) -force Start-Sleep -s 3 $cmdline = "ren \\$Cmpter\c$\windows\System32\wbem\Repository \\$Cmpter\c$\windows\System32\wbem\Repositoryold" $listBox1.Items.add("Rename $cmdline to RepositoryOld...") Invoke-expression $cmdline Start-Sleep -s 3 $ListBox1.Items.Add("Starting WMI Service") Start-Service -inputobject $(get-service -computername $Cmpter -name winmgmt) Start-Sleep -s 3 $ListBox1.Items.Add("Done") } function EnableRemoteDesktop { $Cmpter = Get-Content $DD Set-RegDWord -Data 0 -Key "System\CurrentControlSet\Control\Terminal Server" -Value fDenyTSConnections -ComputerName $Cmpter -Force -Hive LocalMachine $listBox1.Items.Add( "Remote Desktop has been enabled, Please run the Restart Computer function of this script.") } function RestartComputer { $Cmpter = Get-Content $DD $cmdline = "shutdown.exe /r /m \\$Cmpter /t 60 /c `"This Computer will restart in 60 Seconds to apply system changes`"" Invoke-Expression $cmdline $listBox1.Items.Add( "$Cmpter will be restarted in 60 Seconds") } Function OSD { $listBox1.Items.Add( "This function is disabled until further notice.") } #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- $handler_button1_Click = { $listBox1.Items.Clear(); if ($checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked -and !$checkBox5.Checked -and !$checkBox6.Checked) {FixWindowsUpdate} if ($checkBox2.Checked -and !$checkBox1.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked -and !$checkBox5.Checked -and !$checkBox6.Checked) {fixGroupPolicy} if ($checkBox3.Checked -and !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox4.Checked -and !$checkBox5.Checked -and !$checkBox6.Checked) {ResetComputerRepository} if ($checkBox4.Checked -and !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox5.Checked -and !$checkBox6.Checked) {EnableRemoteDesktop} if ($checkBox5.Checked -and !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked -and !$checkBox6.Checked) {RestartComputer} if ($checkBox6.Checked -and !$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked -and !$checkBox5.Checked) {OSD} if (!$checkBox1.Checked -and !$checkBox2.Checked -and !$checkBox3.Checked -and !$checkBox4.Checked -and !$checkBox5.Checked -and !$checkBox6.Checked) {$listBox1.Items.Add("No CheckBox selected....")} } $handler_button2_Click = { $listBox1.Items.Clear(); $Computer = $ObjTextBox.text "$Computer" > $DD if ($Computer -ne "") { $info = Get-ADComputer $computer -Properties ipv4address, DistinguishedName $Username = Get-WmiObject win32_computersystem -computer $computer | select username $listBox1.Items.Add($info.name) $listBox1.Items.Add($info.IPv4Address) $listBox1.Items.Add($info.DistinguishedName) $listBox1.Items.Add("$Username") } } #Correct the initial state of the form to prevent the .Net maximized form issue $OnLoadForm_StateCorrection = { $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code #Draws GUI Box and Labels it $form1.Text = "Tim's Remediation Tool" $form1.Name = "form1" $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Size = New-Object System.Drawing.Size (1000,420) $form1.ClientSize = $System_Drawing_Size #Draws output Box $listBox1.FormattingEnabled = $True $System_Drawing_Size = New-Object System.Drawing.Size (780,380) $listBox1.Size = $System_Drawing_Size $listBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $listBox1.Name = "listBox1" $System_Drawing_Point = New-Object System.Drawing.Point (200,13) $listBox1.Location = $System_Drawing_Point $listBox1.TabIndex = 3 $form1.Controls.Add($listBox1) #Places Please Enter The Computer Name text above Input Box $objLabel.Location = New-Object System.Drawing.Size(27,13) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Enabled=$True $objLabel.Text = "Please enter the Computer Name:" $form1.Controls.Add($objLabel) #Draws the Check Computer Button $button2.TabIndex = 4 $button2.Name = "button2" $System_Drawing_Size = New-Object System.Drawing.Size (100,23) $button2.Size = $System_Drawing_Size $button2.UseVisualStyleBackColor = $True $button2.Text = "Check Computer" $System_Drawing_Point = New-Object System.Drawing.Point (27,60) $button2.Location = $System_Drawing_Point $button2.DataBindings.DefaultDataSourceUpdateMode = 0 $button2.add_Click($handler_button2_Click) $form1.Controls.Add($button2) #Draws Input box for Computer Name Entry $ObjTextBox.Location = New-Object System.Drawing.Point (27,35) $objTextBox.Size = New-Object System.Drawing.Size(150,20) $objTextBox.TabIndex = 0 $form1.Controls.Add($objTextBox) #Draws Fix Winodws Update button and labels it (First Menu Option) $checkBox1.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (150,30) $checkBox1.Size = $System_Drawing_Size $checkBox1.Text = "Fix Windows Update" $System_Drawing_Point = New-Object System.Drawing.Point (27,91) $checkBox1.Location = $System_Drawing_Point $checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox1.Name = "checkBox1" $form1.Controls.Add($checkBox1) #Draws Fix Group Policy button and labels it (2nd Menu Option) $checkBox2.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (108,24) $checkBox2.Size = $System_Drawing_Size $checkBox2.Text = "Fix Group Policy" $System_Drawing_Point = New-Object System.Drawing.Point (27,122) $checkBox2.Location = $System_Drawing_Point $checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox2.Name = "checkBox2" $form1.Controls.Add($checkBox2) #Draws Reset Computer Repository and labels it (3rd Menu Option) $checkBox3.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (280,20) $checkBox3.Size = $System_Drawing_Size $checkBox3.Text = "Reset Computer Repository" $System_Drawing_Point = New-Object System.Drawing.Point (27,153) $checkBox3.Location = $System_Drawing_Point $checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox3.Name = "checkBox3" $form1.Controls.Add($checkBox3) #Draws Enable Remote Desktop and labels it (4th Menu Option) $checkBox4.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (280,20) $checkBox4.Size = $System_Drawing_Size $checkBox4.Text = "Enable Remote Desktop" $System_Drawing_Point = New-Object System.Drawing.Point (27,184) $checkBox4.Location = $System_Drawing_Point $checkBox4.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox4.Name = "checkBox4" $form1.Controls.Add($checkBox4) #Draws Restart Computer and labels it (5th Menu Option) $checkBox5.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (280,20) $checkBox5.Size = $System_Drawing_Size $checkBox5.Text = "Restart Computer" $System_Drawing_Point = New-Object System.Drawing.Point (27,215) $checkBox5.Location = $System_Drawing_Point $checkBox5.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox5.Name = "checkBox5" $form1.Controls.Add($checkBox5) #Draws OSD This Computer and labels it (6th Menu Option) $checkBox6.UseVisualStyleBackColor = $True $System_Drawing_Size = New-Object System.Drawing.Size (280,20) $checkBox6.Size = $System_Drawing_Size $checkBox6.Text = "OSD This Computer" $System_Drawing_Point = New-Object System.Drawing.Point (27,277) $checkBox6.Location = $System_Drawing_Point $checkBox6.DataBindings.DefaultDataSourceUpdateMode = 0 $checkBox6.Name = "checkBox6" $form1.Controls.Add($checkBox6) #Draws the Run Script Button $button1.TabIndex = 4 $button1.Name = "button1" $System_Drawing_Size = New-Object System.Drawing.Size (75,23) $button1.Size = $System_Drawing_Size $button1.UseVisualStyleBackColor = $True $button1.Text = "Run Script" $System_Drawing_Point = New-Object System.Drawing.Point (27,246) $button1.Location = $System_Drawing_Point $button1.DataBindings.DefaultDataSourceUpdateMode = 0 $button1.add_Click($handler_button1_Click) $form1.Controls.Add($button1) #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