Posts

Showing posts with the label PowerShell

Scheduling a PowerShell script

Make sure Powershell is installed and the execution policy is remote signed. To know more about how to check and set execution policy, see my post on  Executing a PowerShell script . Schedule the PowerShell script execution using the Windows task scheduler. Suppose your PowerShell script file is Main.ps1 and is located at “D:\Scripts” folder then following would be the command to run Powershell.exe D:\Scripts\Main.ps1

PowerShell script to get details of SQL Server instances

Following is the PowerShell script to list details of SQL Instances installed on a host. It gets details of even 32 bit installed on 64 bit host. Following is the script: $ComputerName=get-content env:computername $sqlInstLst = @() echo $ComputerName $RegPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL' if (Test-Path $RegPath){      try{          Get-ItemProperty -Path $RegPath      }     catch{           Write-Error $_.Exception.Message           return $false     } } else{      echo $ComputerName+'does not have SQL Server installed' } $RegPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\Instance Names\SQL' if (Test-Path $RegPath){      echo '32 bit instances on'+$ComputerName      try...

PowerShell script to find Windows computers in domain

When you are preparing for SQL Server inventory with any tool you will either have option to read entire AD for Server having SQL Server installed or you will need to feed the tool with a file having list of all the servers which you want to scan. Following is the PowerShell script to list all the windows machines from you domain: $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.PageSize = 9000 # Mention here how many results you need $objSearcher.Filter = '(OperatingSystem=*Window*)' # Filter for computer having words "Windows" in their operating system # Following options also can be used # '(OperatingSystem=*Window*Server*)'  # Above filter is for computer having words "Windows" and "Server" in their operating system # '(OperatingSystem=*Window*Server*2008*)'  # Above filter is for comput...

SQL Server Services Monitoring with PowerShell

Following is the PowerShell script to monitor SQL Server Services. I schedule it to run periodically on each Windows server hosting SQL Server instances, one or more than one. It checks for SQL Server database and agent services which are stopped and attempts to restart them. It also sends email if it finds a service in stopped state. $ServiceNameLike = "SQL Server (*" $Status = "Stopped" $To = "admin@email.com" $Node = gc env:computername $From = $Node +"@email.com" $smtpServer = "mail.server.com" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $StoppedServices=get-service | Where-Object {$_.DisplayName -like $ServiceNameLike -and $_.Status -eq $Status} if ($StoppedServices -ne $null) { foreach ($Service in $StoppedServices) { $Subject = "Attempting to restart service " + $Service.DisplayName + " on "+ $Node $Body = "Service " + $Service.DisplayName + " on "+ ...

Executing a PowerShell script

If you are facing following error message while trying to execute a PowerShell .PS1 file then the execution policy might be Restricted. File C:\Filepath\Filename.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details. To check the execution policy execute following command: Get-ExecutionPolicy The Restricted execution policy is the default PowerShell execution policy and it does not allow to run PowerShell script files but one can just execute individual commands. To enable execution of PowerShell script set appropriate execution policy for PowerShell. To know more about PowerShell execution policies click here . To set the PowerShell execution policy to RemoteSigned start PowerShell with Administrator privileges and execute following command: Set-ExecutionPolicy RemoteSigned Now from the PowerShell execute following command: C:\Filepath\Filename.ps1 Or if you are already in the dir...

Get disk space warnings through PowerShell

Image
Credits Susan NYC I use powershell scripts for disk free space monitoring on servers. Following are the task performed by the script: Clears the content of D:\lowdisk.txt. Adds required headers to D:\lowdisk.txt. Checks through all drives if free disk space is less than specified threshold. If a disk has free space less than threshold then adds info to D:\lowdisk.txt. Mails to users the contents of the  D:\lowdisk.txt if any disk is having free space less than threshold. To know how to execute the PowerShell scripts go to Executing a PowerShell script . Following is the code for the script. You can schedule it using windows scheduler. #Modify the below threshold value as required. It represents the minimum percent of free disk space required. $threshold = 10 #Add the recipients below. Example $To="user1@mail.com", "user2@mail.com", "user3@mail.com" $To = "email@mail.com" $smtpServer = "mail.server.com" $Node = gc env:comput...

Make Money from Surveys