DBA Hub

📋Steps in this guide1/5

PowerShell : Useful Commands

A collection of commands and scripts I've found useful when working with Windows PowerShell.

oracle miscconfigurationintermediate
by OracleDba
14 views
1

Enable Scripts

By default scripting is disabled. You need to choose what type of scripts to accept: - Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode. - AllSigned - Only scripts signed by a trusted publisher can be run. - RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run. - Unrestricted - No restrictions; all Windows PowerShell scripts can be run. For example.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
PS C:\> Set-ExecutionPolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS C:\>
2

Sending Emails

A simple example of sending an email from Windows PowerShell.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
$smtpServer = "smtp.example.com"
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "[email protected]"
$msg.ReplyTo = "[email protected]"
$msg.To.Add("[email protected]")
$msg.subject = "Test Mail"
$msg.body = "This is a test mail."
$smtp.Send($msg)
3

Check if a process is running

Check is a process is running based on the executable name. Check is a process is running based on the process name.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
if(get-process | ?{$_.path -eq "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"}){
  # The process is running, so do something.
  Write-Host "FireFox is running!"
}

if(get-process firefox){
  # The process is running, so do something.
  Write-Host "FireFox is running!"
}
4

Kill a running process

Identify process of interest. Kill the process by name.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
PS C:\> get-process firefox

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    276      44   110232      94772   315     1.39   3176 firefox


PS C:\>

PS C:\> stop-process -name firefox -force

PS C:\> stop-process -id 3176 -force
5

Scheduling PowerShell scripts

- Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - Add arguments: C:\myscript.ps1 For more information see: - Windows PowerShell Hope this helps. Regards Tim...

Comments (0)

Please to add comments

No comments yet. Be the first to comment!