Powershell

Setting the Max Memory in SQL Server

See the source image

Here is a quick post about setting the max memory in SQL Server so that you got no worries if you forgot to set this at installation.

I get it we all been there before. This was a step I over looked when starting out with SharePoint many years ago. However I did recently have this come up in a discussion and I remembered and thought I should do this in PowerShell to exercise my scripting technique. It seems to be best used as an expression.

Disclaimer:

Test this in a dev environment first and not production to ensure compatibility with versions and the .net framework:

The Nitty Gritty:


Function Set-SQLServerMemory
{
    [CmdletBinding()]
    Param(
            [parameter(ValueFromPipelineByPropertyName)][string[]]$ServerName,
            [parameter(ValueFromPipelineByPropertyName)][int[]]$MaxMemory,
            [parameter(ValueFromPipelineByPropertyName)][int[]]$MinMemory




         )
    Begin{
    write-verbose "Begining"
     $report=@()
     #$report+=New-Object -TypeName psobject -Property @{Servername="testServer"; TotalMemory=8076;} | select Servername,TotalMemory
     $servernames=$servername

    }

    Process{
    write-verbose "Begining the Process"

    foreach($ServerName in $ServerNames){
    Write-Verbose "In The Loop"
            $Rawmemorytotal = Get-wmiobject -ComputerName $ServerName -class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum
            $TotalMemory=$Rawmemorytotal.Sum
            $report+=New-Object -TypeName psobject -Property @{ 
                                                                ServerName="$Servername" 
                                                                TotalMemory=($Rawmemorytotal.Sum / 1MB)
                                                              }
    
            $min_os_memory = 2048 
            if( $TotalMemory -le $min_os_memory) {
                Write-Host "Memory too Low, Server could be Serverly impacted." -ForegroundColor Red 
            }
            if ($TotalMemory -ge 8192) {
                $sql_mem = $TotalMemory - $min_os_memory
            } else {
                $sql_mem = $TotalMemory * 0.79 ;
            }

    if ($MinMemory -eq 0) {
            $MinMemory = $MaxMemory
        }
        [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
        $SQLServer = New-Object Microsoft.SQLServer.Management.Smo.Server($SQLInstanceName)
        if ($SQLServer.status) {
              $SQLServer.Configuration.MaxServerMemory.ConfigValue = $MaxMemory
            $SQLServer.Configuration.MinServerMemory.ConfigValue = $MinMemory   
            $SQLServer.Configuration.Alter()
            }#end if

    
        }#end Foreach
        }

        End{
        return $report | select Servername,TotalMemory
        #return [int]$sql_mem ;  

        }
}


 

Powershell

Learning to access data from APIs (Invoke-WebRequest)

PowerShell is known for its wide capabilities to “shell” out changes and reporting data.

Websites are embracing the movement with data standards native to PowerShell and it’s cyphering functionality. Formats like JSON, and XML that are leveraged as responses for sites that load data.

So using invoke-webrequest and piping the results to convertfrom-json can turn this output in to readable in-memory native PowerShell styled .NET objects.

Using foreach-object and leveraging PowerShell 7 newest parallel feature could add some more speed to your larger task sets. I encourage you to explore these parameters.

 Check out the link for syntax

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7

 

 

Powershell

Scripting in other languges

Recently I noticed that there are not a lot of scriptwriters stick to PowerShell only.

There is a way to harness PowerShell to do other jobs like:

  • modify/build databases
  • Build/update websites
  • build and update dashboards
  • reconfigure Switches

I will be going over some simple examples that you can explore and build on.

Stay Tuned….

Powershell

Effective practices

I have been scripting for quite sometime and sometimes there are older CMD tools that just work.

Most would say why reinvent the wheel?

Well if the wheel wasn’t constantly being revised and upgraded then all the newer methods and functions wouldn’t be as robust.

With programming in c# there are methods and you can build functions there and in powershell to simply your daily task and pc deployments.

Don’t forget that most great ideas come from someone thinking it could be done better. Making things better would never happen if we don’t take steps to refine our processes we have grown so attached to.

Do you have a function that you created to make your life easier? Please share in the comments below.

Uncategorized

PowerShell to Manage thumbnail Cache databases used by file Explorer

So I have seen some issues with thumbnails in 1809 Enterprise edition.

When I am aggravated with them I clip them. it’s just good hygiene. lol

 

so here is a telltale script that does just that.

<#
Thumbnail Issue
-kill explorer tasks
-Remove Thumbnail databases
-restart explorer

#>


#-kill explorer tasks
write-host "stopping the Explorer Process."
Stop-Process -Name explorer -Force

#-Remove Thumbnail databases

Write-Host "These files need to be removed to ensure that they are not Corrupted:" -ForegroundColor Yellow
Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\" thumbcache_* | select -ExpandProperty fullname -OutVariable THUMBS

remove-item $THUMBS -Force  -ErrorAction SilentlyContinue -ErrorVariable ThumbErrors

new-item c:\ -name Logs -ItemType Directory -ErrorAction Stop; $THUMBS| Export-Csv THumbDBErrorLog.csv -NoTypeInformation

That is how you clip those unsightly thumbnails….

Functions, Scripting Technique, Uncategorized

User Back up function or Script

 

First I want to point out the $home variable for individual self initiated back ups.

so if you wanted to back up specifics to the current logged in user $home is Sufficient, even if you have DFS / or SMB redirected home folders.

if you are backing up all users you need to also save the sticky notes. that’s an added bonus to reading my article.

Function Get-Backup ($user){

# Get logged on Username
if ($User -like $null) {$User = $env:username}

Create path using variables so that we arent guessing usernames.
$path = "\\Server\Users\$user\user_backup\$(Get-Date -Format M)\"

#create newer Path based on the date.
new-item -ItemType directory -Path $path -ErrorAction SilentlyContinue
$logged ="$path\Results.txt"

#copy contents of vital folders specified below from there paths to the new location.

copy-item c:\users\$user\pictures -recurse -Destination $path -ErrorAction SilentlyContinue
copy-item c:\users\$user\documents -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\downloads -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\favorites -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\links -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\desktop -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\music -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
copy-item c:\users\$user\contacts -recurse -Destination $path -Verbose -ErrorAction SilentlyContinue
# I only included these folders because these are standard folders anything extra needs to be added manually.

I know a lot of people who are attached to their sticky notes.
located here:
$notes= ls "C:\Program Files\WindowsApps\microsoftstickynotes*"
copy-item $notes.fullname $path -Verbose -ErrorAction SilentlyContinue
}#End Function  you can always make this much more robust.

 

Enjoy City Life? I grew up in the city and I rather not deal with heavily populated areas. although I prefer people than bugs that goes for computer bugs too lol