Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################################################################
- #
- #
- # The script's intended purpose is to completely un-install and re-install the SCCM Client.
- # Written By: Chris Beattie
- #
- ###########################################################################################
- If ($PSVersionTable.PSVersion.Major -gt 3) {Write-Host "You have PowerShell Version 4 or higher - you should be fine to run this script"}
- elseif ($PSVersionTable.PSVersion.Major -eq 3) {Write-Host "You have PowerShell Version 3 - you should be fine to run this script"}
- elseif ($PSVersionTable.PSVersion.Major -eq 2) {Write-Host "You have PowerShell Version 2 - you may have issues running this script"}
- elseif ($PSVersionTable.PSVersion.Major -eq 1) {Write-Host "You have PowerShell Version 1 - time to upgrade this script is not going to work for you"}
- Start-sleep -seconds 2
- # define working directory of the script based on powershell version that is hosting the script
- if($PSVersionTable.PSVersion.Major -lt 3){$WorkingDirectory = split-path -parent $MyInvocation.MyCommand.Definition}
- else{$WorkingDirectory = $PSScriptRoot}
- # define variables
- $Domain = $env:USERDNSDOMAIN
- $WriteHost = $True
- $inputfile = "$WorkingDirectory\Input\servers.txt"
- $logfolder = "$WorkingDirectory\Output\"
- $Trace32 = "$WorkingDirectory\Trace32"
- $timestamp = Get-Date -format "yyyy_MM_dd_HHmmss"
- $resultfile = $logfolder + "SCCM_Client_Reinstall_" + $Domain + "_" + $item + "_" + $timestamp + ".csv"
- # Define services we will start and stop
- $bits = "bits"
- $wuauserv = "wuauserv"
- $appidsvc = "appidsvc"
- $cryptsvc = "cryptsvc"
- $ccmexec = "ccmexec"
- # set PSexec path..
- Set-Alias psexec "$WorkingDirectory\psexec\psexec.exe"
- # call out which domain we're running this script on based on environment variable.
- Write-Host "#------------------------------------------------------------#"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") "Running script in Domain: $Domain"
- Write-Host "#------------------------------------------------------------#"
- # If log path doesn't exist create it..
- If (!(Test-Path -Path "$logfolder" -ErrorAction SilentlyContinue)) { New-Item "$logfolder" -Type Directory -ErrorAction SilentlyContinue | Out-Null }
- # define 'functions' of the script
- Function Add_To_Log {
- Process {$_ | Add-Content -Path $resultfile}
- }
- Function Remove-CCMNamespace {
- # If it exists:
- if (get-wmiobject -query "Select * FROM __Namespace WHERE Name='ccm'" -Namespace "root" -Computername $computer)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Found ccm Namespace"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Deleting ccm Namespace"
- # Delete
- get-wmiobject -query "Select * FROM __Namespace WHERE Name='ccm'" -Namespace "root" -Computername $computer | Remove-WMIobject
- Start-sleep -seconds 2
- # Check Delete success/fail
- $Status = Get-wmiobject -query "Select * FROM __Namespace WHERE Name='ccm'" -Namespace "root" -computername $Computer
- if ($Status) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red "Failed to delete ccm namespace WMI ISSUE"
- Write-Output "$Computer, Failed to Delete CCM namespace, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully removed ccm namespace"}
- }
- Else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "ccm namespace does not exist"
- }
- }
- Function Remove-SMSNamespace {
- if (get-wmiobject -query "Select * FROM __Namespace WHERE Name='sms'" -Namespace "root\cimv2" -Computername $computer) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Found SMS Namespace"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Deleting SMS Namespace"
- get-wmiobject -query "Select * FROM __Namespace WHERE Name='sms'" -Namespace "root\cimv2" -Computername $computer | Remove-WMIobject
- Start-sleep -seconds 2
- $Status = Get-wmiobject -query "Select * FROM __Namespace WHERE Name='sms'" -Namespace "root\cimv2" -computername $Computer
- if ($Status) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "RED" "Failed to delete SMS namespace WMI ISSUES"
- Write-Output "$Computer, Failed to Delete SMS namespace, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Successfully removed SMS namespace"}
- }
- Else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "SMS namespace does not exist"
- }
- }
- Function Verify-WMI {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Item "Running WMI Consistency Check winmgmt /verifyrepository"
- $computername = "\\$item"
- $Scriptblock = "winmgmt /verifyrepository"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- if ($output -eq "WMI repository is consistent")
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Item "WMI Check: Success"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") $Output
- Write-Output "$Item, WMI Consistency Check, success,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- $SuccessfulResults += $Item
- return $True
- }
- else
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Item "WMI Check: Failure"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") $Output
- Write-Output "$Item, WMI Consistency Check, Failed,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- }
- #Function Remove-WUSoftwareDistributionFolder {
- #$SoftwareDistributionPath = "\\$Computer\C$\Windows\SoftwareDistribution"
- #
- # Write-Output ("{0}, Removing Software Distribution folder and subfolders" -f $Computer) | Add_To_Log
- # Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing Software Distribution folder and subfolders"
- #
- # if (test-path -path "$SoftwareDistributionPath")
- # {Try
- # {
- # $files = Get-ChildItem -file -literalpath $SoftwareDistributionPath -Recurse -force
- # $filecount = $files.count
- # $iteration = 0
- # Foreach ($file in $files) {
- # $iteration++
- # Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- # Remove-Item $file.fullname -recurse -force
- # }
- # Write-Progress -Completed -activity "Removing..."
- # Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- # Remove-item -literalPath $SoftwareDistributionPath -recurse -force
- #
- # Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $SoftwareDistributionPath exists"
- # Start-sleep -seconds 2
- #
- # if (test-path -literalpath $SoftwareDistributionPath) {
- # Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $SoftwareDistributionPath"
- # } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $SoftwareDistributionPath"}
- # } Catch {
- # Write-Output ("{0}, {1} ERROR" -f $Computer,$_.Exception.Message) | Add_To_Log
- # Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- # }
- # }
- #}
- function Rename-WUSoftwareDistributionFolder {
- $Path = "\\$Computer\C$\Windows\SoftwareDistribution"
- $NewName = "SoftwareDistribution" + "_" + $timestamp + ".old"
- $NewPath = "\\$Computer\C$\Windows\$NewName"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting to rename SoftwareDistribution Directory"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor white "from:"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "$Path"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor white "to:"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "$NewPath"
- Rename-item $Path -NewName $NewName
- Start-sleep -seconds 2
- if (test-path $NewPath) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully renamed to: $NewName"
- # if new path exists... means we've likely successfully renamed directory
- }
- if (!(test-path $path)) {
- # AND original path no longer exists
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "$Path no longer exists"
- }
- # else since new path doesn't exist unlikely that original path has been renamed as it should...
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "$Path still exists!"
- }
- }
- Function Remove-Catroot2Folder {
- $catroot2Path = "\\$computer\C$\windows\syswow64\catroot2"
- $catroot2x86 = "\\$computer\C$\windows\system32\catroot2"
- if (test-path -path $catroot2Path)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $catroot2Path"
- $files = Get-ChildItem -file -literalpath $catroot2Path -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $catroot2Path -recurse -force
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $catroot2Path exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $catroot2Path) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $catroot2Path"
- Write-Output ("$Computer, Failed to Delete $Catroot2Path, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss"),{0}" -f $_.ExceptionMessage) | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $catroot2Path"}
- } Catch {
- Write-Output ("$Computer, Failed to Delete $Catroot2Path, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss"),{0}" -f $_.ExceptionMessage) | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer $_.Exception.Message
- }
- }
- # moving onto x86 path:
- if (test-path -path $catroot2x86)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $catroot2x86"
- $files = Get-ChildItem -file -literalpath $catroot2x86 -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $catroot2x86 -recurse -force
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $catroot2x86 exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $catroot2x86) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $catroot2x86"
- Write-Output "$Computer, Failed to Delete $catroot2x86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $catroot2x86"}
- } Catch {
- Write-Output ("$Computer, Failed to Delete $catroot2x86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss"),{0}" -f $_.ExceptionMessage) | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- }
- Function Remove-CCMFolder {
- $ccmPath = "\\$computer\C$\windows\syswow64\ccm"
- $ccmPathx86 = "\\$computer\C$\windows\system32\ccm"
- if (test-path -path $ccmPath)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmPath"
- $files = Get-ChildItem -file -literalpath $ccmPath -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmPath -recurse -force
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmPath exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmPath) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $ccmPath"
- Write-Output "$Computer, Failed to Delete $ccmPath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmPath"}
- } Catch {
- Write-Output "$Computer, Failed to Delete $ccmpath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- # moving onto x86 path:
- if (test-path -path $ccmPathx86)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmPathx86"
- $files = Get-ChildItem -file -literalpath $ccmPathx86 -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmPathx86 -recurse -force
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmPathx86 exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmPathx86) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $ccmPathx86"
- Write-Output "$Computer, Failed to Delete $ccmPathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmPathx86"}
- } Catch {
- Write-Output "$Computer, Failed to Delete $ccmPathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- }
- Function Remove-CCMCacheFolder {
- $ccmcachePath = "\\$Computer\C$\windows\syswow64\ccm\cache"
- $ccmcachePathx86 = "\\$Computer\c$\Windows\system32\ccm\cache"
- if (test-path -path $ccmcachePath)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmcachePath"
- $files = Get-ChildItem -file -literalpath $ccmcachePath -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmcachePath -recurse -force
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmcachePath exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmcachePath) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $ccmcachePath"
- Write-Output "$Computer, Failed to Delete $ccmcachepath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmcachePath"}
- } Catch {
- Write-Output "$Computer, Failed to Delete $ccmcachepath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- # moving onto x86 path:
- if (test-path -path $ccmcachePathx86)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmcachePathx86"
- $files = Get-ChildItem -file -literalpath $ccmcachePathx86 -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmcachePathx86 -recurse -force
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmcachePathx86 exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmcachePathx86) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $ccmcachePathx86"
- Write-Output "$Computer, Failed to Delete $ccmcachepathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmcachePathx86"}
- } Catch {
- Write-Output "$Computer, Failed to Delete $ccmcachepathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- }
- Function Remove-CCMSetupFolder {
- $ccmsetupPath = "\\$Computer\C$\windows\ccmsetup"
- $ccmsetupPathx86 = "\\$computer\C$\windows\system32\ccmsetup"
- if (test-path -path $ccmsetupPath)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmsetupPath"
- $files = Get-ChildItem -file -literalpath $ccmsetupPath -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmsetupPath -recurse -force
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmsetupPath exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmsetupPath) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete $ccmsetupPath"
- Write-Output "$Computer, Failed to Delete $ccmsetupPath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmsetupPath"}
- } Catch {
- Write-Output "$Computer, Failed to Delete $ccmsetupPath, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- # moving onto x86 path:
- if (test-path -path $ccmsetupPathx86)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing $ccmsetupPathx86"
- $files = Get-ChildItem -file -literalpath $ccmsetupPathx86 -Recurse -force
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "Done with foreach, moving to delete the whole directory and empty folder structure"
- Remove-item -literalPath $ccmsetupPathx86 -recurse -force
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $ccmsetupPathx86 exists"
- Start-sleep -seconds 2
- if (test-path -literalpath $ccmsetupPathx86) {
- Write-Output "$Computer, Found $ccmsetupPathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted $ccmsetupPathx86"}
- } Catch {
- Write-Output "$Computer, Found $ccmsetupPathx86, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- }
- Function Remove-qmgrdatfiles {
- $qmgrdatxpPath = "\\$Computer\c$\Documents and Settings\All Users\Application Data\Microsoft\Network\Downloader"
- $qmgrdatwin7Path = "\\$Computer\C$\ProgramData\Microsoft\Network\Downloader"
- if (test-path -path $qmgrdatwin7Path)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing qmgr*.dat files in $qmgrdatwin7Path"
- $files = Get-ChildItem -file -literalpath $qmgrdatwin7Path -Recurse -force | where {$_.Name -like "qmgr*.dat"}
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $files exist in $qmgrdatwin7Path"
- Start-sleep -seconds 2
- # if files exist then delete failed
- if (Get-Childitem -literalpath $qmgrdatwin7Path -file | where {$_.Name -like "qmgr*.dat"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete qmgr*.dat files"
- Write-Output "$Computer, Failed to Delete qmgr*.dat files, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- # if files don't exist in that location delete was successful.
- else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted qmgr*.dat files"}
- }
- #catch any errors in this process
- Catch {
- Write-Output "$Computer, Failed to Delete $qmgrdatwin7Path, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- # moving onto XP path:
- if (test-path -path $qmgrdatxpPath)
- {Try
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Removing qmgr*.dat files in $qmgrdatxpPath"
- $files = Get-ChildItem -file -literalpath $qmgrdatxpPath -Recurse -force | where {$_.Name -like "qmgr*.dat"}
- $filecount = $files.count
- $iteration = 0
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Removing..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- Remove-Item $file.fullname -recurse -force
- }
- Write-Progress -Completed -activity "Removing..."
- # test to see if the delete actually worked
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "checking to see if $files exist in $qmgrdatxpPath"
- Start-sleep -seconds 2
- if (Get-Childitem -literalpath $qmgrdatxpPath -file | where {$_.Name -like "qmgr*.dat"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer "Failed to Delete qmgr*.dat files"
- Write-Output "$Computer, Failed to Delete qmgr*.dat files, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- } else {Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Deleted qmgr*.dat files"}
- } Catch {
- Write-Output "$Computer, Failed to Delete qmgr*.dat files, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" ($Computer,$_.Exception.Message)
- }
- }
- }
- Function Force-WUAgentDetectnow {
- $computername = "\\$computer"
- $Scriptblock = "wuauclt /resetauthorization /detectnow"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## run the scriptblock:
- if ($output -eq $null) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $computer "Successfully sent wuauclt /detectnow /resetauthorization"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Something went wrong sending wuauclt /detectnow /resetauthorization"
- Write-Output "$Computer, Something went wrong wuauclt detectnow not sent or received properly.. not so much an issue.. WUAgent should recover by itself in time, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- Function Copy-SCCMClientInstallation {
- $destinationparentdir = "\\$Computer\C$\temp"
- $sourcePath = "$WorkingDirectory\Client"
- $destinationPath = "\\$Computer\C$\Temp\SCCMClientInstall\Client"
- # if c:\temp on target machine doesn't exist, create it
- if (!(Test-path $destinationparentdir)) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") "Creating Directory" -foregroundcolor "green" $destinationparentdir
- New-Item "$destinationparentdir" -Type Directory -ErrorAction SilentlyContinue | Out-Null }
- # if we haven't already copied files to target machine do so.
- if (!(Test-path $destinationPath)) {
- $files = Get-ChildItem -Path $sourcePath -Recurse
- $filecount = $files.count
- $iteration = 0
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -Foregroundcolor "Yellow" $Computer "Copying from $SourcePath to $DestinationPath"
- Foreach ($file in $files) {
- $iteration++
- Write-Progress -activity "Copying..." -status "$file ($iteration of $filecount)" -percentcomplete (($iteration/$filecount)*100)
- # Determine the absolute path of this object's parent container. This is stored as a different attribute on file and folder objects so we use an if block to cater for both
- if ($file.psiscontainer) {$sourcefilecontainer = $file.parent} else {$sourcefilecontainer = $file.directory}
- # Calculate the path of the parent folder relative to the source folder
- $relativepath = $sourcefilecontainer.fullname.SubString($sourcepath.length)
- # Copy the object to the appropriate folder within the destination folder
- Copy-Item $file.fullname ($destinationPath + $relativepath)
- }
- Write-Progress -Completed -activity "Copying..."
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -Foregroundcolor "yellow" $Computer "SCCM Client installation files already exist on this machine, suggests script has run before"
- }
- }
- Function LoadCCMsetupLog {
- if (test-path -path \\$Computer\C$\windows\ccmsetup) {Start $Trace32\Trace32.exe \\$Computer\C$\Windows\ccmsetup\ccmsetup.log}
- elseif (test-path -path \\$Computer\C$\windows\system32\ccmsetup) {Start $Trace32\Trace32.exe \\$Computer\C$\windows\system32\ccmsetup\ccmsetup.log}
- elseif (test-path -path \\$Computer\C$\winnt) {Start $Trace32\Trace32.exe \\$Computer\C$\winnt\system32\ccmsetup\ccmsetup.log}
- }
- Function Uninstall-SCCMClient {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Yellow" $Computer "Running SCCM Client Uninstall"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Computer Opening Log
- LoadCCMSetupLog
- if (test-path -path "\\$Computer\C$\windows\syswow64") {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Computer "Detected x64 running SCCM Uninstall"
- psexec \\$computer C:\temp\SCCMClientInstall\Client\ccmsetup.exe /uninstall
- }
- elseif (test-path -path "\\$Computer\C$\windows\system32") {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $computer "Detected x86 running SCCM Uninstall"
- psexec \\$computer C:\temp\SCCMClientInstall\Client\ccmsetup.exe /uninstall
- }
- Start-sleep -seconds 7
- Get-process Trace32 | stop-process
- }
- Function Install-SCCMClient {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Beginning SCCM Client Reinstall"
- psexec \\$computer C:\temp\SCCMClientInstall\Client\ccmsetup.exe /mp:melysccm1.oceania.cshare.net SMSSITECODE=O01 SMSSLP=melysccm1.oceania.cshare.net CCMLOGLEVEL=0 CCMLOGMAXHISTORY=3 RESETKEYINFORMATION=TRUE FSP=melysccm1.oceania.cshare.net
- Start-sleep -seconds 2
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Computer Opening Log
- LoadCCMSetupLog
- CheckifCCMSetupStillRunning
- }
- Function CheckifCCMsetupStillrunning {
- Start-sleep -seconds 1
- if (Get-process -ComputerName $Computer ccmsetup) {
- do {
- $Process = Get-Process -ComputerName $Computer | where {$_.ProcessName -eq "ccmsetup"}
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow "ccmsetup.exe still running on $Computer - please be patient"
- Start-Sleep -Seconds 5
- } until ($Process -eq $null)
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") ("{0}: CCMsetup.exe has exited" -f $Computer)
- }
- }
- Function Confirm-BitsExists {
- if (Get-Service -Name $bits -Computername $Computer -ErrorAction SilentlyContinue)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "$bits Exists"
- return $true
- }
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "$bits does NOT exist on this computer"
- Write-Output "$Computer, $bits service does NOT exist, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- Function Confirm-WuauservExists {
- if (Get-Service -Name $wuauserv -Computername $Computer -ErrorAction SilentlyContinue)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "$wuauserv Exists"
- return $true
- }
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "$wuauserv does NOT exist on this computer"
- Write-Output "$Computer, $wuauserv service does not exist, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- Function Confirm-AppIDsvcExists {
- if (Get-Service -Name $appidsvc -Computername $Computer -ErrorAction SilentlyContinue)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "$appidsvc Exists"
- return $true
- }
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "$appidsvc does NOT exist on this computer"
- Write-Output "$Computer, $appidsvc does NOT exist, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- Function Confirm-cryptsvcExists {
- if (Get-Service -Name $cryptsvc -Computername $Computer -ErrorAction SilentlyContinue)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "$cryptsvc Exists"
- return $true
- }
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "$cryptsvc does NOT exist on this computer"
- Write-Output "$Computer, $cryptsvc does NOT exist, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- Function Confirm-ccmexecExists {
- if (Get-Service -Name $ccmexec -Computername $Computer -ErrorAction SilentlyContinue)
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "$ccmexec Exists"
- return $true
- }
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "$ccmexec does NOT exist on this computer"
- Write-Output "$Computer, $ccmexec service does NOT exist, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- return $false
- }
- Function Stop-BitsIfItExists {
- $bitsexists = Confirm-BitsExists $bitsexists
- if ($bitsexists) {
- $bitsstatus = Get-Service -computername $Computer -name $bits
- if ($bitsstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $bits | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) CONFIG $bits start= disabled
- Start-sleep -seconds 2
- SC.exe ("\\" + $Computer) Stop $bits
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $bits service"
- start-sleep 20
- $bitsstatus2 = Get-Service -computername $Computer -name $bits
- if ($bitsstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$bits"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$bits"
- Write-Output "$Computer, $bits Failed to Stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($bitsstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$bits already stopped"
- }
- }
- }
- Function Stop-WuauservIfItExists {
- $wuauservexists = Confirm-WuauservExists $wuauservexists
- if ($wuauservexists) {
- $wuauservstatus = Get-Service -computername $Computer -name $wuauserv
- if ($wuauservstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $wuauserv | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) CONFIG $wuauserv start= disabled
- Start-sleep -seconds 2
- SC.exe ("\\" + $Computer) Stop $wuauserv
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $wuauserv service"
- start-sleep 20
- $wuauservstatus2 = Get-Service -computername $Computer -name $wuauserv
- if ($wuauservstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$wuauserv"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$wuauserv"
- Write-Output "$Computer, $wuauserv Failed to Stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($wuauservstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$wuauserv already stopped"
- }
- }
- }
- Function Stop-appidsvcIfItExists {
- $appidsvcexists = Confirm-appidsvcExists $appidsvcexists
- if ($appidsvcexists) {
- $appidsvcstatus = Get-Service -computername $Computer -name $appidsvc
- if ($appidsvcstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $appidsvc | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) CONFIG $appidsvc start= disabled
- Start-sleep -seconds 2
- SC.exe ("\\" + $Computer) Stop $appidsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $appidsvc service"
- start-sleep 20
- $appidsvcstatus2 = Get-Service -computername $Computer -name $appidsvc
- if ($appidsvcstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$appidsvc"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$appidsvc"
- Write-Output ("{0}, $appidsvc Failed to stop" -f $Computer) | Add_To_Log
- Write-Output "$Computer, $appidsvc Failed to Stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($appidsvcstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$appidsvc already stopped"
- }
- }
- }
- Function Stop-cryptsvcIfItExists {
- $cryptsvcexists = Confirm-cryptsvcExists $cryptsvcexists
- if ($cryptsvcexists) {
- $cryptsvcstatus = Get-Service -computername $Computer -name $cryptsvc
- if ($cryptsvcstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $cryptsvc | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) CONFIG $cryptsvc start= disabled
- Start-sleep -seconds 2
- SC.exe ("\\" + $Computer) Stop $cryptsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $cryptsvc service"
- start-sleep 20
- $cryptsvcstatus2 = Get-Service -computername $Computer -name $cryptsvc
- if ($cryptsvcstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$cryptsvc"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$cryptsvc"
- Write-Output "$Computer, $cryptsvc Failed to Stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($cryptsvcstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$cryptsvc already stopped"
- }
- }
- }
- Function Stop-ccmexecIfItExists {
- $ccmexecexists = Confirm-ccmexecExists $ccmexecexists
- if ($ccmexecexists) {
- $ccmexecstatus = Get-Service -computername $Computer -name $ccmexec
- if ($ccmexecstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $ccmexec | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) Stop $ccmexec
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $ccmexec service"
- start-sleep 20
- $ccmexecstatus2 = Get-Service -computername $Computer -name $ccmexec
- if ($ccmexecstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$ccmexec"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$ccmexec"
- Write-Output "$Computer, $ccmexec Failed to stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($ccmexecstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$ccmexec already stopped"
- }
- }
- }
- Function Stop-iphlpsvcIfItExists {
- $iphlpsvcexists = Confirm-iphlpsvcExists $iphlpsvcexists
- if ($iphlpsvcexists) {
- $iphlpsvcstatus = Get-Service -computername $Computer -name $iphlpsvc
- if ($iphlpsvcstatus | Where {$_.Status -eq "Running"})
- {
- #get-service -computername $computer $iphlpsvc | set-service -startuptype Disabled
- SC.exe ("\\" + $Computer) CONFIG $iphlpsvc start= disabled
- Start-sleep -seconds 2
- SC.exe ("\\" + $Computer) Stop $iphlpsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Stopping $iphlpsvc service"
- start-sleep 20
- $iphlpsvcstatus2 = Get-Service -computername $Computer -name $iphlpsvc
- if ($iphlpsvcstatus2 | Where {$_.Status -eq "Stopped"} ) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer "Service successfully stopped...$iphlpsvc"
- } else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "Failed to stop...$iphlpsvc"
- Write-Output "$Computer, $iphlpsvc Failed to stop, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- elseif ($iphlpsvcstatus | Where {$_.Status -eq "Stopped"}) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "$iphlpsvc already stopped"
- }
- }
- }
- Function Start-WuauservIfItExists {
- $wuauservexists = Confirm-wuauservExists $wuauservexists
- if ($wuauservexists) {
- #get-service -computername $computer $Wuauserv | set-service -startuptype automatic
- SC.exe ("\\" + $Computer) CONFIG $wuauserv start= auto
- Start-Sleep -seconds 2
- SC.exe ("\\" + $Computer) Start $Wuauserv
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer ("Service Starting..." + $Wuauserv)
- Start-Sleep -Seconds 20
- $status = Get-Service -computername $Computer $Wuauserv | where {$_.Status -eq "Running"}
- if (($status).status -eq "Running")
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer ("Service Started Successfully..." + $Wuauserv)
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Red" $Computer ("Service failed to start..." + $Wuauserv)
- Write-Output "$Computer, $Wuauserv Failed to start, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- }
- Function Start-AppIDsvcIfItExists {
- $AppIDsvcexists = Confirm-AppIDsvcExists $AppIDsvcexists
- if ($AppIDsvcexists) {
- #get-service -computername $computer $AppIDsvc | set-service -startuptype automatic
- SC.exe ("\\" + $Computer) CONFIG $appidsvc start= auto
- Start-Sleep -seconds 2
- SC.exe ("\\" + $Computer) Start $AppIDsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer ("Service Starting..." + $AppIDsvc)
- Start-Sleep -Seconds 20
- $status = Get-Service -computername $Computer $AppIDsvc | where {$_.Status -eq "Running"}
- if (($status).status -eq "Running")
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer ("Service Started Successfully..." + $AppIDsvc)
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Red" $Computer ("Service failed to start..." + $AppIDsvc)
- Write-Output "$Computer, $AppIDsvc failed to start, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- }
- Function Start-iphlpsvcIfItExists {
- $iphlpsvcexists = Confirm-iphlpsvcexists $iphlpsvcexists
- if ($iphlpsvcexists) {
- #get-service -computername $computer $iphlpsvc | set-service -startuptype automatic
- SC.exe ("\\" + $Computer) CONFIG $iphlpsvc start= auto
- Start-Sleep -seconds 2
- SC.exe ("\\" + $Computer) Start $iphlpsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer ("Service Starting..." + $iphlpsvc)
- Start-Sleep -Seconds 20
- $status = Get-Service -computername $Computer $iphlpsvc | where {$_.Status -eq "Running"}
- if (($status).status -eq "Running")
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer ("Service Started Successfully..." + $iphlpsvc)
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Red" $Computer ("Service failed to start..." + $iphlpsvc)
- Write-Output "$Computer, $iphlpsvc failed to start, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- }
- Function Start-CryptSvcIfItExists {
- $CryptSvcexists = Confirm-CryptSvcexists $CryptSvcexists
- if ($CryptSvcexists) {
- #get-service -computername $computer $cryptsvc | set-service -startuptype automatic
- SC.exe ("\\" + $Computer) CONFIG $cryptsvc start= auto
- Start-Sleep -seconds 2
- SC.exe ("\\" + $Computer) Start $cryptsvc
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer ("Service Starting..." + $cryptsvc)
- Start-Sleep -Seconds 20
- $status = Get-Service -computername $Computer $cryptsvc | where {$_.Status -eq "Running"}
- if (($status).status -eq "Running")
- {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" $Computer ("Service Started Successfully..." + $cryptsvc)
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Red" $Computer ("Service failed to start..." + $cryptsvc)
- Write-Output "$Computer, $cryptsvc failed to start, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- }
- Function Start-bitsIfItExists {
- $bitsexists = Confirm-bitsexists $bitsexists
- if ($bitsexists) {
- #get-service -computername $computer $bits | set-service -startuptype Manual
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Setting $bits to manual startup"
- SC.exe ("\\" + $Computer) CONFIG $bits start= demand
- Start-sleep -seconds 2
- ##MISSING A CHECK TO FIND OUT IF SERVICE WAS SUCCESSFULLY SET TO MANUAL HERE#####################################################################################
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Computer "Set $bits service to Manual startup ..it does not need to be started"
- }
- }
- Function Reset-security-descriptor-bits {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting to reset security descriptor on $bits service"
- $Setbitsdescriptor = SC.exe ("\\" + $Computer) sdset bits "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)"
- If ($Setbitsdescriptor -eq "[SC] SetServiceObjectSecurity SUCCESS") {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Reset Security Descriptor on $bits service"
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer"Failed to reset security descriptor on $bits service"
- }
- }
- Function Reset-security-descriptor-wuauserv {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting to reset security descriptor on $wuauserv service"
- $Setwuauservdescriptor = SC.exe ("\\" + $Computer) sdset wuauserv "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)"
- If ($Setwuauservdescriptor -eq "[SC] SetServiceObjectSecurity SUCCESS") {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer "Successfully Reset Security Descriptor on $wuauserv service"
- }
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red $Computer"Failed to reset security descriptor on $wuauserv service"
- }
- }
- Function reset-Bitsadmin {
- $computername = "\\$computer"
- $Scriptblock = "bitsadmin.exe /reset /allusers"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-atl-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s atl.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-urlmon-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s urlmon.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-mshtml-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s mshtml.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-shdocvw-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s shdocvw.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-browseui-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s browseui.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-jscript-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s jscript.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-vbscript-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s vbscript.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-scrrun-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s scrrun.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-msxml-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s msxml.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-msxml3-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s msxml3.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-msxml6-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s msxml6.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-actxprxy-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s actxprxy.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-softpub-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s softpub.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wintrust-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wintrust.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-dssenh-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s dssenh.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-rsaenh-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s rsaenh.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-gpkcsp-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s gpkcsp.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-sccbase-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s sccbase.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-slbcsp-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s slbcsp.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-cryptdlg-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s cryptdlg.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-oleaut32-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s oleaut32.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-ole32-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s ole32.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-shell32-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s shell32.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-initpki-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s initpki.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wuapi-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wuapi.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wuaueng-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wuaueng.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wuaueng1-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wuaueng1.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wucltui-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wucltui.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wups-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wups.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wups2-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wups2.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wuweb-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wuweb.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-qmgr-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s qmgr.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-qmgrprxy-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s qmgrprxy.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wucltux-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wucltux.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-muweb-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s muweb.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function registerWU-wuwebv-dll {
- $computername = "\\$computer"
- $Scriptblock = "regsvr32.exe /s wuwebv.dll"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor yellow $Computer "Attempting $Scriptblock"
- ## Prepare the command line for PsExec. We use the XML output encoding so
- ## that PowerShell can convert the output back into structured objects.
- ## PowerShell expects that you pass it some input when being run by PsExec
- ## this way, so the 'echo .' statement satisfies that appetite.
- $commandLine = "echo . | powershell -Output XML "
- ## Convert the command into an encoded command for PowerShell
- $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($scriptblock)
- $encodedCommand = [Convert]::ToBase64String($commandBytes)
- $commandLine += "-EncodedCommand $encodedCommand"
- ## Collect the output and error output
- $errorOutput = [IO.Path]::GetTempFileName()
- $output = psexec /acceptEula $computername cmd /c $commandLine 2>$errorOutput
- ## Check for any errors
- $errorContent = Get-Content $errorOutput
- Remove-Item $errorOutput
- if($errorContent -match "(Access is denied)|(failure)|(Couldn't)")
- {
- $OFS = "`n"
- $errorMessage = "Could not execute remote expression. "
- $errorMessage += "Ensure that your account has administrative " +
- "privileges on the target machine.`n"
- $errorMessage += ($errorContent -match "psexec.exe :")
- Write-Error $errorMessage
- }
- ## Return the output for use in if statement
- $output
- }
- Function IsItStillPinging {
- if (Test-Connection -computername $Computer -count 1 -quiet)
- {Return $True}
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "LOST CONNECTION"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "LOST CONNECTION"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $Computer "LOST CONNECTION"
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "LOST CONNECTION"
- Write-Output "$Computer, Lost Connection!, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Return $False}
- }
- #--------------------------------------------------------------------------------------------
- # MAIN SCRIPT ACTIONS
- #--------------------------------------------------------------------------------------------
- # Get our list of machines if it exists..
- #--------------------------------------------------------------------------------------------
- if (test-path -path "$inputfile" -erroraction SilentlyContinue) {
- Write-Host -foregroundcolor Green "Found: $Inputfile"
- $MasterList = gc $inputfile
- if ($MasterList -ne $null) {
- Write-Host -foregroundcolor cyan "Working from this list to clean based on WMI Success/Failure:"
- $MasterList
- Start-sleep -seconds 2
- #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
- #
- #
- # (Multi threaded) TESTING TO CLEAN MASTER LIST OF WMI FAILURES, REPORT ON THEM TO SHELL and LOG FILE and THEN fill $computers variable with ONLY the successful WMI checks.
- #
- #
- #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
- .\MultiThread\Run-CommandMultiThreaded.ps1 -Command "Verify-WMI" -InputParam computername -ObjectList $Masterlist
- # $Computers Variable equals the current content of the $successfulResults variable.
- $Computers = $SuccessfulResults
- #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
- #
- #
- # END OF TESTING TO CLEAN MASTER LIST OF WMI FAILURES
- #
- #
- #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
- # start iteration at 1
- $Iteration = 1
- # fill $Total with the count of computers we're going to work from
- $Total = $Computers.count
- #-----------------------------------#
- #-----------------------------------#
- #-----------------------------------#
- # START MAIN Foreach
- #-----------------------------------#
- #-----------------------------------#
- #-----------------------------------#
- ForEach ($Computer in $Computers) {
- # Set a value we can change later if things fail to do what we want them to and we want to halt the script.
- # as of writing NO functions cause the script to fail.
- $Continue = $True
- # more stolen secrets of the gods from Ken Hamilton RE: iteration
- $i = "{0:D4}" -f $Iteration
- $t = "{0:D4}" -f $Total
- if($WriteHost -eq $True){Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor cyan "$i of $t`t$($Computer.Name)"}
- # now that we've written the current machine's iteration, we increment that number so when the next machine goes through the loop, the number has gone up.
- $Iteration++
- # Write to log we are starting the process of remediation for this machine.
- Write-Output "$Computer, Starting Process, start,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- # Test to see if machine is pingable and only continue if it is
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "yellow" $Computer "Testing network connection"
- If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "Green" ($Computer + " is pingable")
- # Since we've determined WMI should work... this command should work to determine OS... If supported operating system, then continue:
- $OS = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem
- if ($OS.Caption -notlike "*2003*" -and $OS.Caption -notlike "*XP*" -and $OS.Caption -notlike "*10*") {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor green $Computer $OS.Caption
- # if it's a supported operating system, then do our functions...
- do {
- #Copy-SCCMClientInstallation
- #Stop-BitsIfItExists
- #Stop-WuauservIfItExists
- #Stop-appidsvcIfItExists
- #Stop-cryptsvcIfItExists
- #Stop-ccmexecIfItExists
- #Remove-qmgrdatfiles
- #Remove-WUSoftwareDistributionFolder
- #Rename-WUSoftwareDistributionFolder
- #Remove-CatRoot2Folder
- #Reset-security-descriptor-bits
- #Reset-security-descriptor-wuauserv
- #registerWU-atl-dll
- #registerWU-urlmon-dll
- #registerWU-mshtml-dll
- #registerWU-shdocvw-dll
- #registerWU-browseui-dll
- #registerWU-jscript-dll
- #registerWU-vbscript-dll
- #registerWU-scrrun-dll
- #registerWU-msxml-dll
- #registerWU-msxml3-dll
- #registerWU-msxml6-dll
- #registerWU-actxprxy-dll
- #registerWU-softpub-dll
- #registerWU-wintrust-dll
- #registerWU-dssenh-dll
- #registerWU-rsaenh-dll
- #registerWU-gpkcsp-dll
- #registerWU-sccbase-dll
- #registerWU-slbcsp-dll
- #registerWU-cryptdlg-dll
- #registerWU-oleaut32-dll
- #registerWU-ole32-dll
- #registerWU-shell32-dll
- #registerWU-initpki-dll
- #registerWU-wuapi-dll
- #registerWU-wuaueng-dll
- #registerWU-wuaueng1-dll
- #registerWU-wucltui-dll
- #registerWU-wups-dll
- #registerWU-wups2-dll
- #registerWU-wuweb-dll
- #registerWU-qmgr-dll
- #registerWU-qmgrprxy-dll
- #registerWU-wucltux-dll
- #registerWU-muweb-dll
- #registerWU-wuwebv-dll
- #Start-BitsIfItExists
- #Start-WuauservIfItExists
- #Start-AppIDsvcIfItExists
- #Start-CryptSvcIfItExists
- #reset-Bitsadmin
- #Remove-CCMCacheFolder
- #Uninstall-SCCMClient
- #Remove-CCMSetupFolder
- #Remove-CCMFolder
- #Remove-CCMNamespace
- #Remove-SMSNamespace
- #Install-SCCMClient
- #Force-WUAgentDetectnow
- $Continue = $False
- } while ($Continue -eq $True)
- # If theres a log file/s open for that machine in trace32, (we do the if because i dunno maybe someone closed it, or it's failed to open properly or something) wait 5 seconds and close it/them.
- # also don't want to close someone's log of looking at another pc >_> which is annoying and has happened to me a lot...
- if (Get-process trace32 | where {$_.MainWindowTitle -like "*$Computer*"}) {
- $seconds = 5
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") "$Computer Exiting log in $seconds seconds"
- Start-sleep -seconds $seconds
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "green" $Computer "Exiting Log"
- $process = get-process trace32 | where {$_.MainWindowTitle -like "*$Computer*"}
- $Process | Stop-Process
- }
- }
- Write-Output "$Computer, Ending Process, end,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- # Start of ELSE in OS Checking IF statement
- else {
- # OS type Unsupported..
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor Red $Computer "This machine will not be processed, Reason: Unsupported Machine Type: "$OS.Caption
- Write-Output "$Computer, Unsupported Machine Type: $OS.Caption, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Output "$Computer, Ending Process, end,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- #ping fails:
- else {
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor "red" $computer "Ping FAILED - machine offline or not listening for ICMP"
- Write-Output "$Computer, Failed to Ping - Machine offline or not listening for ICMP, error,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- Write-Output "$Computer, Ending Process, end,$(Get-Date -format "dd_MM_yyyy_HH:mm:ss")" | Add_To_Log
- }
- }
- #-----------------------------------#
- #-----------------------------------#
- #-----------------------------------#
- # END MAIN Foreach
- #-----------------------------------#
- #-----------------------------------#
- #-----------------------------------#
- }
- # End if - master list had content
- else {
- #Input file was empty?
- Write-Host -foregroundcolor red "InputFile empty?"
- }
- }
- else {
- # no input file even exists...
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") -foregroundcolor red "No servers.txt file found in: $WorkingDirectory\Input\"
- }
- #--------------------------------------------------------------------------------------------
- # MAIN SCRIPT ACTIONS
- #--------------------------------------------------------------------------------------------
- # Write where a user can get the log from...
- Write-Host $(Get-Date -format "dd_MM_yyyy_HH:mm:ss") "Output file saved here: $resultfile"
- # Then because i'm lazy i want it to open for them...
- Start-Sleep -seconds 2
- explorer.exe $logfolder
- ## END OF SCRIPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement