Pages

Sunday, 22 July 2012

PowerShell Execution Policy Commands



Set-ExecutionPolicy Restricted

Set-ExecutionPolicy AllSigned

Set-ExecutionPolicy Unrestricted

Set-ExecutionPolicy RemoteSigned

When any of the above command is run, following registry value changes:


HKLM\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

Restricted
AllSigned
RemoteSigned
Unrestricted

PowerShell Execution Policy

Following are the execution policies available in PowerShell


1. Restricted: PowerShell Execution policy is set to Restricted by default which means powershell scripts are not allowed for execution


2. All Signed: In this policy, only the signed powershell scripts are allowed for execution


3. RemoteSigned: In this policy, all powershell unsigned scripts present on local harddrive are allowed for execution but scripts are executed remotely from a network share or from internet are not allowed for executed, 


   remote scripts need to be signed for execution.


4. UnRestricted: In this policy all powershell scripts whether they are signed or unsigned are allowed for execution.



Friday, 20 July 2012

Start Service on Remote Computer - PowerShell


Following script will start the service "Browser"


$sb={
$service=get-service browser
if ($Service.status -eq "Stopped") {
write-Host "Starting service on $env:Computername" -foreground Green
$service | Start-service -PassThru
}
else {
$service
}
}

To run the above script type following command:

invoke-command $sb -comp remotecomputername

Result will be as follows: 


Connect to Remote Machine using Powershell



To connect to remote machine using PowerShell. WinRM (Windows Remote Management) service needs to be in started mode on the remote computer.


Then, run the following command on remote computer: WinRM quickconfig


Follow the steps displayed in command prompt to receive requests and remote management. Now the remote machine is ready to be accessed using powershell from any other computer.


Type following command in Powershell to access the remote machine:
-> Enter-PSSession ComputerName

Get Status of Service on a Remote Machine - PowerShell Command


Get-Service ServiceName -computername remotecompname | Select-object -Property Name,Status, MachineName

Get all services which are running - PowerShell Command


Get-Service | Where {$_.Status -eq "running"}

What are Cmdlets

Cmdlets are specialized commands in the PowerShell environment that implement specific functions. These are the native commands in the PowerShell stack. Cmdlets follow a <verb>-<noun> naming pattern, such as Get-ChildItem